-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
MigrationRunCommand.ts
79 lines (71 loc) · 2.44 KB
/
MigrationRunCommand.ts
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
import path from "path"
import * as process from "process"
import * as yargs from "yargs"
import { PlatformTools } from "../platform/PlatformTools"
import { DataSource } from "../data-source"
import { CommandUtils } from "./CommandUtils"
/**
* Runs migration command.
*/
export class MigrationRunCommand implements yargs.CommandModule {
command = "migration:run"
describe = "Runs all pending migrations."
builder(args: yargs.Argv) {
return args
.option("dataSource", {
alias: "d",
describe:
"Path to the file where your DataSource instance is defined.",
demandOption: true,
})
.option("transaction", {
alias: "t",
default: "default",
describe:
"Indicates if transaction should be used or not for migration run. Enabled by default.",
})
}
async handler(args: yargs.Arguments) {
let dataSource: DataSource | undefined = undefined
try {
dataSource = await CommandUtils.loadDataSource(
path.resolve(process.cwd(), args.dataSource as string),
)
dataSource.setOptions({
subscribers: [],
synchronize: false,
migrationsRun: false,
dropSchema: false,
logging: ["query", "error", "schema"],
})
await dataSource.initialize()
const options = {
transaction:
dataSource.options.migrationsTransactionMode ??
("all" as "all" | "none" | "each"),
}
switch (args.t) {
case "all":
options.transaction = "all"
break
case "none":
case "false":
options.transaction = "none"
break
case "each":
options.transaction = "each"
break
default:
// noop
}
await dataSource.runMigrations(options)
await dataSource.destroy()
// exit process if no errors
process.exit(0)
} catch (err) {
if (dataSource) await dataSource.destroy()
PlatformTools.logCmdErr("Error during migration run:", err)
process.exit(1)
}
}
}