-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
MigrateStatus.test.ts
252 lines (199 loc) · 9.36 KB
/
MigrateStatus.test.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import fs from 'fs-jetpack'
import path from 'path'
import { MigrateStatus } from '../commands/MigrateStatus'
import { consoleContext, Context } from './__helpers__/context'
import { tearDownMysql } from '../utils/setupMysql'
import {
SetupParams,
setupPostgres,
tearDownPostgres,
} from '../utils/setupPostgres'
const ctx = Context.new().add(consoleContext()).assemble()
process.env.GITHUB_ACTIONS = '1'
describe('common', () => {
it('should fail if no schema file', async () => {
ctx.fixture('empty')
const result = MigrateStatus.new().parse(['--early-access-feature'])
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`
Could not find a schema.prisma file that is required for this command.
You can either provide it with --schema, set it as \`prisma.schema\` in your package.json or put it into the default location ./prisma/schema.prisma https://pris.ly/d/prisma-schema-location
`)
})
it('should fail if no flag', async () => {
ctx.fixture('empty')
const result = MigrateStatus.new().parse([])
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`
This feature is currently in Early Access. There may be bugs and it's not recommended to use it in production environments.
Please provide the --early-access-feature flag to use this command.
`)
})
})
describe('sqlite', () => {
it('should fail if no sqlite db - empty schema', async () => {
ctx.fixture('schema-only-sqlite')
const result = MigrateStatus.new().parse([
'--schema=./prisma/empty.prisma',
'--early-access-feature',
])
await expect(result).rejects.toMatchInlineSnapshot(
`P1003: SQLite database file doesn't exist`,
)
expect(
ctx.mocked['console.info'].mock.calls.join('\n'),
).toMatchInlineSnapshot(`Prisma schema loaded from prisma/empty.prisma`)
expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot()
expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot()
})
it('existing-db-1-failed-migration', async () => {
ctx.fixture('existing-db-1-failed-migration')
const result = MigrateStatus.new().parse(['--early-access-feature'])
await expect(result).resolves.toMatchInlineSnapshot(`
The failed migration(s) can be marked as rolled back or applied:
- If you rolled back the migration(s) manually:
prisma migrate resolve --rolledback "20201231000000_failed" --early-access-feature
- If you fixed the database manually (hotfix):
prisma migrate resolve --applied "20201231000000_failed" --early-access-feature
Read more in our docs: https://pris.ly/migrate-resolve
`)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Status
1 migration found in prisma/migrations
Following migration have failed:
20201231000000_failed
During development if the failed migration(s) have not been deployed to a production database you can then fix the migration(s) and run prisma migrate dev --early-access-feature.
`)
expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot()
expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot()
})
it('baseline-sqlite', async () => {
ctx.fixture('baseline-sqlite')
const result = MigrateStatus.new().parse(['--early-access-feature'])
await expect(result).resolves.toMatchInlineSnapshot(`
The current database is not managed by Prisma Migrate.
If you want to keep the current database structure and data and create new migrations, baseline this database with the migration "20201231000000_":
prisma migrate resolve --applied "20201231000000_" --early-access-feature
`)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Status
1 migration found in prisma/migrations
Following migration have not yet been applied:
20201231000000_
To apply migrations in development run prisma migrate dev --early-access-feature.
To apply migrations in production run prisma migrate deploy --early-access-feature.
Read more in our docs: https://pris.ly/migrate-deploy
`)
expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot()
expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot()
})
it('existing-db-1-migration', async () => {
ctx.fixture('existing-db-1-migration')
const result = MigrateStatus.new().parse(['--early-access-feature'])
await expect(result).resolves.toMatchInlineSnapshot(
`Database schema is up to date!`,
)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Status
1 migration found in prisma/migrations
`)
expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot()
expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot()
})
it('existing-db-1-migration-conflict', async () => {
ctx.fixture('existing-db-1-migration-conflict')
const result = MigrateStatus.new().parse(['--early-access-feature'])
await expect(result).resolves.toMatchInlineSnapshot(`
The current database is not managed by Prisma Migrate.
If you want to keep the current database structure and data and create new migrations, baseline this database with the migration "20201231000000_init":
prisma migrate resolve --applied "20201231000000_init" --early-access-feature
`)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Status
1 migration found in prisma/migrations
Following migration have not yet been applied:
20201231000000_init
To apply migrations in development run prisma migrate dev --early-access-feature.
To apply migrations in production run prisma migrate deploy --early-access-feature.
Read more in our docs: https://pris.ly/migrate-deploy
`)
expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot()
expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot()
})
it('existing-db-brownfield', async () => {
ctx.fixture('existing-db-brownfield')
const result = MigrateStatus.new().parse(['--early-access-feature'])
await expect(result).rejects.toMatchInlineSnapshot(
`Check init flow with introspect + SQL schema dump (TODO docs)`,
)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Status
No migration found in prisma/migrations
`)
expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot()
expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot()
})
it('existing-db-warnings', async () => {
ctx.fixture('existing-db-warnings')
const result = MigrateStatus.new().parse(['--early-access-feature'])
await expect(result).rejects.toMatchInlineSnapshot(
`Check init flow with introspect + SQL schema dump (TODO docs)`,
)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Status
No migration found in prisma/migrations
`)
expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot()
expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot()
})
it('old-migrate', async () => {
ctx.fixture('old-migrate')
const result = MigrateStatus.new().parse(['--early-access-feature'])
await expect(result).rejects.toMatchInlineSnapshot(`
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.
`)
expect(
ctx.mocked['console.info'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot()
expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot()
})
it('initialized-sqlite', async () => {
ctx.fixture('initialized-sqlite')
const result = MigrateStatus.new().parse(['--early-access-feature'])
await expect(result).rejects.toMatchInlineSnapshot(
`P1003: SQLite database file doesn't exist`,
)
expect(
ctx.mocked['console.info'].mock.calls.join('\n'),
).toMatchInlineSnapshot(`Prisma schema loaded from prisma/schema.prisma`)
expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot()
expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot()
})
it('reset', async () => {
ctx.fixture('reset')
const result = MigrateStatus.new().parse(['--early-access-feature'])
await expect(result).resolves.toMatchInlineSnapshot(
`Database schema is up to date!`,
)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Status
1 migration found in prisma/migrations
`)
expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot()
expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot()
})
})