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

        TRUNCATE public."UserRole";

        ALTER TABLE public."UserRole" DROP COLUMN "Role";

        ALTER TABLE public."UserRole"
            RENAME "Id" TO id;

        ALTER TABLE public."UserRole"
            RENAME "UserId" TO "user";

        ALTER TABLE public."UserRole"
            ADD COLUMN role character varying(512) NOT NULL;

        ALTER TABLE public."UserRole"
            ADD COLUMN created_at timestamp without time zone NOT NULL;
        ALTER TABLE public."UserRole" DROP CONSTRAINT "UserRole_userId_fkey";

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

INSERT INTO public."DBVersion" VALUES ('DMPDB', '00.01.020', '2023-11-20 12:00:00.000000+02', now(), 'Sync table UserRole.');

END$$;