modified: backend/src/config/database.ts
modified: backend/src/server.ts
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
import { Pool } from 'pg';
|
import { Pool } from 'pg';
|
||||||
import { config } from './environment';
|
import { config } from './environment';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
// Main database pool for KIS API Builder metadata
|
// Main database pool for KIS API Builder metadata
|
||||||
export const mainPool = new Pool({
|
export const mainPool = new Pool({
|
||||||
@@ -28,3 +30,24 @@ export const initializeDatabase = async () => {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const runMigrations = async () => {
|
||||||
|
console.log('🔄 Running migrations...');
|
||||||
|
try {
|
||||||
|
const migrationsDir = path.join(__dirname, '../migrations');
|
||||||
|
const files = fs.readdirSync(migrationsDir)
|
||||||
|
.filter(f => f.endsWith('.sql'))
|
||||||
|
.sort();
|
||||||
|
|
||||||
|
for (const file of files) {
|
||||||
|
console.log(` 📄 ${file}`);
|
||||||
|
const sql = fs.readFileSync(path.join(migrationsDir, file), 'utf-8');
|
||||||
|
await mainPool.query(sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('✅ Migrations completed');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Migration failed:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import helmet from 'helmet';
|
|||||||
import swaggerUi from 'swagger-ui-express';
|
import swaggerUi from 'swagger-ui-express';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { config } from './config/environment';
|
import { config } from './config/environment';
|
||||||
import { initializeDatabase } from './config/database';
|
import { initializeDatabase, runMigrations } from './config/database';
|
||||||
import { generateDynamicSwagger } from './config/dynamicSwagger';
|
import { generateDynamicSwagger } from './config/dynamicSwagger';
|
||||||
import { databasePoolManager } from './services/DatabasePoolManager';
|
import { databasePoolManager } from './services/DatabasePoolManager';
|
||||||
|
|
||||||
@@ -154,6 +154,7 @@ app.use((err: any, _req: Request, res: Response, _next: any) => {
|
|||||||
const startServer = async () => {
|
const startServer = async () => {
|
||||||
try {
|
try {
|
||||||
await initializeDatabase();
|
await initializeDatabase();
|
||||||
|
await runMigrations();
|
||||||
await databasePoolManager.initialize();
|
await databasePoolManager.initialize();
|
||||||
|
|
||||||
app.listen(config.port, () => {
|
app.listen(config.port, () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user