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
|
|
@ -15,8 +15,9 @@ const dbPath = path.join(dataDir, 'candles.db');
|
|||
const sqlite = new Database(dbPath);
|
||||
export const db = drizzle(sqlite, { schema });
|
||||
|
||||
// Run migrations only in runtime (not during build)
|
||||
// Check if we're in build mode by looking for the NEXT_PHASE environment variable
|
||||
// Run migrations at startup (for local dev).
|
||||
// In Docker, migrations are run by scripts/run-migrations.js before the app starts,
|
||||
// so this will be a no-op (all migrations already applied).
|
||||
const isBuildTime = process.env.NEXT_PHASE === 'phase-production-build' || process.env.NEXT_PHASE === 'phase-development-build';
|
||||
|
||||
if (!isBuildTime) {
|
||||
|
|
@ -24,16 +25,8 @@ if (!isBuildTime) {
|
|||
migrate(db, { migrationsFolder: path.join(process.cwd(), 'drizzle') });
|
||||
console.log('✅ Database migrations completed');
|
||||
} catch (error) {
|
||||
// Check if error is about table already existing - this is expected when container restarts with existing volume
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
const causeMessage = error instanceof Error && error.cause ? String(error.cause) : '';
|
||||
|
||||
if (errorMessage.includes('already exists') || causeMessage.includes('already exists')) {
|
||||
console.log('ℹ️ Database schema already up to date');
|
||||
} else {
|
||||
console.error('❌ Migration failed:', error);
|
||||
throw error;
|
||||
}
|
||||
console.error('❌ Migration failed:', error);
|
||||
throw error;
|
||||
}
|
||||
} else {
|
||||
console.log('ℹ️ Skipping migrations during build phase');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue