-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
MigrateReset.ts
190 lines (162 loc) · 5.17 KB
/
MigrateReset.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import {
arg,
Command,
format,
getCommandWithExecutor,
getSchemaPath,
HelpError,
isError,
isCi,
} from '@prisma/sdk'
import chalk from 'chalk'
import path from 'path'
import prompt from 'prompts'
import { Migrate } from '../Migrate'
import {
EarlyAcessFlagError,
ExperimentalFlagWithNewMigrateError,
} from '../utils/flagErrors'
import { printFilesFromMigrationIds } from '../utils/printFiles'
import { isOldMigrate } from '../utils/detectOldMigrate'
export class MigrateReset implements Command {
public static new(): MigrateReset {
return new MigrateReset()
}
private static help = format(`
Reset your database and apply all migrations
${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 reset [options] --early-access-feature
${chalk.bold('Options')}
-h, --help Display this help message
--schema Custom path to your Prisma schema
-f, --force Skip the confirmation prompt
--skip-generate Skip generate
${chalk.bold('Examples')}
Reset your database, structure and data will be lost
${chalk.dim('$')} prisma migrate reset --early-access-feature
Specify a schema
${chalk.dim(
'$',
)} prisma migrate reset --schema=./schema.prisma --early-access-feature
`)
public async parse(argv: string[]): Promise<string | Error> {
const args = arg(argv, {
'--help': Boolean,
'-h': '--help',
'--force': Boolean,
'-f': '--force',
'--skip-generate': Boolean,
'--experimental': Boolean,
'--early-access-feature': Boolean,
'--schema': String,
'--telemetry-information': String,
})
if (isError(args)) {
return this.help(args.message)
}
if (args['--help']) {
return this.help()
}
if (args['--experimental']) {
throw new ExperimentalFlagWithNewMigrateError()
}
if (!args['--early-access-feature']) {
throw new EarlyAcessFlagError()
}
const schemaPath = await getSchemaPath(args['--schema'])
if (!schemaPath) {
throw new Error(
`Could not find a ${chalk.bold(
'schema.prisma',
)} file that is required for this command.\nYou can either provide it with ${chalk.greenBright(
'--schema',
)}, set it as \`prisma.schema\` in your package.json or put it into the default location ${chalk.greenBright(
'./prisma/schema.prisma',
)} https://pris.ly/d/prisma-schema-location`,
)
} else {
console.info(
chalk.dim(
`Prisma schema loaded from ${path.relative(
process.cwd(),
schemaPath,
)}`,
),
)
const migrationDirPath = path.join(path.dirname(schemaPath), 'migrations')
if (isOldMigrate(migrationDirPath)) {
// Maybe add link to docs?
throw Error(
`The migrations folder contains migrations files from an older version of Prisma Migrate which is not compatible.
Delete the current migrations folder to continue and read the documentation for how to upgrade / baseline.`,
)
}
}
if (!args['--force']) {
// We use prompts.inject() for testing in our CI
if (isCi() && Boolean((prompt as any)._injected?.length) === false) {
throw Error(
`Use the --force flag to use the reset command in an unnattended environment like ${chalk.bold.greenBright(
getCommandWithExecutor(
'prisma migrate reset --force --early-access-feature',
),
)}`,
)
}
console.info() // empty line
const confirmation = await prompt({
type: 'confirm',
name: 'value',
message: `Are you sure you want to reset your database? ${chalk.red(
'All data will be lost',
)}.`,
})
if (!confirmation.value) {
console.info('Reset cancelled.')
process.exit(0)
// For snapshot test, because exit() is mocked
return ``
}
}
const migrate = new Migrate(schemaPath)
await migrate.reset()
const { appliedMigrationNames: migrationIds } = await migrate.applyOnly()
migrate.stop()
if (migrationIds.length === 0) {
console.info(
`\n${chalk.green(
'Database reset successful',
)} - Prisma Migrate didn't find unapplied migrations.`,
)
} else {
console.info(
`\n${chalk.green(
'Database reset successful',
)} - Prisma Migrate applied the following migration(s):\n\n${chalk(
printFilesFromMigrationIds('migrations', migrationIds, {
'migration.sql': '',
}),
)}`,
)
// Run if not skipped
if (!process.env.MIGRATE_SKIP_GENERATE && !args['--skip-generate']) {
await migrate.tryToRunGenerate()
}
}
return ``
}
public help(error?: string): string | HelpError {
if (error) {
return new HelpError(
`\n${chalk.bold.red(`!`)} ${error}\n${MigrateReset.help}`,
)
}
return MigrateReset.help
}
}