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

    ALTER TABLE public."Description"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."Description"
        ADD CONSTRAINT "Description_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."DescriptionReference"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."DescriptionReference"
        ADD CONSTRAINT "DescriptionReference_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."DescriptionTag"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."DescriptionTag"
        ADD CONSTRAINT "DescriptionTag_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."DescriptionTemplate"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."DescriptionTemplate"
        ADD CONSTRAINT "DescriptionTemplate_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."DescriptionTemplateType"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."DescriptionTemplateType"
        ADD CONSTRAINT "DescriptionTemplateType_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."DmpBlueprint"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."DmpBlueprint"
        ADD CONSTRAINT "DmpBlueprint_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."DmpDescriptionTemplate"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."DmpDescriptionTemplate"
        ADD CONSTRAINT "DmpDescriptionTemplate_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."Dmp"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."Dmp"
        ADD CONSTRAINT "Dmp_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."DmpReference"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."DmpReference"
        ADD CONSTRAINT "DmpReference_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."DmpUser"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."DmpUser"
        ADD CONSTRAINT "DmpUser_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."EntityDoi"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."EntityDoi"
        ADD CONSTRAINT "EntityDoi_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."Language"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."Language"
        ADD CONSTRAINT "Language_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."Reference"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."Reference"
        ADD CONSTRAINT "Reference_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."ReferenceType"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."ReferenceType"
        ADD CONSTRAINT "ReferenceType_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."StorageFile"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."StorageFile"
        ADD CONSTRAINT "StorageFile_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."SupportiveMaterial"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."SupportiveMaterial"
        ADD CONSTRAINT "SupportiveMaterial_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."Tag"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."Tag"
        ADD CONSTRAINT "Tag_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."UserContactInfo"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."UserContactInfo"
        ADD CONSTRAINT "UserContactInfo_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."UserCredential"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."UserCredential"
        ADD CONSTRAINT "UserCredential_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."UserDescriptionTemplate"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."UserDescriptionTemplate"
        ADD CONSTRAINT "UserDescriptionTemplate_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."UserRole"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."UserRole"
        ADD CONSTRAINT "UserRole_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;

    ALTER TABLE public."UserSettings"
        ADD COLUMN tenant uuid;
    ALTER TABLE public."UserSettings"
        ADD CONSTRAINT "UserSettings_tenant_fkey" FOREIGN KEY (tenant)
        REFERENCES public."Tenant" (id)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID;
        
    INSERT INTO public."DBVersion" VALUES ('DMPDB', '00.01.038', '2023-12-06 12:00:00.000000+02', now(), 'Add Tenant Id to tables.');

END$$;