-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Migrate.ts
178 lines (143 loc) · 5.42 KB
/
Migrate.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
import Debug from '@prisma/debug'
import { enginesVersion } from '@prisma/engines-version'
import { getGenerators, getGeneratorSuccessMessage, getSchemaPathSync } from '@prisma/sdk'
import chalk from 'chalk'
import fs from 'fs'
import logUpdate from 'log-update'
import path from 'path'
import { MigrateEngine } from './MigrateEngine'
import type { EngineArgs, EngineResults } from './types'
import { NoSchemaFoundError } from './utils/errors'
const debug = Debug('prisma:migrate')
const packageJson = eval(`require('../package.json')`)
export class Migrate {
public engine: MigrateEngine
private schemaPath?: string
public migrationsDirectoryPath?: string
constructor(schemaPath?: string, enabledPreviewFeatures?: string[]) {
// schemaPath and migrationsDirectoryPath is optionnal for primitives
// like migrate diff and db execute
if (schemaPath) {
this.schemaPath = this.getSchemaPath(schemaPath)
this.migrationsDirectoryPath = path.join(path.dirname(this.schemaPath), 'migrations')
this.engine = new MigrateEngine({
projectDir: path.dirname(this.schemaPath),
schemaPath: this.schemaPath,
enabledPreviewFeatures,
})
} else {
this.engine = new MigrateEngine({
projectDir: process.cwd(),
enabledPreviewFeatures,
})
}
}
public stop(): void {
this.engine.stop()
}
public getSchemaPath(schemaPathFromOptions?): string {
const schemaPath = getSchemaPathSync(schemaPathFromOptions)
if (!schemaPath) {
throw new NoSchemaFoundError()
}
return schemaPath
}
public getDatamodel(): string {
if (!this.schemaPath) throw new Error('this.schemaPath is undefined')
return fs.readFileSync(this.schemaPath, 'utf-8')
}
public reset(): Promise<void> {
return this.engine.reset()
}
public createMigration(params: EngineArgs.CreateMigrationInput): Promise<EngineResults.CreateMigrationOutput> {
return this.engine.createMigration(params)
}
public diagnoseMigrationHistory({
optInToShadowDatabase,
}: {
optInToShadowDatabase: boolean
}): Promise<EngineResults.DiagnoseMigrationHistoryOutput> {
if (!this.migrationsDirectoryPath) throw new Error('this.migrationsDirectoryPath is undefined')
return this.engine.diagnoseMigrationHistory({
migrationsDirectoryPath: this.migrationsDirectoryPath,
optInToShadowDatabase,
})
}
public listMigrationDirectories(): Promise<EngineResults.ListMigrationDirectoriesOutput> {
if (!this.migrationsDirectoryPath) throw new Error('this.migrationsDirectoryPath is undefined')
return this.engine.listMigrationDirectories({
migrationsDirectoryPath: this.migrationsDirectoryPath,
})
}
public devDiagnostic(): Promise<EngineResults.DevDiagnosticOutput> {
if (!this.migrationsDirectoryPath) throw new Error('this.migrationsDirectoryPath is undefined')
return this.engine.devDiagnostic({
migrationsDirectoryPath: this.migrationsDirectoryPath,
})
}
public async markMigrationApplied({ migrationId }: { migrationId: string }): Promise<void> {
if (!this.migrationsDirectoryPath) throw new Error('this.migrationsDirectoryPath is undefined')
return await this.engine.markMigrationApplied({
migrationsDirectoryPath: this.migrationsDirectoryPath,
migrationName: migrationId,
})
}
public markMigrationRolledBack({ migrationId }: { migrationId: string }): Promise<void> {
return this.engine.markMigrationRolledBack({
migrationName: migrationId,
})
}
public applyMigrations(): Promise<EngineResults.ApplyMigrationsOutput> {
if (!this.migrationsDirectoryPath) throw new Error('this.migrationsDirectoryPath is undefined')
return this.engine.applyMigrations({
migrationsDirectoryPath: this.migrationsDirectoryPath,
})
}
public evaluateDataLoss(): Promise<EngineResults.EvaluateDataLossOutput> {
if (!this.migrationsDirectoryPath) throw new Error('this.migrationsDirectoryPath is undefined')
const datamodel = this.getDatamodel()
return this.engine.evaluateDataLoss({
migrationsDirectoryPath: this.migrationsDirectoryPath,
prismaSchema: datamodel,
})
}
public async push({ force = false }: { force?: boolean }): Promise<EngineResults.SchemaPush> {
const datamodel = this.getDatamodel()
const { warnings, unexecutable, executedSteps } = await this.engine.schemaPush({
force,
schema: datamodel,
})
return {
executedSteps,
warnings,
unexecutable,
}
}
public async tryToRunGenerate(): Promise<void> {
if (!this.schemaPath) throw new Error('this.schemaPath is undefined')
const message: string[] = []
console.info() // empty line
logUpdate(`Running generate... ${chalk.dim('(Use --skip-generate to skip the generators)')}`)
const generators = await getGenerators({
schemaPath: this.schemaPath,
printDownloadProgress: true,
version: enginesVersion,
cliVersion: packageJson.version,
dataProxy: false,
})
for (const generator of generators) {
logUpdate(`Running generate... - ${generator.getPrettyName()}`)
const before = Date.now()
try {
await generator.generate()
const after = Date.now()
message.push(getGeneratorSuccessMessage(generator, after - before))
generator.stop()
} catch (e: any) {
message.push(`${e.message}`)
generator.stop()
}
}
logUpdate(message.join('\n'))
}
}