-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
run.ts
139 lines (121 loc) · 3.42 KB
/
run.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { flags } from '@adonisjs/core/ace'
import MigrationsBase from './_base.js'
import { MigrationRunner } from '../../src/migration/runner.js'
import { CommandOptions } from '@adonisjs/core/types/ace'
/**
* The command is meant to migrate the database by executing migrations
* in `up` direction.
*/
export default class Migrate extends MigrationsBase {
static commandName = 'migration:run'
static description = 'Migrate database by running pending migrations'
static options: CommandOptions = {
startApp: true,
}
private migrator?: MigrationRunner
/**
* Custom connection for running migrations.
*/
@flags.string({ description: 'Define a custom database connection', alias: 'c' })
declare connection: string
/**
* Force run migrations in production
*/
@flags.boolean({ description: 'Explicitly force to run migrations in production' })
declare force: boolean
/**
* Perform dry run
*/
@flags.boolean({ description: 'Do not run actual queries. Instead view the SQL output' })
declare dryRun: boolean
/**
* Display migrations result in one compact single-line output
*/
@flags.boolean({ description: 'A compact single-line output' })
compactOutput: boolean = false
/**
* Disable advisory locks
*/
@flags.boolean({ description: 'Disable locks acquired to run migrations safely' })
declare disableLocks: boolean
/**
* Instantiating the migrator instance
*/
private async instantiateMigrator() {
const db = await this.app.container.make('lucid.db')
this.migrator = new MigrationRunner(db, this.app, {
direction: 'up',
connectionName: this.connection,
dryRun: this.dryRun,
disableLocks: this.disableLocks,
})
}
/**
* Run as a subcommand. Never close database connections or exit
* process inside this method
*/
private async runAsSubCommand() {
const db = await this.app.container.make('lucid.db')
this.connection = this.connection || db.primaryConnectionName
/**
* Continue with migrations when not in prod or force flag
* is passed
*/
let continueMigrations = !this.app.inProduction || this.force
if (!continueMigrations) {
continueMigrations = await this.takeProductionConsent()
}
/**
* Do not continue when in prod and the prompt was cancelled
*/
if (!continueMigrations) {
return
}
/**
* Invalid database connection
*/
if (!db.manager.has(this.connection)) {
this.printNotAValidConnection(this.connection)
this.exitCode = 1
return
}
await this.instantiateMigrator()
await this.runMigrations(this.migrator!, this.connection)
}
/**
* Branching out, so that if required we can implement
* "runAsMain" separately from "runAsSubCommand".
*
* For now, they both are the same
*/
private async runAsMain() {
await this.runAsSubCommand()
}
/**
* Handle command
*/
async run(): Promise<void> {
if (this.isMain) {
await this.runAsMain()
} else {
await this.runAsSubCommand()
}
}
/**
* Lifecycle method invoked by ace after the "run"
* method.
*/
async completed() {
if (this.migrator && this.isMain) {
await this.migrator.close()
}
}
}