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

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

		BEGIN
			alter table public."ProjectStatuses" add column IF NOT exists "IsDefault" bool DEFAULT false NOT null;

			CREATE UNIQUE INDEX IF NOT exists "IDX_ProjectStatuses-EnitiyId-Type-IsDefault" ON public."ProjectStatuses" USING btree("EntityId", "EntityType", "Type", "IsDefault") where ("IsDefault" = true);

			 update public."ProjectStatuses" set "IsDefault" = true
				 where "EntityType" in (38,41) and "Type" = 5 and "IsDeleted" = false and ("PatternId" in 
				 (select "Id" from public."ProjectStatuses" where "EntityId" IS NULL AND "EntityType" in (38,41)) or "EntityId" IS NULL);

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