-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathmigration-down-command.ts
More file actions
82 lines (74 loc) · 3.21 KB
/
Copy pathmigration-down-command.ts
File metadata and controls
82 lines (74 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Deepkit Framework
* Copyright (C) 2021 Deepkit UG, Marc J. Schmidt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the MIT License.
*
* You should have received a copy of the MIT License along with this program.
*/
import { indent } from '@deepkit/core';
import { cli, Flag } from '@deepkit/app';
import { LoggerInterface } from '@deepkit/logger';
import { MigrationProvider } from '../migration/migration-provider.js';
import { SQLDatabaseAdapter, SqlMigrationHandler } from '../sql-adapter.js';
import { BaseCommand } from './base-command.js';
/**
* @description Executes down migration, reverting old migration files.
*/
@cli.controller('migration:down')
export class MigrationDownCommand extends BaseCommand {
constructor(
protected logger: LoggerInterface,
protected provider: MigrationProvider,
) {
super();
}
async execute(
/**
* @description Limit migrations to a specific database.
*/
database?: string & Flag,
/**
* @description Sets the migration version without executing actual SQL commands.
*/
fake: boolean & Flag = false,
): Promise<void> {
if (this.migrationDir) this.provider.setMigrationDir(this.migrationDir);
if (this.path) await this.provider.addDatabase(this.path);
const migrationsPerDatabase = await this.provider.getMigrationsPerDatabase(database);
for (const [database, migrations] of migrationsPerDatabase.entries()) {
this.logger.log(`Execute migrations for <yellow>${database.name}</yellow>`);
if (database.adapter instanceof SQLDatabaseAdapter) {
const migrationHandler = new SqlMigrationHandler(database);
try {
const latestVersion = await migrationHandler.getLatestMigrationVersion();
const migration = migrations.find(v => v.version === latestVersion);
if (!migration) {
this.logger.log('<red>No migration found to execute</red>');
return;
}
const connection = await database.adapter.connectionPool.getConnection();
try {
this.logger.log(` Migration down <yellow>${migration.name}</yellow>`);
if (fake) {
this.logger.log(` Faking migration.`);
} else {
let i = 1;
for (const sql of migration.down()) {
this.logger.log(`<yellow> ${i++}. ${indent(4)(sql)}</yellow>`);
await connection.run(sql);
}
}
await migrationHandler.removeMigrationVersion(migration.version);
this.logger.log(`<green>Successfully migrated down to version ${migration.version}</green>`);
} finally {
connection.release();
}
} finally {
database.disconnect();
}
}
}
}
}