CREATE TABLE `chat_messages` ( `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL, `chat_id` integer NOT NULL, `thread_key` text, `thread_title` text, `kind` text NOT NULL, `important` integer DEFAULT false NOT NULL, `author_kind` text DEFAULT 'system' NOT NULL, `author_user_id` integer, `author_login` text, `via` text, `text` text NOT NULL, `question_id` integer, `task_id` integer, `url` text, `dedupe_key` text, `tg_chat_id` integer, `tg_message_id` integer, `created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL, FOREIGN KEY (`chat_id`) REFERENCES `chats`(`id`) ON UPDATE no action ON DELETE cascade, FOREIGN KEY (`author_user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE set null, FOREIGN KEY (`question_id`) REFERENCES `questions`(`id`) ON UPDATE no action ON DELETE set null, FOREIGN KEY (`task_id`) REFERENCES `tasks`(`id`) ON UPDATE no action ON DELETE set null ); --> statement-breakpoint CREATE UNIQUE INDEX `chat_messages_dedupe_key_unique` ON `chat_messages` (`dedupe_key`);--> statement-breakpoint CREATE INDEX `chat_messages_chat` ON `chat_messages` (`chat_id`,`id`);--> statement-breakpoint CREATE INDEX `chat_messages_created_at` ON `chat_messages` (`created_at`);--> statement-breakpoint CREATE INDEX `chat_messages_tg` ON `chat_messages` (`tg_chat_id`,`tg_message_id`);--> statement-breakpoint CREATE TABLE `chats` ( `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL, `kind` text NOT NULL, `user_id` integer, `project_id` integer, `tg_chat_id` integer, `tg_title` text, `tg_linked_at` integer, `tg_linked_by` integer, `created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL, FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade, FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE cascade, FOREIGN KEY (`tg_linked_by`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE set null ); --> statement-breakpoint CREATE UNIQUE INDEX `chats_user_id_unique` ON `chats` (`user_id`);--> statement-breakpoint CREATE UNIQUE INDEX `chats_project_id_unique` ON `chats` (`project_id`);--> statement-breakpoint CREATE UNIQUE INDEX `chats_tg_chat_id_unique` ON `chats` (`tg_chat_id`);--> statement-breakpoint ALTER TABLE `telegram_outbox` ADD `chat_message_id` integer REFERENCES chat_messages(id) ON DELETE set null;