chore(docker): refactor Docker configuration for environment variables and database initialization
- Updated docker-compose.yml to use environment variable fallbacks for configuration. - Modified Dockerfile to streamline database initialization using Prisma migrations directly. - Removed init-db.js script as its functionality is now integrated into the Docker CMD.
This commit is contained in:
@@ -1,64 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Smart database initialization script
|
||||
* - Fresh DB (no tables) -> prisma db push (fast, applies full schema)
|
||||
* - Existing DB with data -> prisma migrate deploy (safe, incremental)
|
||||
*/
|
||||
|
||||
const { execSync } = require('child_process');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
function exec(command) {
|
||||
try {
|
||||
return execSync(command, { encoding: 'utf-8', stdio: 'pipe' });
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function getDatabasePath() {
|
||||
const dbUrl = process.env.DATABASE_URL || 'file:./data/prod.db';
|
||||
// Extract path from SQLite URL (file:./path or file:/absolute/path)
|
||||
return dbUrl.replace(/^file:/, '');
|
||||
}
|
||||
|
||||
function isDatabaseEmpty() {
|
||||
const dbPath = getDatabasePath();
|
||||
const absolutePath = path.resolve(dbPath);
|
||||
|
||||
console.log(`🔍 Checking database at: ${absolutePath}`);
|
||||
|
||||
// Si le fichier DB n'existe pas, c'est vide
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
console.log('📦 Database file does not exist - fresh install');
|
||||
return true;
|
||||
}
|
||||
|
||||
// Vérifier si des tables existent
|
||||
const result = exec('pnpm prisma db execute --stdin <<< ".tables"');
|
||||
if (!result || result.trim() === '') {
|
||||
console.log('📦 Database exists but has no tables - fresh install');
|
||||
return true;
|
||||
}
|
||||
|
||||
console.log('🔄 Database has existing tables');
|
||||
return false;
|
||||
}
|
||||
|
||||
function main() {
|
||||
console.log('🚀 Starting database initialization...\n');
|
||||
|
||||
if (isDatabaseEmpty()) {
|
||||
console.log('✨ Applying full schema with prisma db push...');
|
||||
execSync('pnpm prisma db push --skip-generate', { stdio: 'inherit' });
|
||||
} else {
|
||||
console.log('📝 Applying migrations with prisma migrate deploy...');
|
||||
execSync('pnpm prisma migrate deploy', { stdio: 'inherit' });
|
||||
}
|
||||
|
||||
console.log('\n✅ Database ready!');
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
Reference in New Issue
Block a user