Files
GoClaw/scripts/apply-migration.mjs
2026-03-20 17:34:20 -04:00

57 lines
1.7 KiB
JavaScript

import mysql from 'mysql2/promise';
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __dirname = dirname(fileURLToPath(import.meta.url));
async function main() {
const conn = await mysql.createConnection(process.env.DATABASE_URL);
try {
// Read the migration SQL
const sql = readFileSync(join(__dirname, '../drizzle/0001_secret_guardian.sql'), 'utf8');
// Split by statement-breakpoint and execute each statement
const statements = sql
.split('--> statement-breakpoint')
.map(s => s.trim())
.filter(s => s.length > 0);
console.log(`Executing ${statements.length} statements...`);
for (const stmt of statements) {
try {
await conn.query(stmt);
console.log('✓', stmt.slice(0, 60).replace(/\n/g, ' '));
} catch (e) {
if (e.code === 'ER_TABLE_EXISTS_ERROR' || e.message.includes('already exists')) {
console.log('⚠ Already exists (skipping):', stmt.slice(0, 60).replace(/\n/g, ' '));
} else {
console.error('✗ Error:', e.message, '\nSQL:', stmt.slice(0, 100));
}
}
}
// Mark migration as applied
try {
await conn.query(
'INSERT IGNORE INTO __drizzle_migrations (hash, created_at) VALUES (?, ?)',
['0001_secret_guardian_hash', Date.now()]
);
console.log('✓ Migration marked as applied');
} catch (e) {
console.log('Migration tracking:', e.message);
}
// Verify tables
const [tables] = await conn.query('SHOW TABLES');
console.log('\nAll tables:', tables.map(t => Object.values(t)[0]));
} finally {
await conn.end();
}
}
main().catch(console.error);