fix: resolve database migration failures and startup ordering
- Add scripts/run-migrations.js to run migrations before data loading - Fix startup.sh ordering: migrations -> data load -> app start - Fix migration 0005 missing statement-breakpoint between ALTER TABLE statements - Add migration 0005 to drizzle journal (was missing) - Fix load-initial-data.js to check table existence before querying - Fix load-initial-data.js to create chart record before inserting candles (chart_id NOT NULL constraint) - Simplify db/index.ts migration error handling (remove overly broad 'already exists' catch) - Add pre-migration check for inconsistent DB state (tables without migration tracking)
This commit is contained in:
parent
573efea5b5
commit
2bde38d0bf
6 changed files with 87 additions and 17 deletions
51
scripts/run-migrations.js
Normal file
51
scripts/run-migrations.js
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const Database = require('better-sqlite3');
|
||||
|
||||
const dataDir = path.join(__dirname, '..', 'data');
|
||||
const dbPath = path.join(dataDir, 'candles.db');
|
||||
const migrationsFolder = path.join(__dirname, '..', 'drizzle');
|
||||
|
||||
// Ensure data directory exists
|
||||
if (!fs.existsSync(dataDir)) {
|
||||
fs.mkdirSync(dataDir, { recursive: true });
|
||||
}
|
||||
|
||||
// Check for inconsistent DB state before migrating
|
||||
if (fs.existsSync(dbPath)) {
|
||||
try {
|
||||
const checkDb = new Database(dbPath);
|
||||
const hasDrizzleMigrations = checkDb.prepare(
|
||||
"SELECT name FROM sqlite_master WHERE type='table' AND name='__drizzle_migrations'"
|
||||
).get();
|
||||
const hasAnyTables = checkDb.prepare(
|
||||
"SELECT name FROM sqlite_master WHERE type='table' AND name != '__drizzle_migrations'"
|
||||
).get();
|
||||
checkDb.close();
|
||||
|
||||
if (hasAnyTables && !hasDrizzleMigrations) {
|
||||
console.log('⚠️ Database has tables but no migration tracking. Recreating...');
|
||||
fs.unlinkSync(dbPath);
|
||||
}
|
||||
} catch {
|
||||
console.log('⚠️ Database file is corrupted. Recreating...');
|
||||
try { fs.unlinkSync(dbPath); } catch {}
|
||||
}
|
||||
}
|
||||
|
||||
// Run migrations using better-sqlite3 directly
|
||||
const { drizzle } = require('drizzle-orm/better-sqlite3');
|
||||
const { migrate } = require('drizzle-orm/better-sqlite3/migrator');
|
||||
|
||||
const sqlite = new Database(dbPath);
|
||||
const db = drizzle(sqlite);
|
||||
|
||||
try {
|
||||
migrate(db, { migrationsFolder });
|
||||
console.log('✅ Database migrations completed');
|
||||
} catch (error) {
|
||||
console.error('❌ Migration failed:', error);
|
||||
process.exit(1);
|
||||
} finally {
|
||||
sqlite.close();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue