DO $$
    DECLARE
        _path character varying(256) := 'patch-2026_01_30-EntityLifetimes';
    BEGIN
        IF EXISTS (SELECT 1 FROM public."Versions" WHERE "Path" = _path) THEN
            RETURN;
        END IF;

        IF EXISTS (SELECT 1 FROM public."Versions" WHERE "Build" >= 16999) THEN
            RETURN;
        END IF;

        BEGIN
 

	CREATE TABLE IF NOT EXISTS public."EntityLifetimes"
	(
    	"Id" bigserial NOT NULL,
    	"EntityId" bigint NOT NULL,
    	"EntityType" integer NOT NULL,
    	"ExpirationDate" timestamp without time zone NOT NULL,
    	"Timestamp" timestamp without time zone NOT NULL DEFAULT timezone('utc'::text, now()),
    	CONSTRAINT "EntityLifetimes_pkey" PRIMARY KEY ("Id")
	);

	CREATE INDEX IF NOT EXISTS "IDX_EntityLifetimes-ExpirationDate"
    		ON public."EntityLifetimes" USING btree
    		("ExpirationDate" ASC)
    		TABLESPACE pg_default;

	CREATE UNIQUE INDEX IF NOT EXISTS "IDX_EntityLifetimes-EntityType-EntityId"
   	 ON public."EntityLifetimes" USING btree
    	("EntityType" ASC, "EntityId" ASC)
    	TABLESPACE pg_default;


            INSERT INTO public."Versions" ("Path", "Build") VALUES (_path, 0);

        EXCEPTION
            WHEN OTHERS THEN
                RAISE NOTICE '[ERR] %: %', _path, SQLERRM;
        END;

    END;
$$;