миграция
This commit is contained in:
@@ -2,6 +2,7 @@ const express = require('express');
|
||||
const cors = require('cors');
|
||||
const dotenv = require('dotenv');
|
||||
const connectDB = require('./config/db');
|
||||
const { runMigrations } = require('./scripts/run-migrations');
|
||||
|
||||
// Загрузить переменные окружения
|
||||
dotenv.config();
|
||||
@@ -29,11 +30,32 @@ const homeRoutes = require('./routes/home');
|
||||
|
||||
const app = express();
|
||||
|
||||
// Подключить MongoDB при инициализации
|
||||
// Подключить MongoDB и запустить миграции при инициализации
|
||||
let dbConnected = false;
|
||||
connectDB().then(() => {
|
||||
dbConnected = true;
|
||||
});
|
||||
let migrationsCompleted = false;
|
||||
|
||||
const initializeApp = async () => {
|
||||
try {
|
||||
await connectDB().then(() => {
|
||||
dbConnected = true;
|
||||
});
|
||||
|
||||
// Запустить миграции после успешного подключения
|
||||
if (dbConnected) {
|
||||
try {
|
||||
await runMigrations();
|
||||
migrationsCompleted = true;
|
||||
} catch (migrationError) {
|
||||
console.error('⚠️ Migrations failed but app will continue:', migrationError.message);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error during app initialization:', err);
|
||||
}
|
||||
};
|
||||
|
||||
// Запустить инициализацию
|
||||
initializeApp();
|
||||
|
||||
// Middleware
|
||||
app.use(cors());
|
||||
@@ -68,6 +90,7 @@ app.get('/health', (req, res) => {
|
||||
status: 'ok',
|
||||
api: 'running',
|
||||
database: dbConnected ? 'mongodb' : 'mock',
|
||||
migrations: migrationsCompleted ? 'completed' : 'pending',
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user