-
Notifications
You must be signed in to change notification settings - Fork 216
/
mod.rs
888 lines (762 loc) Β· 27.7 KB
/
mod.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
use barrel::{functions, types};
use indoc::formatdoc;
use indoc::indoc;
use introspection_engine_tests::test_api::*;
use quaint::prelude::Queryable;
use test_macros::test_connector;
#[test_connector]
async fn a_simple_table_with_gql_types(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("Blog", move |t| {
t.add_column("bool", types::boolean());
t.add_column("float", types::float());
t.add_column("date", types::datetime());
t.add_column("id", types::primary());
t.add_column("int", types::integer());
t.add_column("string", types::text());
});
})
.await?;
let float_native = if api.sql_family().is_mssql() {
"@db.Real"
} else if api.sql_family().is_mysql() {
"@db.Float"
} else {
""
};
let timestamp_native = if api.sql_family().is_postgres() {
"@db.Timestamp(6)"
} else if api.sql_family().is_mysql() {
"@db.DateTime(0)"
} else {
""
};
let text_native = if api.sql_family().is_mssql() || api.sql_family().is_mysql() {
"@db.Text"
} else {
""
};
let dm = formatdoc! {r##"
model Blog {{
bool Boolean
float Float {float_native}
date DateTime {timestamp_native}
id Int @id @default(autoincrement())
int Int
string String {text_native}
}}
"##, float_native = float_native, timestamp_native = timestamp_native, text_native = text_native};
api.assert_eq_datamodels(&dm, &api.introspect().await?);
Ok(())
}
#[test_connector]
async fn should_ignore_prisma_helper_tables(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("Blog", move |t| {
t.add_column("id", types::primary());
});
migration.create_table("_RelayId", move |t| {
t.add_column("id", types::primary());
t.add_column("stablemodelidentifier", types::text());
});
migration.create_table("_Migration", move |t| {
t.add_column("revision", types::text());
t.add_column("name", types::text());
t.add_column("datamodel", types::text());
t.add_column("status", types::text());
t.add_column("applied", types::text());
t.add_column("rolled_back", types::text());
t.add_column("datamodel_steps", types::text());
t.add_column("database_migration", types::text());
t.add_column("errors", types::text());
t.add_column("started_at", types::text());
t.add_column("finished_at", types::text());
});
migration.create_table("_prisma_migrations", move |t| {
t.add_column("id", types::primary());
t.add_column("checksum", types::text());
t.add_column("finished_at", types::text());
t.add_column("migration_name", types::text());
t.add_column("logs", types::text());
t.add_column("rolled_back_at", types::text());
t.add_column("started_at", types::text());
t.add_column("applied_steps_count", types::text());
});
})
.await?;
let dm = indoc! {r##"
model Blog {
id Int @id @default(autoincrement())
}
"##};
api.assert_eq_datamodels(dm, &api.introspect().await?);
Ok(())
}
#[test_connector]
async fn a_table_with_compound_primary_keys(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("Blog", |t| {
t.add_column("id", types::integer());
t.add_column("authorId", types::integer());
t.set_primary_key(&["id", "authorId"]);
});
})
.await?;
let dm = indoc! {r##"
model Blog {
id Int
authorId Int
@@id([id, authorId])
}
"##};
api.assert_eq_datamodels(dm, &api.introspect().await?);
Ok(())
}
#[test_connector]
async fn a_table_with_unique_index(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("Blog", |t| {
t.add_column("id", types::primary());
t.add_column("authorId", types::integer());
t.add_index("test", types::index(vec!["authorId"]).unique(true));
});
})
.await?;
let dm = indoc! {r##"
model Blog {
id Int @id @default(autoincrement())
authorId Int @unique
}
"##};
api.assert_eq_datamodels(dm, &api.introspect().await?);
Ok(())
}
#[test_connector]
async fn a_table_with_multi_column_unique_index(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("User", |t| {
t.add_column("id", types::primary());
t.add_column("firstname", types::integer());
t.add_column("lastname", types::integer());
t.add_index("test", types::index(vec!["firstname", "lastname"]).unique(true));
});
})
.await?;
let dm = indoc! {r##"
model User {
id Int @id @default(autoincrement())
firstname Int
lastname Int
@@unique([firstname, lastname], name: "test")
}
"##};
api.assert_eq_datamodels(dm, &api.introspect().await?);
Ok(())
}
#[test_connector]
async fn a_table_with_required_and_optional_columns(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("User", |t| {
t.add_column("id", types::primary());
t.add_column("requiredname", types::integer().nullable(false));
t.add_column("optionalname", types::integer().nullable(true));
});
})
.await?;
let dm = indoc! {r##"
model User {
id Int @id @default(autoincrement())
requiredname Int
optionalname Int?
}
"##};
api.assert_eq_datamodels(dm, &api.introspect().await?);
Ok(())
}
#[test_connector]
async fn a_table_with_default_values(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("User", |t| {
t.add_column("id", types::primary());
t.add_column("bool", types::boolean().default(false));
t.add_column("bool2", types::boolean().default(true));
t.add_column("float", types::float().default(5.3));
t.add_column("int", types::integer().default(5));
t.add_column("string", types::char(4).default("Test"));
});
})
.await?;
let native_string = if !api.sql_family().is_sqlite() {
"@db.Char(4)"
} else {
""
};
let float_string = if api.sql_family().is_mysql() {
"@db.Float"
} else if api.sql_family().is_mssql() {
"@db.Real"
} else {
""
};
let dm = formatdoc! {r##"
model User {{
id Int @id @default(autoincrement())
bool Boolean @default(false)
bool2 Boolean @default(true)
float Float @default(5.3) {}
int Int @default(5)
string String @default("Test") {}
}}
"##, float_string, native_string};
api.assert_eq_datamodels(&dm, &api.introspect().await?);
Ok(())
}
#[test_connector]
async fn a_table_with_a_non_unique_index(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("User", |t| {
t.add_column("a", types::integer());
t.add_column("id", types::primary());
t.add_index("test", types::index(vec!["a"]));
});
})
.await?;
let dm = indoc! {r##"
model User {
a Int
id Int @id @default(autoincrement())
@@index([a], name: "test")
}
"##};
api.assert_eq_datamodels(dm, &api.introspect().await?);
Ok(())
}
#[test_connector]
async fn a_table_with_a_multi_column_non_unique_index(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("User", |t| {
t.add_column("a", types::integer());
t.add_column("b", types::integer());
t.add_column("id", types::primary());
t.add_index("test", types::index(vec!["a", "b"]));
});
})
.await?;
let dm = indoc! { r##"
model User {
a Int
b Int
id Int @id @default(autoincrement())
@@index([a,b], name: "test")
}
"##};
api.assert_eq_datamodels(dm, &api.introspect().await?);
Ok(())
}
// SQLite does not have a serial type that's not a primary key.
#[test_connector(exclude(Sqlite))]
async fn a_table_with_non_id_autoincrement(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("Test", |t| {
t.add_column("id", types::integer().primary(true));
t.add_column("authorId", types::serial().unique(true));
});
})
.await?;
let dm = indoc! {r#"
model Test {
id Int @id
authorId Int @default(autoincrement()) @unique
}
"#};
api.assert_eq_datamodels(dm, &api.introspect().await?);
Ok(())
}
#[test_connector]
async fn default_values(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("Test", move |t| {
t.add_column("id", types::primary());
t.add_column(
"string_static_char",
types::custom("char(5)").default("test").nullable(true),
);
t.add_column(
"string_static_char_null",
types::r#char(5).default(types::null()).nullable(true),
);
t.add_column(
"string_static_varchar",
types::varchar(5).default("test").nullable(true),
);
t.add_column("int_static", types::integer().default(2).nullable(true));
t.add_column("float_static", types::float().default(1.43).nullable(true));
t.add_column("boolean_static", types::boolean().default(true).nullable(true));
t.add_column(
"datetime_now",
types::datetime().default(functions::current_timestamp()).nullable(true),
);
});
})
.await?;
let char_native = if !api.sql_family().is_sqlite() {
"@db.Char(5)"
} else {
""
};
let varchar_native = if !api.sql_family().is_sqlite() {
"@db.VarChar(5)"
} else {
""
};
let float_native = if api.sql_family().is_mssql() {
"@db.Real"
} else if api.sql_family().is_mysql() {
"@db.Float"
} else {
""
};
let timestamp_native = if api.sql_family().is_postgres() {
"@db.Timestamp(6)"
} else if api.sql_family().is_mysql() {
"@db.DateTime(0)"
} else {
""
};
let dm = formatdoc! { r#"
model Test {{
id Int @id @default(autoincrement())
string_static_char String? @default("test") {}
string_static_char_null String? {}
string_static_varchar String? @default("test") {}
int_static Int? @default(2)
float_static Float? @default(1.43) {}
boolean_static Boolean? @default(true)
datetime_now DateTime? @default(now()) {}
}}
"#, char_native, char_native, varchar_native, float_native, timestamp_native};
api.assert_eq_datamodels(&dm, &api.introspect().await?);
Ok(())
}
#[test_connector(tags(Postgres), exclude(Cockroach))]
async fn pg_default_value_as_dbgenerated(api: &TestApi) -> TestResult {
let sequence = "CREATE SEQUENCE test_seq START 1".to_string();
api.database().execute_raw(&sequence, &[]).await?;
api.barrel()
.execute(|migration| {
migration.create_table("Test", |t| {
t.add_column("id", types::primary());
t.inject_custom("string_function text Default E' ' || '>' || ' '");
t.inject_custom("int_serial Serial4");
t.inject_custom("int_function Integer DEFAULT EXTRACT(year from TIMESTAMP '2001-02-16 20:38:40')");
t.inject_custom("int_sequence Integer DEFAULT nextval('test_seq')");
t.inject_custom("datetime_now TIMESTAMP DEFAULT NOW()");
t.inject_custom("datetime_now_lc TIMESTAMP DEFAULT now()");
});
})
.await?;
let dm = indoc! {r#"
model Test {
id Int @id @default(autoincrement())
string_function String? @default(dbgenerated("((' '::text || '>'::text) || ' '::text)"))
int_serial Int @default(autoincrement())
int_function Int? @default(dbgenerated("date_part('year'::text, '2001-02-16 20:38:40'::timestamp without time zone)"))
int_sequence Int? @default(autoincrement())
datetime_now DateTime? @default(now()) @db.Timestamp(6)
datetime_now_lc DateTime? @default(now()) @db.Timestamp(6)
}
"#};
api.assert_eq_datamodels(dm, &api.introspect().await?);
Ok(())
}
//todo maybe need to split due to
// no function default values on mysql 5.7 and 8.0 -.-
// maria db allows this
#[test_connector(tags(Mysql))]
async fn my_default_value_as_dbgenerated(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("Test", |t| {
t.add_column("id", types::primary());
t.inject_custom("datetime_now TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP");
t.inject_custom("datetime_now_lc TIMESTAMP NULL DEFAULT current_timestamp");
});
})
.await?;
let dm = indoc! {r#"
model Test {
id Int @id @default(autoincrement())
datetime_now DateTime? @default(now()) @db.Timestamp(0)
datetime_now_lc DateTime? @default(now()) @db.Timestamp(0)
}
"#};
api.assert_eq_datamodels(dm, &api.introspect().await?);
Ok(())
}
#[test_connector(tags(Mysql8))]
async fn a_table_with_an_index_that_contains_expressions_should_be_ignored(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("Test", |t| {
t.add_column("id", types::integer().primary(true));
t.add_column("parentId", types::integer().nullable(true));
t.add_column("name", types::varchar(45).nullable(true));
t.inject_custom("UNIQUE KEY `SampleTableUniqueIndexName` (`name`,(ifnull(`parentId`,-(1))))");
});
})
.await?;
let dm = indoc! {r#"
model Test {
id Int @id
parentId Int?
name String? @db.VarChar(45)
}
"#};
api.assert_eq_datamodels(dm, &api.introspect().await?);
Ok(())
}
#[test_connector(tags(Postgres))]
async fn default_values_on_lists_should_be_ignored(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("User", |t| {
t.add_column("id", types::primary());
t.inject_custom("ints integer[] DEFAULT array[]::integer[]");
t.inject_custom("ints2 integer[] DEFAULT '{}'");
});
})
.await?;
let dm = indoc! {r#"
model User {
id Int @id @default(autoincrement())
ints Int[]
ints2 Int[]
}
"#};
let result = api.introspect().await?;
api.assert_eq_datamodels(dm, &result);
Ok(())
}
// MySQL doesn't have partial indices.
#[test_connector(exclude(Mysql))]
async fn a_table_with_partial_indexes_should_ignore_them(api: &TestApi) -> TestResult {
api.barrel()
.execute(move |migration| {
migration.create_table("pages", move |t| {
t.add_column("id", types::primary());
t.add_column("staticId", types::integer().nullable(false));
t.add_column("latest", types::integer().nullable(false));
t.add_column("other", types::integer().nullable(false));
t.add_index("full", types::index(vec!["other"]).unique(true));
t.add_partial_index("partial", types::index(vec!["staticId"]).unique(true), "latest = 1");
});
})
.await?;
let dm = indoc! {
r#"
model pages {
id Int @id @default(autoincrement())
staticId Int
latest Int
other Int @unique
}
"#
};
api.assert_eq_datamodels(dm, &api.introspect().await?);
Ok(())
}
#[test_connector(tags(Postgres))]
async fn introspecting_a_table_with_json_type_must_work(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("Blog", |t| {
t.add_column("id", types::primary());
t.add_column("json", types::json());
});
})
.await?;
let dm = indoc! {r#"
model Blog {
id Int @id @default(autoincrement())
json Json @db.Json
}
"#};
let result = api.introspect().await?;
api.assert_eq_datamodels(dm, &result);
Ok(())
}
#[test_connector(tags(Mariadb))]
async fn different_default_values_should_work(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("Blog", move |t| {
t.add_column("id", types::primary());
t.inject_custom("text Text Default \"one\"");
t.inject_custom("`tinytext_string` tinytext COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT \"twelve\"");
t.inject_custom("`tinytext_number_string` tinytext COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT \"1\"");
t.inject_custom("`tinytext_number` tinytext COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 10");
t.inject_custom("`tinytext_float` tinytext COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 1.0");
t.inject_custom("`tinytext_short` tinytext COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 1");
});
})
.await?;
let dm = indoc! {r##"
model Blog {
id Int @id @default(autoincrement())
text String? @default("one") @db.Text
tinytext_string String @default("twelve") @db.TinyText
tinytext_number_string String @default("1") @db.TinyText
tinytext_number String @default(dbgenerated("(10)")) @db.TinyText
tinytext_float String @default(dbgenerated("(1.0)")) @db.TinyText
tinytext_short String @default(dbgenerated("(1)")) @db.TinyText
}
"##};
api.assert_eq_datamodels(dm, &api.introspect().await?);
Ok(())
}
#[test_connector(exclude(Sqlite))]
async fn negative_default_values_should_work(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("Blog", move |t| {
t.add_column("id", types::primary());
t.add_column("int", types::integer().default(1));
t.add_column("neg_int", types::integer().default(-1));
t.add_column("float", types::float().default(2.1));
t.add_column("neg_float", types::float().default(-2.1));
t.add_column("big_int", types::custom("bigint").default(3));
t.add_column("neg_big_int", types::custom("bigint").default(-3));
});
})
.await?;
let float_native = if api.sql_family().is_mysql() {
"@db.Float"
} else if api.sql_family().is_mssql() {
"@db.Real"
} else {
""
};
let dm = formatdoc! {r##"
model Blog {{
id Int @id @default(autoincrement())
int Int @default(1)
neg_int Int @default(-1)
float Float @default(2.1) {float_native}
neg_float Float @default(-2.1) {float_native}
big_int BigInt @default(3)
neg_big_int BigInt @default(-3)
}}
"##, float_native = float_native};
api.assert_eq_datamodels(&dm, &api.introspect().await?);
Ok(())
}
#[test_connector(tags(Mysql))]
async fn partial_indexes_should_be_ignored_on_mysql(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("Blog", move |t| {
t.add_column("id", types::primary());
t.add_column("int_col", types::integer());
t.inject_custom("blob_col mediumblob");
t.inject_custom("Index `partial_blob_col_index` (blob_col(10))");
t.inject_custom("Index `partial_compound` (blob_col(10), int_col)");
});
})
.await?;
let dm = indoc! {r##"
model Blog {
id Int @id @default(autoincrement())
int_col Int
blob_col Bytes? @db.MediumBlob
}
"##};
let result = &api.introspect().await?;
api.assert_eq_datamodels(dm, result);
Ok(())
}
#[test_connector(tags(Sqlite))]
async fn expression_indexes_should_be_ignored_on_sqlite(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("Blog", move |t| {
t.add_column("id", types::primary());
t.add_column("author", types::text());
});
migration.inject_custom("CREATE INDEX author_lowercase_index ON Blog(LOWER(author));")
})
.await?;
let dm = indoc! {r##"
model Blog {
id Int @id @default(autoincrement())
author String
}
"##};
let result = &api.introspect().await?;
api.assert_eq_datamodels(dm, result);
Ok(())
}
#[test_connector(tags(Mysql))]
async fn casing_should_not_lead_to_mix_ups(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("address", move |t| {
t.inject_custom("addressid INT NOT NULL");
t.inject_custom("PRIMARY KEY(addressid)");
});
migration.create_table("ADDRESS", move |t| {
t.inject_custom("ADDRESSID INT NOT NULL");
t.inject_custom("PRIMARY KEY(ADDRESSID)");
});
migration.create_table("Address", move |t| {
t.inject_custom("AddressID INT NOT NULL AUTO_INCREMENT");
t.inject_custom("PRIMARY KEY(AddressID)");
});
})
.await?;
let dm = indoc! {r##"
model ADDRESS {
ADDRESSID Int @id
}
model Address {
AddressID Int @id @default(autoincrement())
}
model address {
addressid Int @id
}
"##};
let result = &api.introspect().await?;
api.assert_eq_datamodels(dm, result);
Ok(())
}
#[test_connector(tags(Mysql), exclude(Mariadb))]
async fn unique_and_index_on_same_field_works_mysql(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.inject_custom(
"create table users (
id serial primary key not null
);",
)
})
.await?;
let dm = indoc! {r##"
model users {
id BigInt @id @unique @default(autoincrement()) @db.UnsignedBigInt
}
"##};
let result = &api.introspect().await?;
api.assert_eq_datamodels(dm, result);
Ok(())
}
#[test_connector(tags(Mariadb))]
async fn unique_and_index_on_same_field_works_mariadb(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.inject_custom(
"create table users (
id Integer primary key not null,
CONSTRAINT really_must_be_different UNIQUE (id)
);",
)
})
.await?;
let dm = indoc! {r##"
model users {
id Int @id @unique
}
"##};
let result = &api.introspect().await?;
api.assert_eq_datamodels(dm, result);
Ok(())
}
#[test_connector(tags(Sqlite))]
async fn unique_and_index_on_same_field_works_sqlite(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.inject_custom(
"create table users (
id Integer primary key not null unique
);",
)
})
.await?;
let dm = indoc! {r##"
model users {
id Int @id @unique @default(autoincrement())
}
"##};
let result = &api.introspect().await?;
api.assert_eq_datamodels(dm, result);
Ok(())
}
#[test_connector(tags(Mssql))]
async fn unique_and_id_on_same_field_works_mssql(api: &TestApi) -> TestResult {
let schema = api.schema_name();
api.barrel()
.execute(|migration| {
migration.inject_custom(format!(
"create table {}.users (
id int identity primary key,
constraint unique_and_index_same unique(id)
);",
schema
))
})
.await?;
let dm = indoc! {r##"
model users {
id Int @id @unique @default(autoincrement())
}
"##};
let result = &api.introspect().await?;
api.assert_eq_datamodels(dm, result);
Ok(())
}
#[test_connector(tags(Postgres))]
// If multiple constraints are created in the create table statement Postgres seems to collapse them
// into the first named one. So on the db level there will be one named really_must_be_different that
// is both unique and primary. We only render it as @id then.
// If a later alter table statement adds another unique constraint then it is persisted as its own
// entity and can be introspected.
async fn unique_and_index_on_same_field_works_postgres(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.inject_custom(
"create table users (
id Integer primary key not null,
CONSTRAINT really_must_be_different UNIQUE (id),
CONSTRAINT must_be_different UNIQUE (id)
);",
)
})
.await?;
let dm = indoc! {r##"
model users {
id Int @id
}
"##};
let result = &api.introspect().await?;
api.assert_eq_datamodels(dm, result);
api.barrel()
.execute(|migration| migration.inject_custom("Alter table users Add Constraint z_unique Unique(id);"))
.await?;
let dm2 = indoc! {r##"
model users {
id Int @id @unique
}
"##};
let result = &api.introspect().await?;
api.assert_eq_datamodels(dm2, result);
Ok(())
}