-
Notifications
You must be signed in to change notification settings - Fork 213
/
enums.rs
565 lines (460 loc) Β· 14.3 KB
/
enums.rs
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
use migration_engine_tests::sync_test_api::*;
use sql_schema_describer::ColumnTypeFamily;
const BASIC_ENUM_DM: &str = r#"
model Cat {
id Int @id
mood CatMood
}
enum CatMood {
HAPPY
HUNGRY
}
"#;
#[test_connector(capabilities(Enums))]
fn adding_an_enum_field_must_work(api: TestApi) {
let dm = r#"
model Test {
id String @id @default(cuid())
enum MyEnum
}
enum MyEnum {
A
B
}
"#;
api.schema_push_w_datasource(dm).send().assert_green_bang();
api.assert_schema().assert_table("Test", |table| {
table.assert_columns_count(2).assert_column("enum", |c| {
if api.is_postgres() {
c.assert_is_required()
.assert_type_family(ColumnTypeFamily::Enum("MyEnum".to_owned()))
} else if api.is_mysql() {
c.assert_is_required().assert_type_family(ColumnTypeFamily::Enum(
api.normalize_identifier("Test_enum").into_owned(),
))
} else {
c.assert_is_required().assert_type_is_string()
}
})
});
// Check that the migration is idempotent.
api.schema_push_w_datasource(dm).send().assert_no_steps();
}
#[test_connector(capabilities(Enums))]
fn adding_an_enum_field_must_work_with_native_types_off(api: TestApi) {
let dm = r#"
model Test {
id String @id @default(cuid())
enum MyEnum
}
enum MyEnum {
A
B
}
"#;
api.schema_push_w_datasource(dm).send().assert_green_bang();
api.assert_schema().assert_table("Test", |table| {
table.assert_columns_count(2).assert_column("enum", |c| {
if api.is_postgres() {
c.assert_is_required()
.assert_type_family(ColumnTypeFamily::Enum("MyEnum".to_owned()))
} else if api.is_mysql() {
c.assert_is_required()
.assert_type_family(ColumnTypeFamily::Enum(api.normalize_identifier("Test_enum").into()))
} else {
c.assert_is_required().assert_type_is_string()
}
})
});
// Check that the migration is idempotent.
api.schema_push_w_datasource(dm).send().assert_no_steps();
}
#[test_connector(capabilities(Enums))]
fn an_enum_can_be_turned_into_a_model(api: TestApi) {
api.schema_push_w_datasource(BASIC_ENUM_DM).send().assert_green_bang();
let enum_name = if api.lower_cases_table_names() {
"cat_mood"
} else if api.is_mysql() {
"Cat_mood"
} else {
"CatMood"
};
#[allow(clippy::redundant_closure)]
api.assert_schema().assert_enum(enum_name, |enm| enm);
let dm2 = r#"
model Cat {
id Int @id
moodId Int
mood CatMood @relation(fields: [moodId], references: [id])
}
model CatMood {
id Int @id
description String
biteRisk Int
c Cat[]
}
"#;
api.schema_push_w_datasource(dm2).send().assert_green_bang();
api.assert_schema()
.assert_table("Cat", |table| table.assert_columns_count(2).assert_has_column("moodId"))
.assert_table("CatMood", |table| table.assert_column_count(3))
.assert_has_no_enum("CatMood");
}
#[test_connector(capabilities(Enums))]
fn variants_can_be_added_to_an_existing_enum(api: TestApi) {
let dm1 = r#"
model Cat {
id Int @id
mood CatMood
}
enum CatMood {
HUNGRY
}
"#;
api.schema_push_w_datasource(dm1).send().assert_green_bang();
let enum_name = if api.lower_cases_table_names() {
"cat_mood"
} else if api.is_mysql() {
"Cat_mood"
} else {
"CatMood"
};
api.assert_schema()
.assert_enum(enum_name, |enm| enm.assert_values(&["HUNGRY"]));
let dm2 = r#"
model Cat {
id Int @id
mood CatMood
}
enum CatMood {
HUNGRY
HAPPY
JOYJOY
}
"#;
api.schema_push_w_datasource(dm2).send().assert_green_bang();
api.assert_schema()
.assert_enum(enum_name, |enm| enm.assert_values(&["HUNGRY", "HAPPY", "JOYJOY"]));
}
#[test_connector(capabilities(Enums))]
fn variants_can_be_removed_from_an_existing_enum(api: TestApi) {
let dm1 = r#"
model Cat {
id Int @id
mood CatMood
}
enum CatMood {
HAPPY
HUNGRY
}
"#;
api.schema_push_w_datasource(dm1).send().assert_green_bang();
let enum_name = if api.lower_cases_table_names() {
"cat_mood"
} else if api.is_mysql() {
"Cat_mood"
} else {
"CatMood"
};
api.assert_schema()
.assert_enum(enum_name, |enm| enm.assert_values(&["HAPPY", "HUNGRY"]));
let dm2 = r#"
model Cat {
id Int @id
mood CatMood
}
enum CatMood {
HUNGRY
}
"#;
let warning = if api.is_mysql() {
"The values [HAPPY] on the enum `Cat_mood` will be removed. If these variants are still used in the database, this will fail."
} else {
"The values [HAPPY] on the enum `CatMood` will be removed. If these variants are still used in the database, this will fail."
};
api.schema_push_w_datasource(dm2)
.force(true)
.send()
.assert_warnings(&[warning.into()])
.assert_executable();
api.assert_schema()
.assert_enum(enum_name, |enm| enm.assert_values(&["HUNGRY"]));
}
#[test_connector(capabilities(Enums))]
fn models_with_enum_values_can_be_dropped(api: TestApi) {
api.schema_push_w_datasource(BASIC_ENUM_DM).send().assert_green_bang();
api.assert_schema().assert_tables_count(1);
api.insert("Cat").value("id", 1).value("mood", "HAPPY").result_raw();
let warn = if api.lower_cases_table_names() {
"You are about to drop the `cat` table, which is not empty (1 rows)."
} else {
"You are about to drop the `Cat` table, which is not empty (1 rows)."
};
api.schema_push_w_datasource("")
.force(true)
.send()
.assert_executable()
.assert_warnings(&[warn.into()]);
api.assert_schema().assert_tables_count(0);
}
#[test_connector(capabilities(Enums))]
fn enum_field_to_string_field_works(api: TestApi) {
let dm1 = r#"
model Cat {
id Int @id
mood CatMood?
}
enum CatMood {
HAPPY
HUNGRY
}
"#;
api.schema_push_w_datasource(dm1).send().assert_green_bang();
api.assert_schema().assert_table("Cat", |table| {
table.assert_column("mood", |col| col.assert_type_is_enum())
});
api.insert("Cat").value("id", 1).value("mood", "HAPPY").result_raw();
let dm2 = r#"
model Cat {
id Int @id
mood String?
}
"#;
api.schema_push_w_datasource(dm2).force(true).send().assert_executable();
api.assert_schema().assert_table("Cat", |table| {
table.assert_column("mood", |col| col.assert_type_is_string())
});
}
#[test_connector(capabilities(Enums))]
fn string_field_to_enum_field_works(api: TestApi) {
let dm1 = r#"
model Cat {
id Int @id
mood String?
}
"#;
api.schema_push_w_datasource(dm1).send().assert_green_bang();
api.assert_schema().assert_table("Cat", |table| {
table.assert_column("mood", |col| col.assert_type_is_string())
});
api.insert("Cat").value("id", 1).value("mood", "HAPPY").result_raw();
let dm2 = r#"
model Cat {
id Int @id
mood CatMood?
}
enum CatMood {
HAPPY
HUNGRY
}
"#;
let warn = if api.is_postgres() {
"The `mood` column on the `Cat` table would be dropped and recreated. This will lead to data loss."
} else if api.lower_cases_table_names() {
"You are about to alter the column `mood` on the `cat` table, which contains 1 non-null values. The data in that column will be cast from `VarChar(191)` to `Enum(\"Cat_mood\")`."
} else {
"You are about to alter the column `mood` on the `Cat` table, which contains 1 non-null values. The data in that column will be cast from `VarChar(191)` to `Enum(\"Cat_mood\")`."
};
api.schema_push_w_datasource(dm2)
.force(true)
.send()
.assert_executable()
.assert_warnings(&[warn.into()]);
api.assert_schema().assert_table("Cat", |table| {
table.assert_column("mood", |col| col.assert_type_is_enum())
});
}
#[test_connector(capabilities(Enums))]
fn enums_used_in_default_can_be_changed(api: TestApi) {
let dm1 = r#"
model Panther {
id Int @id
mood CatMood @default(HAPPY)
}
model Tiger {
id Int @id
mood CatMood @default(HAPPY)
}
model Leopard {
id Int @id
mood CatMood @default(HAPPY)
}
model Lion {
id Int @id
mood CatMood
}
model GoodDog {
id Int @id
mood DogMood @default(HAPPY)
}
enum CatMood {
HAPPY
HUNGRY
}
enum DogMood {
HAPPY
HUNGRY
}
"#;
api.schema_push_w_datasource(dm1).send().assert_green_bang();
api.assert_schema().assert_tables_count(5);
let dm2 = r#"
model Panther {
id Int @id
mood CatMood @default(HAPPY)
}
model Tiger {
id Int @id
mood CatMood @default(HAPPY)
}
model Leopard {
id Int @id
mood CatMood
}
model Lion {
id Int @id
mood CatMood @default(HAPPY)
}
model GoodDog {
id Int @id
mood DogMood @default(HAPPY)
}
enum CatMood {
HAPPY
ANGRY
}
enum DogMood {
HAPPY
HUNGRY
SLEEPY
}
"#;
if api.is_postgres() {
api.schema_push_w_datasource(dm2)
.force(true)
.send()
.assert_executable()
.assert_warnings(&["The values [HUNGRY] on the enum `CatMood` will be removed. If these variants are still used in the database, this will fail.".into()]
);
} else {
api.schema_push_w_datasource(dm2)
.force(true)
.send()
.assert_executable()
.assert_warnings(& ["The values [HUNGRY] on the enum `Panther_mood` will be removed. If these variants are still used in the database, this will fail.".into(),
"The values [HUNGRY] on the enum `Tiger_mood` will be removed. If these variants are still used in the database, this will fail.".into(),]
);
};
api.assert_schema().assert_tables_count(5);
}
#[test_connector(capabilities(Enums))]
fn changing_all_values_of_enums_used_in_defaults_works(api: TestApi) {
let dm1 = r#"
model Cat {
id Int @id
morningMood CatMood @default(HUNGRY)
alternateMorningMood CatMood @default(HUNGRY)
afternoonMood CatMood @default(HAPPY)
eveningMood CatMood @default(HUNGRY)
defaultMood CatMood
}
enum CatMood {
HAPPY
HUNGRY
}
"#;
api.schema_push_w_datasource(dm1).send().assert_green_bang();
let dm2 = r#"
model Cat {
id Int @id
morningMood CatMood @default(MEOW)
alternateMorningMood CatMood @default(MEOWMEOWMEOW)
afternoonMood CatMood @default(PURR)
eveningMood CatMood @default(MEOWMEOW)
defaultMood CatMood
}
enum CatMood {
MEOW
MEOWMEOW
MEOWMEOWMEOW
PURR
}
"#;
api.schema_push_w_datasource(dm2).force(true).send();
api.assert_schema().assert_table("Cat", |table| {
table.assert_column("eveningMood", |col| col.assert_enum_default("MEOWMEOW"))
});
}
#[test_connector(tags(Postgres))]
fn existing_enums_are_picked_up(api: TestApi) {
let sql = r#"
CREATE TYPE "Genre" AS ENUM ('SKA', 'PUNK');
CREATE TABLE "prisma-tests"."Band" (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
genre "Genre" NOT NULL
);
"#;
api.raw_cmd(sql);
let dm = r#"
enum Genre {
SKA
PUNK
}
model Band {
id Int @id @default(autoincrement())
name String
genre Genre
}
"#;
api.schema_push_w_datasource(dm)
.send()
.assert_green_bang()
.assert_no_steps();
}
// Bug: https://github.com/prisma/prisma/issues/8137
#[test_connector(tags(Postgres))]
fn enum_array_modification_should_work(api: TestApi) {
let migrations_directory = api.create_migrations_directory();
let dm = r#"
datasource test {
provider = "postgres"
url = "postgres://meowmeowmeow"
}
enum Position {
First
Second
Last
}
model Test {
id String @id @default(uuid())
positions Position[]
}
"#;
api.create_migration("01init", &dm, &migrations_directory).send_sync();
api.apply_migrations(&migrations_directory)
.send_sync()
.assert_applied_migrations(&["01init"]);
let dm = r#"
datasource test {
provider = "postgres"
url = "postgres://meowmeowmeow"
}
enum Position {
First
Second
}
model Test {
id String @id @default(uuid())
positions Position[]
}
"#;
api.create_migration("02remove", &dm, &migrations_directory).send_sync();
api.apply_migrations(&migrations_directory)
.send_sync()
.assert_applied_migrations(&["02remove"]);
api.create_migration("03empty", &dm, &migrations_directory).send_sync();
api.apply_migrations(&migrations_directory)
.send_sync()
.assert_applied_migrations(&[]);
}