-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathmigration-up-command.ts
More file actions
107 lines (96 loc) · 4.25 KB
/
Copy pathmigration-up-command.ts
File metadata and controls
107 lines (96 loc) · 4.25 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* 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';
import { Migration } from '../migration/migration.js';
import { Database } from '@deepkit/orm';
/**
* @description Executes pending migration files. Use migration:pending to see which are pending.
*/
@cli.controller('migration:up')
export class MigrationUpCommand 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,
/**
* @description Per default only the next migration is executed. With this flag all pending migrations are executed.
*/
all: 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);
const latestVersion = await migrationHandler.getLatestMigrationVersion();
const migrationToApply = migrations.filter(v => v.version > latestVersion);
if (!migrationToApply.length) {
this.logger.log('<green>All migrations executed</green>');
return;
}
if (all) {
while (migrationToApply.length) {
await this.executeNextMigration(database, migrationHandler, fake, migrationToApply);
}
} else {
await this.executeNextMigration(database, migrationHandler, fake, migrationToApply);
}
if (migrationToApply.length) {
this.logger.log(`<yellow>${migrationToApply.length} migration/s left. Run migration:up again to execute the next migration.</yellow>`);
} else {
this.logger.log('<green>All migrations executed</green>');
}
}
}
}
async executeNextMigration(database: Database<SQLDatabaseAdapter>, migrationHandler: SqlMigrationHandler, fake: boolean, migrationToApply: Migration[]) {
try {
const migration = migrationToApply.shift();
if (!migration) return;
const connection = await database.adapter.connectionPool.getConnection();
try {
this.logger.log(` Migration up <yellow>${migration.name}</yellow>`);
if (fake) {
this.logger.log(` Faking migration.`);
} else {
let i = 1;
for (const sql of migration.up()) {
this.logger.log(`<yellow> ${i++}. ${indent(4)(sql)}</yellow>`);
await connection.run(sql);
}
}
await migrationHandler.setLatestMigrationVersion(migration.version);
this.logger.log(`<green>Successfully migrated up to version ${migration.version}</green>`);
} finally {
connection.release();
}
} finally {
database.disconnect();
}
}
}