Fix database migrations - run from app container instead of volume mount
- Removed migrations volume mount from docker-compose.yml - Added automatic migration runner in server.js on startup - Migrations now run from files built into Docker image - Fixes 'relation does not exist' errors
This commit is contained in:
@@ -11,7 +11,6 @@ services:
|
|||||||
POSTGRES_PASSWORD: ${DB_PASSWORD:-changeme123}
|
POSTGRES_PASSWORD: ${DB_PASSWORD:-changeme123}
|
||||||
volumes:
|
volumes:
|
||||||
- postgres_data:/var/lib/postgresql/data
|
- postgres_data:/var/lib/postgresql/data
|
||||||
- ./migrations:/docker-entrypoint-initdb.d
|
|
||||||
ports:
|
ports:
|
||||||
- "5432:5432"
|
- "5432:5432"
|
||||||
healthcheck:
|
healthcheck:
|
||||||
|
|||||||
33
server.js
33
server.js
@@ -5,6 +5,7 @@ const { Pool } = require('pg');
|
|||||||
const multer = require('multer');
|
const multer = require('multer');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const cookieParser = require('cookie-parser');
|
const cookieParser = require('cookie-parser');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = process.env.PORT || 3000;
|
const PORT = process.env.PORT || 3000;
|
||||||
@@ -21,15 +22,41 @@ const pool = new Pool({
|
|||||||
connectionTimeoutMillis: 2000,
|
connectionTimeoutMillis: 2000,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Test database connection (non-blocking)
|
// Run database migrations
|
||||||
|
async function runMigrations() {
|
||||||
|
try {
|
||||||
|
console.log('Running database migrations...');
|
||||||
|
const migrationsDir = path.join(__dirname, 'migrations');
|
||||||
|
const migrationFiles = fs.readdirSync(migrationsDir).sort();
|
||||||
|
|
||||||
|
for (const file of migrationFiles) {
|
||||||
|
if (file.endsWith('.sql')) {
|
||||||
|
console.log(`Running migration: ${file}`);
|
||||||
|
const migrationSQL = fs.readFileSync(path.join(migrationsDir, file), 'utf8');
|
||||||
|
await pool.query(migrationSQL);
|
||||||
|
console.log(`✓ Migration ${file} completed`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('All migrations completed successfully');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Migration error:', error.message);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test database connection and run migrations
|
||||||
pool.query('SELECT NOW()')
|
pool.query('SELECT NOW()')
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
console.log('Database connected successfully at:', res.rows[0].now);
|
console.log('Database connected successfully at:', res.rows[0].now);
|
||||||
|
return runMigrations();
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
console.log('Database initialization complete');
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error('Database connection error:', err.message);
|
console.error('Database initialization error:', err.message);
|
||||||
console.error('Application will continue but database operations will fail');
|
console.error('Application will continue but database operations will fail');
|
||||||
console.error('Please ensure PostgreSQL is running and accessible');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Middleware
|
// Middleware
|
||||||
|
|||||||
Reference in New Issue
Block a user