-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
MigrateCommand.ts
116 lines (91 loc) · 3.17 KB
/
MigrateCommand.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
import {
arg,
Command,
Commands,
format,
HelpError,
isError,
unknownCommand,
} from '@prisma/sdk'
import Debug from '@prisma/debug'
import chalk from 'chalk'
import { ExperimentalFlagWithNewMigrateError } from '../utils/flagErrors'
export class MigrateCommand implements Command {
public static new(cmds: Commands): MigrateCommand {
return new MigrateCommand(cmds)
}
private static help = format(`${
process.platform === 'win32' ? '' : chalk.bold('🏋️ ')
}Migrate your database with confidence
${chalk.bold.yellow('WARNING')} ${chalk.bold(
"Prisma's migration functionality is currently in Early Access.",
)}
${chalk.dim(
'When using any of the commands below you need to explicitly opt-in via the --early-access-feature flag.',
)}
${chalk.bold('Usage')}
${chalk.dim('$')} prisma migrate [command] [options] --early-access-feature
${chalk.bold('Commands for development')}
dev Create migrations from your Prisma schema, apply them to the database,
generate artifacts (Prisma Client)
reset Reset your database and apply all migrations
${chalk.bold('Commands for staging/production')}
deploy Apply migrations to update the database schema
resolve Resolve issues with database migrations (baseline, failed migration, hotfix)
${chalk.bold('Options')}
-h, --help Display this help message
--schema Custom path to your Prisma schema
${chalk.bold('Examples')}
Specify a schema
${chalk.dim(
'$',
)} prisma migrate --early-access-feature dev --schema=./schema.prisma
Automatically create a migration and apply it if there is a schema change
${chalk.dim('$')} prisma migrate dev --early-access-feature
Reset your database
${chalk.dim('$')} prisma migrate reset --early-access-feature
Deploy the migrations to your database
${chalk.dim('$')} prisma migrate deploy --early-access-feature
Mark a migration as applied
${chalk.dim(
'$',
)} prisma migrate resolve --applied=20201231000000_add_users_table --early-access-feature
`)
private constructor(private readonly cmds: Commands) {}
public async parse(argv: string[]): Promise<string | Error> {
const args = arg(argv, {
'--help': Boolean,
'-h': '--help',
'--experimental': Boolean,
'--early-access-feature': Boolean,
'--telemetry-information': String,
})
if (isError(args)) {
return this.help(args.message)
}
// display help for help flag or no subcommand
if (args._.length === 0 || args['--help']) {
return this.help()
}
if (args['--experimental']) {
throw new ExperimentalFlagWithNewMigrateError()
}
// check if we have that subcommand
const cmd = this.cmds[args._[0]]
if (cmd) {
const argsForCmd = args['--early-access-feature']
? [...args._.slice(1), `--early-access-feature`]
: args._.slice(1)
return cmd.parse(argsForCmd)
}
return unknownCommand(MigrateCommand.help, args._[0])
}
public help(error?: string): string | HelpError {
if (error) {
return new HelpError(
`\n${chalk.bold.red(`!`)} ${error}\n${MigrateCommand.help}`,
)
}
return MigrateCommand.help
}
}