DROP INDEX IF EXISTS `idx_entities_relations_source_key`;--> statement-breakpoint
DROP INDEX IF EXISTS `idx_entities_relations_target_key`;--> statement-breakpoint
DROP INDEX IF EXISTS `idx_entities_relations_source_target`;--> statement-breakpoint
DROP INDEX IF EXISTS `idx_entities_relations_target_source`;--> statement-breakpoint
DROP INDEX IF EXISTS `idx_entities_relations_unique`;--> statement-breakpoint
DROP INDEX IF EXISTS `idx_entities_type`;--> statement-breakpoint
DROP INDEX IF EXISTS `idx_entities_hash`;--> statement-breakpoint
DROP INDEX IF EXISTS `idx_entities_key_source_created_at`;--> statement-breakpoint
DROP INDEX IF EXISTS `idx_entities_key_source_is_current`;--> statement-breakpoint
DROP INDEX IF EXISTS `idx_entities_key_source_is_default`;--> statement-breakpoint
DROP INDEX IF EXISTS `idx_entities_key_source`;--> statement-breakpoint
-- Update all NULL values to '' first before creating unique index
UPDATE `entities_relations` SET "source_version" = '' WHERE "source_version" IS NULL;--> statement-breakpoint
UPDATE `entities_relations` SET "source_revision" = '' WHERE "source_revision" IS NULL;--> statement-breakpoint
UPDATE `entities_relations` SET "target_version" = '' WHERE "target_version" IS NULL;--> statement-breakpoint
UPDATE `entities_relations` SET "target_revision" = '' WHERE "target_revision" IS NULL;--> statement-breakpoint
-- Delete duplicate relations keeping only the most recent one (by id)
DELETE FROM `entities_relations` WHERE `id` NOT IN (
  SELECT MAX(`id`) FROM `entities_relations`
  GROUP BY `source_key`, `target_key`, `source_version`, `target_version`, `source_revision`, `target_revision`
);--> statement-breakpoint
-- We need to set default value to '', because NULL is not unique in sqlite
ALTER TABLE `entities_relations` ALTER COLUMN "source_version" TO "source_version" text NOT NULL DEFAULT '';--> statement-breakpoint
ALTER TABLE `entities_relations` ALTER COLUMN "source_revision" TO "source_revision" text NOT NULL DEFAULT '';--> statement-breakpoint
ALTER TABLE `entities_relations` ALTER COLUMN "target_version" TO "target_version" text NOT NULL DEFAULT '';--> statement-breakpoint
ALTER TABLE `entities_relations` ALTER COLUMN "target_revision" TO "target_revision" text NOT NULL DEFAULT '';--> statement-breakpoint
ALTER TABLE `entities_relations` ADD `is_deleted` integer DEFAULT false;--> statement-breakpoint
CREATE INDEX `idx_entities_relations_source_key` ON `entities_relations` (`source_key`,`is_deleted`);--> statement-breakpoint
CREATE INDEX `idx_entities_relations_target_key` ON `entities_relations` (`target_key`,`is_deleted`);--> statement-breakpoint
CREATE INDEX `idx_entities_relations_source_target` ON `entities_relations` (`source_to_target_relation`);--> statement-breakpoint
CREATE INDEX `idx_entities_relations_target_source` ON `entities_relations` (`target_to_source_relation`);--> statement-breakpoint
CREATE UNIQUE INDEX `idx_entities_relations_unique` ON `entities_relations` (`source_key`,`target_key`,`source_version`,`target_version`,`source_revision`,`target_revision`);--> statement-breakpoint
CREATE INDEX `idx_entities_type` ON `entities` (`type`);--> statement-breakpoint
CREATE INDEX `idx_entities_hash` ON `entities` (`hash`);--> statement-breakpoint
CREATE INDEX `idx_entities_key_source_created_at` ON `entities` (`key`,`created_at`);--> statement-breakpoint
ALTER TABLE `entities` ADD `is_deleted` integer DEFAULT false;--> statement-breakpoint
CREATE INDEX `idx_entities_key_source_is_current` ON `entities` (`key`,`is_current`,`is_deleted`);--> statement-breakpoint
CREATE INDEX `idx_entities_key_source_is_default` ON `entities` (`key`,`is_default_version`);--> statement-breakpoint
CREATE UNIQUE INDEX `idx_entities_key_source` ON `entities` (`key`,`source`,`revision`,`version`);--> statement-breakpoint
ALTER TABLE `entities_relations` DROP COLUMN `source_id`;--> statement-breakpoint
ALTER TABLE `entities_relations` DROP COLUMN `target_id`;--> statement-breakpoint
DROP INDEX IF EXISTS `idx_entities_key_source_is_current`;--> statement-breakpoint
UPDATE `entities` SET "version" = 'not_specified_version' WHERE "version" IS NULL;--> statement-breakpoint
ALTER TABLE `entities` ALTER COLUMN "version" TO "version" text NOT NULL DEFAULT 'not_specified_version';--> statement-breakpoint
UPDATE `entities` SET "revision" = '' WHERE "revision" IS NULL;--> statement-breakpoint
ALTER TABLE `entities` ALTER COLUMN "revision" TO "revision" text NOT NULL;
