CREATE TABLE IF NOT EXISTS `entities_relations` (
	`id` text PRIMARY KEY NOT NULL,
	`organization_id` text NOT NULL,
	`project_id` text NOT NULL,
	`source_key` text NOT NULL,
	`source_id` text,
	`source_to_target_relation` text NOT NULL,
	`target_key` text NOT NULL,
	`target_id` text,
	`target_to_source_relation` text NOT NULL,
	`source_file` text,
	`file_hash` text,
	`created_at` text NOT NULL,
	`updated_at` text NOT NULL,
	`schema_version` integer DEFAULT 1 NOT NULL
);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `idx_entities_relations_source_key` ON `entities_relations` (`source_key`);--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `idx_entities_relations_target_key` ON `entities_relations` (`target_key`);--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `idx_entities_relations_source_target` ON `entities_relations` (`source_to_target_relation`);--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `idx_entities_relations_target_source` ON `entities_relations` (`target_to_source_relation`);--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS `idx_entities_relations_source_key_target_to_source_relation_target_key` ON `entities_relations` (`source_key`,`target_to_source_relation`,`target_key`);--> statement-breakpoint
CREATE TABLE IF NOT EXISTS `entities` (
	`id` text PRIMARY KEY NOT NULL,
	`organization_id` text NOT NULL,
	`project_id` text NOT NULL,
	`key` text NOT NULL,
	`type` text NOT NULL,
	`title` text NOT NULL,
	`summary` text,
	`tags` text,
	`metadata` text,
	`git` text,
	`contact` text,
	`links` text,
	`created_at` text NOT NULL,
	`updated_at` text NOT NULL,
	`source` text DEFAULT 'file',
	`source_file` text,
	`file_hash` text,
	`schema_version` integer DEFAULT 1 NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS `entities_key_unique` ON `entities` (`key`);--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `idx_entities_type` ON `entities` (`type`);--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS `idx_entities_key_source` ON `entities` (`key`,`source`);