50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import 'reflect-metadata';
|
|
import { migrationDataSource } from './data-source';
|
|
|
|
type MigrationCommand = 'run' | 'show' | 'revert';
|
|
|
|
function readCommand(): MigrationCommand {
|
|
const command = process.argv[2];
|
|
if (command === 'run' || command === 'show' || command === 'revert') {
|
|
return command;
|
|
}
|
|
throw new Error('Expected migration command: run, show or revert');
|
|
}
|
|
|
|
async function main(): Promise<void> {
|
|
const command = readCommand();
|
|
await migrationDataSource.initialize();
|
|
|
|
try {
|
|
if (command === 'run') {
|
|
const migrations = await migrationDataSource.runMigrations({
|
|
transaction: 'each',
|
|
});
|
|
console.log(`Applied migrations: ${migrations.length}`);
|
|
for (const migration of migrations) console.log(`- ${migration.name}`);
|
|
return;
|
|
}
|
|
|
|
if (command === 'revert') {
|
|
await migrationDataSource.undoLastMigration({ transaction: 'each' });
|
|
console.log('Last migration reverted');
|
|
return;
|
|
}
|
|
|
|
const hasPendingMigrations = await migrationDataSource.showMigrations();
|
|
console.log(
|
|
hasPendingMigrations
|
|
? 'Pending migrations: yes'
|
|
: 'Pending migrations: no',
|
|
);
|
|
} finally {
|
|
await migrationDataSource.destroy();
|
|
}
|
|
}
|
|
|
|
main().catch((error: unknown) => {
|
|
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
console.error(`Migration command failed: ${message}`);
|
|
process.exit(1);
|
|
});
|