chore(docker): update Docker configuration for database initialization
- Changed exposed port from 3006 to 3007 in docker-compose.yml. - Updated Dockerfile to copy init-db.js script and modified CMD to use it for database initialization instead of Prisma migrations.
This commit is contained in:
@@ -68,6 +68,9 @@ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
|
|||||||
# Copy Prisma schema
|
# Copy Prisma schema
|
||||||
COPY --from=builder /app/prisma ./prisma
|
COPY --from=builder /app/prisma ./prisma
|
||||||
|
|
||||||
|
# Copy scripts (including init-db.js)
|
||||||
|
COPY --from=builder /app/scripts ./scripts
|
||||||
|
|
||||||
# Copy pnpm node_modules (includes .pnpm store with Prisma client)
|
# Copy pnpm node_modules (includes .pnpm store with Prisma client)
|
||||||
COPY --from=builder /app/node_modules ./node_modules
|
COPY --from=builder /app/node_modules ./node_modules
|
||||||
|
|
||||||
@@ -83,5 +86,5 @@ USER nextjs
|
|||||||
|
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|
||||||
# Start the application with database migration
|
# Start the application with smart database initialization
|
||||||
CMD ["sh", "-c", "pnpm prisma migrate deploy && node server.js"]
|
CMD ["sh", "-c", "node scripts/init-db.js && node server.js"]
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ services:
|
|||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
target: runner
|
target: runner
|
||||||
ports:
|
ports:
|
||||||
- '3006:3000'
|
- '3007:3000'
|
||||||
environment:
|
environment:
|
||||||
NODE_ENV: production
|
NODE_ENV: production
|
||||||
DATABASE_URL: 'file:../data/dev.db' # Prisma
|
DATABASE_URL: 'file:../data/dev.db' # Prisma
|
||||||
@@ -49,7 +49,7 @@ services:
|
|||||||
command: >
|
command: >
|
||||||
sh -c "pnpm install &&
|
sh -c "pnpm install &&
|
||||||
pnpm prisma generate &&
|
pnpm prisma generate &&
|
||||||
pnpm prisma migrate deploy &&
|
node scripts/init-db.js &&
|
||||||
pnpm run dev"
|
pnpm run dev"
|
||||||
profiles:
|
profiles:
|
||||||
- dev
|
- dev
|
||||||
|
|||||||
64
scripts/init-db.js
Executable file
64
scripts/init-db.js
Executable file
@@ -0,0 +1,64 @@
|
|||||||
|
#!/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