DO $$DECLARE
   this_version CONSTANT varchar := '00.01.022';
BEGIN
   PERFORM * FROM "DBVersion" WHERE version = this_version;
   IF FOUND THEN RETURN; END IF;

        ALTER TABLE public."Credential" DROP CONSTRAINT fkey_credential_user;

        ALTER TABLE public."Credential"
            ADD FOREIGN KEY ("UserId")
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;
            
        ALTER TABLE public."DMP" DROP CONSTRAINT fk_dmp_creator;

        ALTER TABLE public."DMP"
            ADD FOREIGN KEY ("Creator")
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;
            
        ALTER TABLE public."Dataset" DROP CONSTRAINT fk_dataset_creator;

        ALTER TABLE public."Dataset"
            ADD FOREIGN KEY ("Creator")
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;
            
        ALTER TABLE public."Description" DROP CONSTRAINT "Description_created_by_fkey";

        ALTER TABLE public."Description"
            ADD FOREIGN KEY (created_by)
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;
            
        ALTER TABLE public."Dmp" DROP CONSTRAINT "Dmp_creator_fkey";

        ALTER TABLE public."Dmp"
            ADD FOREIGN KEY (creator)
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;
            
        ALTER TABLE public."DmpUser" DROP CONSTRAINT "DmpUser_user_fkey";

        ALTER TABLE public."DmpUser"
            ADD FOREIGN KEY ("user")
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;

        ALTER TABLE public."FileUpload" DROP CONSTRAINT fk_fileupload_creator;

        ALTER TABLE public."FileUpload"
            ADD FOREIGN KEY ("Creator")
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;
            
        ALTER TABLE public."Grant" DROP CONSTRAINT fk_grant_creator;

        ALTER TABLE public."Grant"
            ADD FOREIGN KEY ("CreationUser")
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;
            
        ALTER TABLE public."Invitation" DROP CONSTRAINT fk_invitation_creator;

        ALTER TABLE public."Invitation"
            ADD FOREIGN KEY ("CreationUser")
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;
            
        ALTER TABLE public."Lock" DROP CONSTRAINT "LockUserReference";

        ALTER TABLE public."Lock"
            ADD FOREIGN KEY ("LockedBy")
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;
            
        ALTER TABLE public."Notification" DROP CONSTRAINT "NotificationUserReference";

        ALTER TABLE public."Notification"
            ADD FOREIGN KEY ("UserId")
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;
            
        ALTER TABLE public."Project" DROP CONSTRAINT fk_project_creator;

        ALTER TABLE public."Project"
            ADD FOREIGN KEY ("CreationUser")
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;
            
        ALTER TABLE public."Reference" DROP CONSTRAINT "Reference_created_by_fkey";

        ALTER TABLE public."Reference"
            ADD FOREIGN KEY (created_by)
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;
            
        ALTER TABLE public."Tag" DROP CONSTRAINT "Tag_created_by_fkey";

        ALTER TABLE public."Tag"
            ADD FOREIGN KEY (created_by)
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;
            
        ALTER TABLE public."UserDMP" DROP CONSTRAINT "UserDMP_usr_fkey";

        ALTER TABLE public."UserDMP"
            ADD FOREIGN KEY (usr)
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;
            
        ALTER TABLE public."UserDescriptionTemplate" DROP CONSTRAINT "UserDatasetProfile_usr_fkey";

        ALTER TABLE public."UserDescriptionTemplate"
            ADD FOREIGN KEY ("user")
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;

--        ALTER TABLE public."UserPreference" DROP CONSTRAINT userpreference_user_fk;

--        ALTER TABLE public."UserPreference"
--            ADD FOREIGN KEY ("UserId")
--            REFERENCES public."User" (id) MATCH SIMPLE
--            ON UPDATE NO ACTION
--            ON DELETE NO ACTION
--            NOT VALID;
            
        ALTER TABLE public."UserToken" DROP CONSTRAINT fkey_usetoken_user;

        ALTER TABLE public."UserToken"
            ADD FOREIGN KEY ("UserId")
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID;

        INSERT INTO public."DBVersion" VALUES ('DMPDB', '00.01.022', '2023-11-20 12:00:00.000000+02', now(), 'Update user tables fks.');

END$$;