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

    DROP TABLE public."Lock";

    CREATE TABLE public."Lock"
    (
        id uuid NOT NULL,
        target uuid NOT NULL,
        target_type smallint NOT NULL,
        locked_by uuid NOT NULL,
        locked_at timestamp without time zone NOT NULL,
        touched_at timestamp without time zone,
        tenant uuid,
        CONSTRAINT "Lock_pkey" PRIMARY KEY (id),
        CONSTRAINT "Lock_lockedby_fkey" FOREIGN KEY (locked_by)
            REFERENCES public."User" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID,
        CONSTRAINT "Lock_tenant_fkey" FOREIGN KEY (tenant)
            REFERENCES public."Tenant" (id) MATCH SIMPLE
            ON UPDATE NO ACTION
            ON DELETE NO ACTION
            NOT VALID
    );
        
    INSERT INTO public."DBVersion" VALUES ('DMPDB', '00.01.044', '2023-12-08 12:00:00.000000+02', now(), 'Drop old Lock Table and create New Lock table.');

END$$;