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

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

	BEGIN
		CREATE TABLE IF NOT EXISTS public."EmailMessageAutoActions"
( 
	"UserEmailId" int8 NOT NULL,
	"HashCode" varchar(64) NOT NULL,
	"ActionType" int4 NOT NULL,
	"ToSend" varchar(64) NOT NULL,
	"Timestamp" timestamp without time zone DEFAULT timezone('utc', now())  NOT NULL
);

CREATE UNIQUE INDEX IF NOT EXISTS "IDX_EmailMessageAutoActions_HashCode"  
	ON public."EmailMessageAutoActions"
	("UserEmailId", "HashCode", "ActionType","ToSend" )
    TABLESPACE pg_default;

		INSERT INTO public."Versions" ("Path", "Build")
		VALUES (_path, 0);
	EXCEPTION
		WHEN OTHERS THEN
			RAISE NOTICE '[ERR] %: %', _path, SQLERRM;
	END;
END;
$$;
