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

ALTER TABLE public."DmpBlueprint" ADD COLUMN "group_id" uuid;
UPDATE public."DmpBlueprint" SET "group_id" = uuid_generate_v4();
ALTER TABLE public."DmpBlueprint" ALTER COLUMN "group_id" SET NOT NULL;

ALTER TABLE public."DmpBlueprint" ADD COLUMN "version" integer;
UPDATE public."DmpBlueprint" SET "version" = 1;
ALTER TABLE public."DmpBlueprint" ALTER COLUMN "version" SET NOT NULL;

ALTER TABLE public."DmpBlueprint" ADD COLUMN "version_status" smallint;
UPDATE public."DmpBlueprint" SET "version_status" = 2 where "status" = 0;
UPDATE public."DmpBlueprint" SET "version_status" = 0 where "status" = 1;
ALTER TABLE public."DmpBlueprint" ALTER COLUMN "version_status" SET NOT NULL;

INSERT INTO public."DBVersion" VALUES ('DMPDB', '00.01.047', '2024-02-02 12:00:00.000000+02', now(), 'Add versioning on dmp blueprint table.');

END$$;