feat(db): add migrations

This commit is contained in:
ItzCrazyKns
2025-09-03 14:36:47 +05:30
parent b0e8a33f1d
commit bb9eab7aa7
4 changed files with 186 additions and 3 deletions

View File

@@ -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`);

View File

@@ -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": {}
}
}

View File

@@ -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
}
]
}
}

View File

@@ -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<Document[]>(),
}).$type<Document[]>().default(sql`'[]'`),
});
interface File {