fix: skip migrations during build phase to prevent 'table already exists' errors

This commit is contained in:
Marko Djordjevic 2026-02-16 19:41:22 +01:00
parent ecb23855b5
commit bbaf330020
4 changed files with 40 additions and 9 deletions

View file

@ -15,11 +15,24 @@ const dbPath = path.join(dataDir, 'candles.db');
const sqlite = new Database(dbPath);
export const db = drizzle(sqlite, { schema });
// Run migrations on startup
try {
migrate(db, { migrationsFolder: path.join(process.cwd(), 'drizzle') });
console.log('✅ Database migrations completed');
} catch (error) {
console.error('❌ Migration failed:', error);
throw error;
// Run migrations only in runtime (not during build)
// Check if we're in build mode by looking for the NEXT_PHASE environment variable
const isBuildTime = process.env.NEXT_PHASE === 'phase-production-build' || process.env.NEXT_PHASE === 'phase-development-build';
if (!isBuildTime) {
try {
migrate(db, { migrationsFolder: path.join(process.cwd(), 'drizzle') });
console.log('✅ Database migrations completed');
} catch (error) {
// Check if error is about table already existing
const errorMessage = error instanceof Error ? error.message : String(error);
if (errorMessage.includes('already exists')) {
console.log('⚠️ Database tables already exist, skipping migrations');
} else {
console.error('❌ Migration failed:', error);
throw error;
}
}
} else {
console.log(' Skipping migrations during build phase');
}