diff --git a/server/index.js b/server/index.js index 7d9ea70..3bbc57a 100644 --- a/server/index.js +++ b/server/index.js @@ -3,6 +3,7 @@ const cors = require('cors'); const helmet = require('helmet'); const path = require('path'); const fs = require('fs'); +const { runMigrations } = require('./db/migrations'); const { initDatabase } = require('./db/init'); const { logStartupBanner } = require('./utils/startupLog'); @@ -18,6 +19,16 @@ const DATA_DIR = process.env.DATA_DIR || path.join(__dirname, '../data'); if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); }); +// Run migrations BEFORE initializing the DB connection used by routes +const DB_PATH = process.env.DB_PATH || path.join(__dirname, '../data/breedr.db'); +console.log('Running database migrations...'); +try { + runMigrations(DB_PATH); +} catch (err) { + console.error('Migration failed — aborting startup:', err.message); + process.exit(1); +} + // Init DB (path is managed internally by db/init.js) console.log('Initializing database...'); initDatabase(); @@ -49,7 +60,7 @@ app.use('/api/settings', require('./routes/settings')); if (process.env.NODE_ENV === 'production') { const clientBuild = path.join(__dirname, '../client/dist'); app.use(express.static(clientBuild)); - app.get(/^(?!\/(?: api|static|uploads)\/).*$/, (_req, res) => { + app.get(/^(?!\/(?:api|static|uploads)\/).*$/, (_req, res) => { res.sendFile(path.join(clientBuild, 'index.html')); }); } @@ -64,7 +75,6 @@ app.use((err, _req, res, _next) => { }); app.listen(PORT, '0.0.0.0', () => { - // Display comprehensive startup log logStartupBanner({ appName: 'BREEDR', port: PORT, @@ -76,4 +86,4 @@ app.listen(PORT, '0.0.0.0', () => { }); }); -module.exports = app; \ No newline at end of file +module.exports = app;