import { LibraryUncoreCentralApplication } from './application'; import { CLIENT_ID, CLIENT_SECRET, DB_VERSION } from './constant'; import { UncoreCentralDataSource } from './datasources/uncore-central.datasource'; import { BlockedUserRepository, ProviderRepository } from './repositories'; import { MigrationRepository } from './repositories/migration.repository'; export async function migrate(args: string[]) { const existingSchema = args.includes('--rebuild') ? 'drop' : 'alter'; console.log('Migrating schemas (%s existing schema)', existingSchema); const app = new LibraryUncoreCentralApplication(); await app.boot(); //await app.migrateSchema({existingSchema}); // Connectors usually keep a pool of opened connections, // this keeps the process running even after all work is done. // We need to exit explicitly. //process.exit(0); const ds: UncoreCentralDataSource = await app.get('datasources.uncore_central'); const migrationRepo = await app.getRepository(MigrationRepository); const providerRepo = await app.getRepository(ProviderRepository); const blockedUserRepo = await app.getRepository(BlockedUserRepository); await ds.autoupdate(); for (let version = 0; version <= DB_VERSION; version++) { if (version == 0 && !await migrationRepo.getMigrationByVersion(version)) { const data = await providerRepo.findOne({ where: { client_id: CLIENT_ID, client_secret: CLIENT_SECRET, } }) if (!data) { await providerRepo.create({ client_id: CLIENT_ID, client_secret: CLIENT_SECRET, is_deleted: false, name: 'internal' }) } await migrationRepo.create({ name: "INIT", version: version }) } } await ds.disconnect(); await app.stop(); } if (require.main === module) { const args = process.argv.slice(2); migrate(args).catch(err => { console.error('Cannot migrate database schema', err); process.exit(1); }); }