59 lines
1.8 KiB
JavaScript
59 lines
1.8 KiB
JavaScript
const { DataSource } = require('typeorm');
|
|
const path = require('path');
|
|
const { existsSync } = require('fs');
|
|
const applicationRoot = existsSync(path.join(__dirname, 'dist'))
|
|
? __dirname
|
|
: path.resolve(__dirname, '../..');
|
|
const { databaseTlsOptions } = require(
|
|
path.join(applicationRoot, 'dist/infrastructure/persistence/typeorm/database-tls')
|
|
);
|
|
|
|
const AppDataSource = new DataSource({
|
|
type: 'postgres',
|
|
host: process.env.DATABASE_HOST,
|
|
port: parseInt(process.env.DATABASE_PORT, 10),
|
|
username: process.env.DATABASE_USER,
|
|
password: process.env.DATABASE_PASSWORD,
|
|
database: process.env.DATABASE_NAME,
|
|
ssl: databaseTlsOptions(
|
|
process.env.DATABASE_SSL,
|
|
process.env.DATABASE_SSL_CA,
|
|
process.env.DATABASE_HOST
|
|
),
|
|
entities: [path.join(applicationRoot, 'dist/**/*.orm-entity.js')],
|
|
migrations: [
|
|
path.join(applicationRoot, 'dist/infrastructure/persistence/typeorm/migrations/*.js'),
|
|
],
|
|
synchronize: false,
|
|
logging: true,
|
|
});
|
|
|
|
console.log('🚀 Starting Xpeditis Backend Migration Script...');
|
|
console.log('📦 Initializing DataSource...');
|
|
|
|
AppDataSource.initialize()
|
|
.then(async () => {
|
|
console.log('✅ DataSource initialized successfully');
|
|
console.log('🔄 Running pending migrations...');
|
|
|
|
const migrations = await AppDataSource.runMigrations();
|
|
|
|
if (migrations.length === 0) {
|
|
console.log('✅ No pending migrations');
|
|
} else {
|
|
console.log(`✅ Successfully ran ${migrations.length} migration(s):`);
|
|
migrations.forEach(migration => {
|
|
console.log(` - ${migration.name}`);
|
|
});
|
|
}
|
|
|
|
await AppDataSource.destroy();
|
|
console.log('✅ Database migrations completed successfully');
|
|
process.exit(0);
|
|
})
|
|
.catch(error => {
|
|
console.error('❌ Error during migration:');
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|