Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"prepare": "test -d node_modules/husky && husky install || echo \"husky is not installed\"",
"lint": "eslint --fix --ext .js .",
"lint:staged": "lint-staged",
"dev": "npx nodemon --exec npx babel-node src/core/bin/www.js",
"dev": "node scripts/db-check.js && npx nodemon --exec npx babel-node src/core/bin/www.js",
"start": "npm run build && node ./dist/core/bin/www.js",
"clean": "rimraf dist && mkdir dist",
"build:babel": "babel ./src --out-dir dist --copy-files",
Expand Down
47 changes: 47 additions & 0 deletions backend/scripts/db-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require('dotenv').config();
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

async function checkAndMigrate() {
console.log('🔍 Checking database migration status...');

// Ensure Prisma client is generated
const clientPath = path.join(__dirname, '../node_modules/.prisma/client');
if (!fs.existsSync(clientPath)) {
console.log('Prisma client not found. Generating...');
try {
execSync('npx prisma generate --schema=prisma/schema', { stdio: 'inherit' });
} catch (err) {
console.error('Failed to generate Prisma client:', err.message);
}
}

try {
// Run migrate diff with exit-code flag.
// Exit code 0 = no diff (in sync), 2 = diff detected, 1 = error.
execSync('npx prisma migrate diff --exit-code --from-config-datasource --to-schema=prisma/schema', { stdio: 'ignore' });
console.log('✅ Database schema is up to date. Skipping migrations and seeding.');
} catch (error) {
// execSync throws an error if exit code is non-zero
if (error.status === 2) {
console.log('⚠️ Detected database schema differences. Applying migrations and seeding...');
try {
// Apply/create migrations (interactive in dev, will prompt for name if there are unmigrated changes)
execSync('npx prisma migrate dev', { stdio: 'inherit' });

// Seed the database
console.log('🌱 Seeding database...');
execSync('npx prisma db seed', { stdio: 'inherit' });
} catch (err) {
console.error('❌ Failed to apply migrations or seed database:', err.message);
process.exit(1);
}
} else {
console.error('❌ Error checking database migration status:', error.message || error);
// We don't exit with 1 here to avoid blocking start if the DB is temporarily down or unreachable
}
}
}

checkAndMigrate();
Loading