fix: replace all SQLite references with PostgreSQL in scripts and lazy-init db connection
- Rewrite scripts/run-migrations.js for PostgreSQL (was better-sqlite3) - Rewrite scripts/load-initial-data.js for PostgreSQL (was better-sqlite3) - Make db connection lazy in src/lib/db/index.ts to avoid build-time errors when DATABASE_URL is not available in Docker build stage
This commit is contained in:
parent
2481fda68c
commit
d5fc4662e9
3 changed files with 139 additions and 149 deletions
|
|
@ -1,51 +1,25 @@
|
|||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const Database = require('better-sqlite3');
|
||||
const { Pool } = require('pg');
|
||||
const { drizzle } = require('drizzle-orm/node-postgres');
|
||||
const { migrate } = require('drizzle-orm/node-postgres/migrator');
|
||||
|
||||
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);
|
||||
const DATABASE_URL = process.env.DATABASE_URL;
|
||||
if (!DATABASE_URL) {
|
||||
console.error('❌ DATABASE_URL environment variable is not set');
|
||||
process.exit(1);
|
||||
} finally {
|
||||
sqlite.close();
|
||||
}
|
||||
|
||||
const pool = new Pool({ connectionString: DATABASE_URL, max: 2 });
|
||||
const db = drizzle(pool);
|
||||
|
||||
migrate(db, { migrationsFolder })
|
||||
.then(() => {
|
||||
console.log('✅ Database migrations completed');
|
||||
return pool.end();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('❌ Migration failed:', error);
|
||||
pool.end().finally(() => process.exit(1));
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue