-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
DbPush.test.ts
291 lines (223 loc) · 10.2 KB
/
DbPush.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
process.env.GITHUB_ACTIONS = '1'
process.env.PRISMA_MIGRATE_SKIP_GENERATE = '1'
import prompt from 'prompts'
import { DbPush } from '../commands/DbPush'
import { consoleContext, Context } from './__helpers__/context'
const ctx = Context.new().add(consoleContext()).assemble()
describe('push', () => {
it('--preview-feature flag is not required anymore', async () => {
ctx.fixture('empty')
const result = DbPush.new().parse(['--preview-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
`)
expect(ctx.mocked['console.warn'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
prisma:warn Prisma "db push" was in Preview and is now Generally Available.
You can now remove the --preview-feature flag.
`)
})
it('should fail if no schema file', async () => {
ctx.fixture('empty')
const result = DbPush.new().parse([])
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 nativeTypes VarChar on sqlite', async () => {
ctx.fixture('nativeTypes-sqlite')
const result = DbPush.new().parse([])
await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`
P1012
error: Native type VarChar is not supported for sqlite connector.
--> schema.prisma:12
|
11 | id Int @id
12 | name String @db.VarChar(100)
|
`)
})
it('--force flag renamed', async () => {
ctx.fixture('reset')
const result = DbPush.new().parse(['--force'])
await expect(result).rejects.toMatchInlineSnapshot(
`The --force flag was renamed to --accept-data-loss in 2.17.0, use prisma db push --accept-data-loss`,
)
expect(
ctx.mocked['console.info'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
})
it('already in sync', async () => {
ctx.fixture('reset')
const result = DbPush.new().parse([])
await expect(result).resolves.toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
🚀 Your database is now in sync with your schema. Done in XXms
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
})
it('missing db', async () => {
ctx.fixture('reset')
ctx.fs.remove('prisma/dev.db')
const result = DbPush.new().parse([])
await expect(result).resolves.toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
SQLite database dev.db created at file:dev.db
🚀 Your database is now in sync with your schema. Done in XXms
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
})
it('should ask for --accept-data-loss if not provided in CI', async () => {
ctx.fixture('existing-db-warnings')
const result = DbPush.new().parse([])
await expect(result).rejects.toMatchInlineSnapshot(
`Use the --accept-data-loss flag to ignore the data loss warnings like prisma db push --accept-data-loss`,
)
expect(
ctx.mocked['console.log'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
})
it('dataloss warnings accepted (prompt)', async () => {
ctx.fixture('existing-db-warnings')
prompt.inject(['y'])
const result = DbPush.new().parse([])
await expect(result).resolves.toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
⚠️ There might be data loss when applying the changes:
• You are about to drop the \`Blog\` table, which is not empty (1 rows).
🚀 Your database is now in sync with your schema. Done in XXms
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
})
it('dataloss warnings cancelled (prompt)', async () => {
ctx.fixture('existing-db-warnings')
const mockExit = jest.spyOn(process, 'exit').mockImplementation()
prompt.inject([new Error()]) // simulate user cancellation
const result = DbPush.new().parse([])
await expect(result).resolves.toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
⚠️ There might be data loss when applying the changes:
• You are about to drop the \`Blog\` table, which is not empty (1 rows).
Push cancelled.
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(mockExit).toBeCalledWith(0)
})
it('--accept-data-loss flag', async () => {
ctx.fixture('existing-db-warnings')
const result = DbPush.new().parse(['--accept-data-loss'])
await expect(result).resolves.toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
⚠️ There might be data loss when applying the changes:
• You are about to drop the \`Blog\` table, which is not empty (1 rows).
🚀 Your database is now in sync with your schema. Done in XXms
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
})
it('unexecutable - drop accepted (prompt)', async () => {
ctx.fixture('existing-db-1-unexecutable-schema-change')
prompt.inject(['y'])
const result = DbPush.new().parse([])
const sqliteDbSizeBefore = ctx.fs.inspect('prisma/dev.db')!.size
await expect(result).resolves.toMatchInlineSnapshot(``)
const sqliteDbSizeAfter = ctx.fs.inspect('prisma/dev.db')!.size
expect(sqliteDbSizeBefore).toBeGreaterThan(10000)
expect(sqliteDbSizeAfter).toBeGreaterThan(10000)
expect(sqliteDbSizeAfter).toBeLessThan(sqliteDbSizeBefore)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
⚠️ We found changes that cannot be executed:
• Made the column \`fullname\` on table \`Blog\` required, but there are 1 existing NULL values.
The SQLite database "dev.db" from "file:dev.db" was successfully reset.
🚀 Your database is now in sync with your schema. Done in XXms
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
})
it('unexecutable - drop cancelled (prompt)', async () => {
ctx.fixture('existing-db-warnings')
const mockExit = jest.spyOn(process, 'exit').mockImplementation()
prompt.inject([new Error()]) // simulate user cancellation
const result = DbPush.new().parse([])
await expect(result).resolves.toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
⚠️ There might be data loss when applying the changes:
• You are about to drop the \`Blog\` table, which is not empty (1 rows).
Push cancelled.
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(mockExit).toBeCalledWith(0)
})
it('unexecutable - --force-reset', async () => {
ctx.fixture('existing-db-1-unexecutable-schema-change')
const result = DbPush.new().parse(['--force-reset'])
await expect(result).resolves.toMatchInlineSnapshot(``)
expect(ctx.mocked['console.info'].mock.calls.join('\n'))
.toMatchInlineSnapshot(`
Prisma schema loaded from prisma/schema.prisma
Datasource "my_db": SQLite database "dev.db" at "file:dev.db"
The SQLite database "dev.db" from "file:dev.db" was successfully reset.
🚀 Your database is now in sync with your schema. Done in XXms
`)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
})
it('unexecutable - should ask for --force-reset in CI', async () => {
ctx.fixture('existing-db-1-unexecutable-schema-change')
const result = DbPush.new().parse([])
await expect(result).rejects.toMatchInlineSnapshot(`
⚠️ We found changes that cannot be executed:
• Made the column \`fullname\` on table \`Blog\` required, but there are 1 existing NULL values.
Use the --force-reset flag to drop the database before push like prisma db push --force-reset
All data will be lost.
`)
expect(
ctx.mocked['console.log'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
expect(
ctx.mocked['console.error'].mock.calls.join('\n'),
).toMatchInlineSnapshot(``)
})
})