feat: implement auto-run migrations for medications table to handle image_url column
This commit is contained in:
@@ -8,3 +8,25 @@ const url = process.env.DATABASE_URL || "file:./data/medassist.db";
|
||||
const client = createClient({ url });
|
||||
|
||||
export const db = drizzle(client);
|
||||
|
||||
// Auto-run migrations on import (self-healing database)
|
||||
async function runMigrations() {
|
||||
const migrations = [
|
||||
{ name: "image_url", sql: "ALTER TABLE medications ADD COLUMN image_url TEXT" },
|
||||
];
|
||||
|
||||
for (const migration of migrations) {
|
||||
try {
|
||||
await client.execute(migration.sql);
|
||||
console.log(`[DB] Migration applied: ${migration.name}`);
|
||||
} catch (e: any) {
|
||||
// Ignore "duplicate column" errors - column already exists
|
||||
if (!e.message?.includes("duplicate column")) {
|
||||
console.error(`[DB] Migration error (${migration.name}):`, e.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Run migrations immediately
|
||||
runMigrations().catch(console.error);
|
||||
|
||||
Reference in New Issue
Block a user