PRAGMA foreign_keys=OFF;--> statement-breakpoint
CREATE TABLE `__new_contexts` (
	`id` text NOT NULL,
	`user_id` text NOT NULL,
	`name` text NOT NULL,
	`type` text NOT NULL,
	`sort_order` integer DEFAULT 0 NOT NULL,
	`meta` text,
	`archived_at` text,
	`is_favorite` integer DEFAULT false NOT NULL,
	`updated_at` text NOT NULL,
	`deleted_at` text,
	`version` integer DEFAULT 1 NOT NULL,
	PRIMARY KEY(`id`, `user_id`)
);
--> statement-breakpoint
INSERT INTO `__new_contexts`("id", "user_id", "name", "type", "sort_order", "meta", "archived_at", "is_favorite", "updated_at", "deleted_at", "version") SELECT "id", '', "name", "type", "sort_order", "meta", "archived_at", "is_favorite", "updated_at", "deleted_at", "version" FROM `contexts`;--> statement-breakpoint
DROP TABLE `contexts`;--> statement-breakpoint
ALTER TABLE `__new_contexts` RENAME TO `contexts`;--> statement-breakpoint
CREATE INDEX `contexts_updated_at_idx` ON `contexts` (`updated_at`);--> statement-breakpoint
CREATE INDEX `contexts_user_id_idx` ON `contexts` (`user_id`);--> statement-breakpoint
CREATE TABLE `__new_topics` (
	`id` text NOT NULL,
	`user_id` text NOT NULL,
	`context_id` text NOT NULL,
	`title` text NOT NULL,
	`status` text DEFAULT 'active' NOT NULL,
	`snooze_until` text,
	`sort_order` integer DEFAULT 0 NOT NULL,
	`is_new` integer DEFAULT true NOT NULL,
	`updated_at` text NOT NULL,
	`deleted_at` text,
	`version` integer DEFAULT 1 NOT NULL,
	PRIMARY KEY(`id`, `user_id`),
	FOREIGN KEY (`context_id`,`user_id`) REFERENCES `contexts`(`id`,`user_id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_topics`("id", "user_id", "context_id", "title", "status", "snooze_until", "sort_order", "is_new", "updated_at", "deleted_at", "version") SELECT "id", '', "context_id", "title", "status", "snooze_until", "sort_order", "is_new", "updated_at", "deleted_at", "version" FROM `topics`;--> statement-breakpoint
DROP TABLE `topics`;--> statement-breakpoint
ALTER TABLE `__new_topics` RENAME TO `topics`;--> statement-breakpoint
CREATE INDEX `topics_updated_at_idx` ON `topics` (`updated_at`);--> statement-breakpoint
CREATE INDEX `topics_context_id_idx` ON `topics` (`context_id`);--> statement-breakpoint
CREATE INDEX `topics_user_id_idx` ON `topics` (`user_id`);--> statement-breakpoint
CREATE TABLE `__new_history_entries` (
	`id` text NOT NULL,
	`user_id` text NOT NULL,
	`topic_id` text NOT NULL,
	`date` text NOT NULL,
	`text` text NOT NULL,
	`sort_order` integer DEFAULT 0 NOT NULL,
	`linked_context_id` text,
	`done_at` text,
	`updated_at` text NOT NULL,
	`deleted_at` text,
	`version` integer DEFAULT 1 NOT NULL,
	PRIMARY KEY(`id`, `user_id`),
	FOREIGN KEY (`topic_id`,`user_id`) REFERENCES `topics`(`id`,`user_id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_history_entries`("id", "user_id", "topic_id", "date", "text", "sort_order", "linked_context_id", "done_at", "updated_at", "deleted_at", "version") SELECT "id", '', "topic_id", "date", "text", "sort_order", "linked_context_id", "done_at", "updated_at", "deleted_at", "version" FROM `history_entries`;--> statement-breakpoint
DROP TABLE `history_entries`;--> statement-breakpoint
ALTER TABLE `__new_history_entries` RENAME TO `history_entries`;--> statement-breakpoint
CREATE INDEX `history_entries_updated_at_idx` ON `history_entries` (`updated_at`);--> statement-breakpoint
CREATE INDEX `history_entries_topic_id_idx` ON `history_entries` (`topic_id`);--> statement-breakpoint
CREATE INDEX `history_entries_user_id_idx` ON `history_entries` (`user_id`);--> statement-breakpoint
CREATE TABLE `__new_ratings` (
	`id` text NOT NULL,
	`user_id` text NOT NULL,
	`topic_id` text NOT NULL,
	`history_entry_id` text NOT NULL,
	`person_name` text NOT NULL,
	`value` integer NOT NULL,
	`comment` text,
	`updated_at` text NOT NULL,
	`deleted_at` text,
	`version` integer DEFAULT 1 NOT NULL,
	PRIMARY KEY(`id`, `user_id`),
	FOREIGN KEY (`topic_id`,`user_id`) REFERENCES `topics`(`id`,`user_id`) ON UPDATE no action ON DELETE no action,
	FOREIGN KEY (`history_entry_id`,`user_id`) REFERENCES `history_entries`(`id`,`user_id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `__new_ratings`("id", "user_id", "topic_id", "history_entry_id", "person_name", "value", "comment", "updated_at", "deleted_at", "version") SELECT "id", '', "topic_id", "history_entry_id", "person_name", "value", "comment", "updated_at", "deleted_at", "version" FROM `ratings`;--> statement-breakpoint
DROP TABLE `ratings`;--> statement-breakpoint
ALTER TABLE `__new_ratings` RENAME TO `ratings`;--> statement-breakpoint
CREATE INDEX `ratings_updated_at_idx` ON `ratings` (`updated_at`);--> statement-breakpoint
CREATE INDEX `ratings_topic_id_idx` ON `ratings` (`topic_id`);--> statement-breakpoint
CREATE INDEX `ratings_user_id_idx` ON `ratings` (`user_id`);--> statement-breakpoint
PRAGMA foreign_keys=ON;
