diff --git a/drizzle/0001_acoustic_wild_pack.sql b/drizzle/0001_acoustic_wild_pack.sql new file mode 100644 index 0000000..977e6b3 --- /dev/null +++ b/drizzle/0001_acoustic_wild_pack.sql @@ -0,0 +1,51 @@ +PRAGMA foreign_keys=OFF; +--> statement-breakpoint + +CREATE TABLE `__new_messages` ( + `id` integer PRIMARY KEY NOT NULL, + `type` text NOT NULL, + `chatId` text NOT NULL, + `createdAt` text DEFAULT CURRENT_TIMESTAMP NOT NULL, + `messageId` text NOT NULL, + `content` text, + `sources` text DEFAULT '[]' +); +--> statement-breakpoint + +INSERT INTO `__new_messages`("id", "type", "chatId", "createdAt", "messageId", "content", "sources") +SELECT + id, + COALESCE(type, 'user') as type, + chatId, + CASE + WHEN metadata IS NOT NULL AND instr(metadata, '\"createdAt\":\"') > 0 THEN + substr( + metadata, + instr(metadata, '\"createdAt\":\"') + 14, + CASE + WHEN instr(substr(metadata, instr(metadata, '\"createdAt\":\"') + 14), '\"') > 0 THEN + instr(substr(metadata, instr(metadata, '\"createdAt\":\"') + 14), '\"') - 1 + ELSE 24 + END + ) + ELSE CURRENT_TIMESTAMP + END as createdAt, + messageId, + content, + '[]' as sources +FROM `messages`; +--> statement-breakpoint + +DROP TABLE `messages`; +--> statement-breakpoint +ALTER TABLE `__new_messages` RENAME TO `messages`; +--> statement-breakpoint + +PRAGMA foreign_keys=ON; +--> statement-breakpoint + +CREATE INDEX IF NOT EXISTS `idx_messages_chatId` ON `messages` (`chatId`); +--> statement-breakpoint +CREATE INDEX IF NOT EXISTS `idx_messages_type` ON `messages` (`type`); +--> statement-breakpoint +CREATE INDEX IF NOT EXISTS `idx_messages_createdAt` ON `messages` (`createdAt`); \ No newline at end of file diff --git a/drizzle/meta/0001_snapshot.json b/drizzle/meta/0001_snapshot.json new file mode 100644 index 0000000..e131360 --- /dev/null +++ b/drizzle/meta/0001_snapshot.json @@ -0,0 +1,125 @@ +{ + "version": "6", + "dialect": "sqlite", + "id": "766180d3-caab-4b9e-9f35-61ee7b652903", + "prevId": "ef3a044b-0f34-40b5-babb-2bb3a909ba27", + "tables": { + "chats": { + "name": "chats", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "focusMode": { + "name": "focusMode", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "files": { + "name": "files", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false, + "default": "'[]'" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "messages": { + "name": "messages", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "chatId": { + "name": "chatId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "CURRENT_TIMESTAMP" + }, + "messageId": { + "name": "messageId", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "content": { + "name": "content", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "sources": { + "name": "sources", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false, + "default": "'[]'" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + } + }, + "views": {}, + "enums": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + }, + "internal": { + "indexes": {} + } +} \ No newline at end of file diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index 5db59d1..9c3456e 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -8,6 +8,13 @@ "when": 1748405503809, "tag": "0000_fuzzy_randall", "breakpoints": true + }, + { + "idx": 1, + "version": "6", + "when": 1756793457846, + "tag": "0001_acoustic_wild_pack", + "breakpoints": true } ] -} +} \ No newline at end of file diff --git a/src/lib/db/schema.ts b/src/lib/db/schema.ts index f040dfd..642708e 100644 --- a/src/lib/db/schema.ts +++ b/src/lib/db/schema.ts @@ -6,14 +6,14 @@ export const messages = sqliteTable('messages', { id: integer('id').primaryKey(), role: text('type', { enum: ['assistant', 'user', 'source'] }).notNull(), chatId: text('chatId').notNull(), - createdAt: text('createdAt').notNull(), + createdAt: text('createdAt').notNull().default(sql`CURRENT_TIMESTAMP`), messageId: text('messageId').notNull(), content: text('content'), sources: text('sources', { mode: 'json', - }).$type(), + }).$type().default(sql`'[]'`), }); interface File {