feat(db): fix migration errors, explicitly migrate based on index

This commit is contained in:
ItzCrazyKns
2025-09-26 13:44:39 +05:30
parent 984163bbbc
commit 650c69e04f
7 changed files with 103 additions and 62 deletions

View File

@@ -1,5 +1,89 @@
import db from './';
import { migrate } from 'drizzle-orm/better-sqlite3/migrator';
import Database from 'better-sqlite3';
import path from 'path';
import fs from 'fs';
migrate(db, { migrationsFolder: path.join(process.cwd(), 'drizzle') });
const db = new Database(path.join(process.cwd(), 'data', 'db.sqlite'));
const migrationsFolder = path.join(process.cwd(), 'drizzle');
db.exec(`
CREATE TABLE IF NOT EXISTS ran_migrations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
run_on DATETIME DEFAULT CURRENT_TIMESTAMP
);
`);
function sanitizeSql(content: string) {
return content
.split(/\r?\n/)
.filter((l) => !l.trim().startsWith('-->') && !l.includes('statement-breakpoint'))
.join('\n');
}
fs.readdirSync(migrationsFolder)
.filter((f) => f.endsWith('.sql'))
.sort()
.forEach((file) => {
const filePath = path.join(migrationsFolder, file);
let content = fs.readFileSync(filePath, 'utf-8');
content = sanitizeSql(content);
const migrationName = file.split('_')[0] || file;
const already = db.prepare('SELECT 1 FROM ran_migrations WHERE name = ?').get(migrationName);
if (already) {
console.log(`Skipping already-applied migration: ${file}`);
return;
}
try {
if (migrationName === '0001') {
const messages = db.prepare('SELECT id, type, metadata, content, chatId, messageId FROM messages').all();
db.exec(`
CREATE TABLE IF NOT EXISTS messages_with_sources (
id INTEGER PRIMARY KEY,
type TEXT NOT NULL,
chatId TEXT NOT NULL,
createdAt TEXT NOT NULL,
messageId TEXT NOT NULL,
content TEXT,
sources TEXT DEFAULT '[]'
);
`);
const insertMessage = db.prepare(`
INSERT INTO messages_with_sources (type, chatId, createdAt, messageId, content, sources)
VALUES (?, ?, ?, ?, ?, ?)
`);
messages.forEach((msg: any) => {
if (msg.type === 'user') {
msg.metadata = JSON.parse(msg.metadata || '{}');
insertMessage.run('user', msg.chatId, msg.metadata['createdAt'], msg.messageId, msg.content, '[]');
} else if (msg.type === 'assistant') {
msg.metadata = JSON.parse(msg.metadata || '{}');
insertMessage.run('assistant', msg.chatId, msg.metadata['createdAt'], msg.messageId, msg.content, '[]');
const sources = msg.metadata['sources'] || '[]'
if (sources && sources.length > 0) {
insertMessage.run('source', msg.chatId, msg.metadata['createdAt'], `${msg.messageId}-source`, '', JSON.stringify(sources));
}
}
});
db.exec('DROP TABLE messages;');
db.exec('ALTER TABLE messages_with_sources RENAME TO messages;');
} else {
db.exec(content);
}
db.prepare('INSERT OR IGNORE INTO ran_migrations (name) VALUES (?)').run(migrationName);
console.log(`Applied migration: ${file}`);
} catch (err) {
console.error(`Failed to apply migration ${file}:`, err);
throw err;
}
});