-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathMigrationsNpgsqlTest.cs
More file actions
3321 lines (2892 loc) · 112 KB
/
MigrationsNpgsqlTest.cs
File metadata and controls
3321 lines (2892 loc) · 112 KB
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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#nullable enable
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Scaffolding.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.TestUtilities;
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Migrations;
public class MigrationsNpgsqlTest : MigrationsTestBase<MigrationsNpgsqlTest.MigrationsNpgsqlFixture>
{
public MigrationsNpgsqlTest(MigrationsNpgsqlFixture fixture, ITestOutputHelper testOutputHelper)
: base(fixture)
{
Fixture.TestSqlLoggerFactory.Clear();
Fixture.TestSqlLoggerFactory.SetTestOutputHelper(testOutputHelper);
}
#region Table
public override async Task Create_table()
{
await base.Create_table();
AssertSql(
"""
CREATE TABLE "People" (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
"Name" text,
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
);
""");
}
[Fact]
public override async Task Create_table_all_settings()
{
await base.Create_table_all_settings();
AssertSql(
"""
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = 'dbo2') THEN
CREATE SCHEMA dbo2;
END IF;
END $EF$;
""",
//
"""
CREATE TABLE dbo2."People" (
"CustomId" integer GENERATED BY DEFAULT AS IDENTITY,
"EmployerId" integer NOT NULL,
"SSN" character varying(11) COLLATE "POSIX" NOT NULL,
CONSTRAINT "PK_People" PRIMARY KEY ("CustomId"),
CONSTRAINT "AK_People_SSN" UNIQUE ("SSN"),
CONSTRAINT "CK_People_EmployerId" CHECK ("EmployerId" > 0),
CONSTRAINT "FK_People_Employers_EmployerId" FOREIGN KEY ("EmployerId") REFERENCES "Employers" ("Id") ON DELETE CASCADE
);
COMMENT ON TABLE dbo2."People" IS 'Table comment';
COMMENT ON COLUMN dbo2."People"."EmployerId" IS 'Employer ID comment';
""",
//
"""CREATE INDEX "IX_People_EmployerId" ON dbo2."People" ("EmployerId");""");
}
public override async Task Create_table_no_key()
{
await base.Create_table_no_key();
AssertSql(
"""
CREATE TABLE "Anonymous" (
"SomeColumn" integer NOT NULL
);
""");
}
public override async Task Create_table_with_comments()
{
await base.Create_table_with_comments();
AssertSql(
"""
CREATE TABLE "People" (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
"Name" text,
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
);
COMMENT ON TABLE "People" IS 'Table comment';
COMMENT ON COLUMN "People"."Name" IS 'Column comment';
""");
}
public override async Task Create_table_with_multiline_comments()
{
await base.Create_table_with_multiline_comments();
AssertSql(
"""
CREATE TABLE "People" (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
"Name" text,
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
);
COMMENT ON TABLE "People" IS 'This is a multi-line
table comment.
More information can
be found in the docs.';
COMMENT ON COLUMN "People"."Name" IS 'This is a multi-line
column comment.
More information can
be found in the docs.';
""");
}
public override async Task Create_table_with_computed_column(bool? stored)
{
if (TestEnvironment.PostgresVersion.IsUnder(12))
{
await Assert.ThrowsAsync<NotSupportedException>(() => base.Create_table_with_computed_column(stored));
return;
}
if (stored != true)
{
// Non-stored generated columns aren't yet supported (PG12)
await Assert.ThrowsAsync<NotSupportedException>(() => base.Create_table_with_computed_column(stored));
return;
}
await base.Create_table_with_computed_column(stored: true);
AssertSql(
"""
CREATE TABLE "People" (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
"Sum" text GENERATED ALWAYS AS ("X" + "Y") STORED,
"X" integer NOT NULL,
"Y" integer NOT NULL,
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
);
""");
}
[Fact]
public virtual async Task Create_table_with_identity_by_default()
{
await Test(
_ => { },
builder => builder.Entity("People").Property<int>("Id")
.UseIdentityByDefaultColumn(),
model =>
{
var table = Assert.Single(model.Tables);
var column = Assert.Single(table.Columns);
Assert.Equal(NpgsqlValueGenerationStrategy.IdentityByDefaultColumn, column[NpgsqlAnnotationNames.ValueGenerationStrategy]);
Assert.Null(column[NpgsqlAnnotationNames.IdentityOptions]);
});
AssertSql(
"""
CREATE TABLE "People" (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
);
""");
}
[Fact]
public virtual async Task Create_table_with_identity_always()
{
await Test(
_ => { },
builder => builder.Entity("People").Property<int>("Id")
.UseIdentityAlwaysColumn(),
model =>
{
var table = Assert.Single(model.Tables);
var column = Assert.Single(table.Columns);
Assert.Equal(NpgsqlValueGenerationStrategy.IdentityAlwaysColumn, column[NpgsqlAnnotationNames.ValueGenerationStrategy]);
Assert.Null(column[NpgsqlAnnotationNames.IdentityOptions]);
});
AssertSql(
"""
CREATE TABLE "People" (
"Id" integer GENERATED ALWAYS AS IDENTITY,
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
);
""");
}
[Fact]
public virtual async Task Create_table_with_identity_always_with_options()
{
await Test(
_ => { },
builder => builder.Entity("People").Property<int>("Id")
.UseIdentityAlwaysColumn()
.HasIdentityOptions(startValue: 10, incrementBy: 2, maxValue: 2000),
model =>
{
var table = Assert.Single(model.Tables);
var column = Assert.Single(table.Columns);
Assert.Equal(NpgsqlValueGenerationStrategy.IdentityAlwaysColumn, column[NpgsqlAnnotationNames.ValueGenerationStrategy]);
var options = IdentitySequenceOptionsData.Get(column);
Assert.Equal(10, options.StartValue);
Assert.Equal(2, options.IncrementBy);
Assert.Equal(2000, options.MaxValue);
Assert.Null(options.MinValue);
Assert.Equal(1, options.NumbersToCache);
Assert.False(options.IsCyclic);
});
AssertSql(
"""
CREATE TABLE "People" (
"Id" integer GENERATED ALWAYS AS IDENTITY (START WITH 10 INCREMENT BY 2 MAXVALUE 2000),
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
);
""");
}
[Fact]
public virtual async Task Create_table_with_serial()
{
await Test(
_ => { },
builder => builder.Entity("People").Property<int>("Id")
.UseSerialColumn(),
model =>
{
var table = Assert.Single(model.Tables);
var column = Assert.Single(table.Columns);
Assert.Equal(NpgsqlValueGenerationStrategy.SerialColumn, column[NpgsqlAnnotationNames.ValueGenerationStrategy]);
Assert.Empty(model.Sequences);
});
AssertSql(
"""
CREATE TABLE "People" (
"Id" serial NOT NULL,
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
);
""");
}
[Fact]
public virtual async Task Create_table_with_system_column()
{
// System columns (e.g. xmin) are implicitly always present. If an xmin property is present,
// nothing should happen.
await Test(
_ => { },
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.Property<uint>("xmin");
e.HasKey("Id");
}),
model =>
{
var table = Assert.Single(model.Tables);
Assert.Equal("Id", Assert.Single(table.Columns).Name);
});
AssertSql(
"""
CREATE TABLE "People" (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
);
""");
}
[Fact]
public virtual async Task Create_table_with_oid_column()
{
var isPgAtLeast12 = TestEnvironment.PostgresVersion.AtLeast(12);
await Test(
_ => { },
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.Property<uint>("oid");
e.HasKey("Id");
}),
model =>
{
var table = Assert.Single(model.Tables);
Assert.Equal("Id", isPgAtLeast12 ? table.Columns[0].Name : Assert.Single(table.Columns).Name);
if (isPgAtLeast12)
{
Assert.Equal("oid", table.Columns[1].Name);
}
});
AssertSql(
isPgAtLeast12
? """
CREATE TABLE "People" (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
oid bigint NOT NULL,
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
);
"""
: """
CREATE TABLE "People" (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
);
""");
}
[Fact]
public virtual async Task Create_table_with_storage_parameter()
{
await Test(
_ => { },
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.HasKey("Id");
e.HasStorageParameter("fillfactor", 70);
e.HasStorageParameter("user_catalog_table", true);
}),
model =>
{
var table = Assert.Single(model.Tables);
Assert.Collection(
table.GetAnnotations()
.Where(a => a.Name.StartsWith(NpgsqlAnnotationNames.StorageParameterPrefix, StringComparison.Ordinal))
.OrderBy(a => a.Name),
annotation =>
{
Assert.Equal(NpgsqlAnnotationNames.StorageParameterPrefix + "fillfactor", annotation.Name);
// Storage parameter values always get scaffolded as strings (PG storage is simply 'name=value')
Assert.Equal("70", annotation.Value);
},
annotation =>
{
Assert.Equal(NpgsqlAnnotationNames.StorageParameterPrefix + "user_catalog_table", annotation.Name);
// Storage parameter values always get scaffolded as strings (PG storage is simply 'name=value')
Assert.Equal("true", annotation.Value);
});
});
AssertSql(
"""
CREATE TABLE "People" (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
)
WITH (fillfactor=70, user_catalog_table=true);
""");
}
[Fact]
public virtual async Task Create_table_with_unlogged()
{
await Test(
_ => { },
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.HasKey("Id");
e.IsUnlogged();
}),
asserter: null); // We don't scaffold unlogged
AssertSql(
"""
CREATE UNLOGGED TABLE "People" (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
);
""");
}
public override async Task Drop_table()
{
await base.Drop_table();
AssertSql("""DROP TABLE "People";""");
}
public override async Task Alter_table_add_comment()
{
await base.Alter_table_add_comment();
AssertSql("""COMMENT ON TABLE "People" IS 'Table comment';""");
}
public override async Task Alter_table_add_comment_non_default_schema()
{
await base.Alter_table_add_comment_non_default_schema();
AssertSql("""COMMENT ON TABLE "SomeOtherSchema"."People" IS 'Table comment';""");
}
public override async Task Alter_table_change_comment()
{
await base.Alter_table_change_comment();
AssertSql("""COMMENT ON TABLE "People" IS 'Table comment2';""");
}
public override async Task Alter_table_remove_comment()
{
await base.Alter_table_remove_comment();
AssertSql("""COMMENT ON TABLE "People" IS NULL;""");
}
[Fact]
public virtual async Task Alter_table_change_storage_parameters()
{
await Test(
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.HasKey("Id");
}),
builder => builder.Entity(
"People", e =>
{
e.HasStorageParameter("fillfactor", 70);
e.HasStorageParameter("user_catalog_table", true);
e.HasStorageParameter("parallel_workers", 8);
}),
builder => builder.Entity(
"People", e =>
{
// Add parameter
e.HasStorageParameter("autovacuum_enabled", true);
// Change parameter
e.HasStorageParameter("fillfactor", 80);
// Drop parameter user_catalog
// Leave parameter unchanged
e.HasStorageParameter("parallel_workers", 8);
}),
asserter: null); // We don't scaffold storage parameters
AssertSql(
"""
ALTER TABLE "People" SET (autovacuum_enabled=true, fillfactor=80);
ALTER TABLE "People" RESET (user_catalog_table);
""");
}
[Fact]
public virtual async Task Alter_table_make_unlogged()
{
await Test(
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.HasKey("Id");
}),
_ => { },
builder => builder.Entity("People").IsUnlogged(),
asserter: null); // We don't scaffold unlogged
AssertSql("""ALTER TABLE "People" SET UNLOGGED;""");
}
[Fact]
public virtual async Task Alter_table_make_logged()
{
await Test(
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.HasKey("Id");
}),
builder => builder.Entity("People").IsUnlogged(),
_ => { },
asserter: null); // We don't scaffold unlogged
AssertSql("""ALTER TABLE "People" SET LOGGED;""");
}
public override async Task Rename_table()
{
await base.Rename_table();
AssertSql(
"""ALTER TABLE "People" DROP CONSTRAINT "PK_People";""",
//
"""ALTER TABLE "People" RENAME TO "Persons";""",
//
"""ALTER TABLE "Persons" ADD CONSTRAINT "PK_Persons" PRIMARY KEY ("Id");""");
}
public override async Task Rename_table_with_primary_key()
{
await base.Rename_table_with_primary_key();
AssertSql(
"""ALTER TABLE "People" DROP CONSTRAINT "PK_People";""",
//
"""ALTER TABLE "People" RENAME TO "Persons";""",
//
"""ALTER TABLE "Persons" ADD CONSTRAINT "PK_Persons" PRIMARY KEY ("Id");""");
}
public override async Task Move_table()
{
await base.Move_table();
AssertSql(
"""
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = 'TestTableSchema') THEN
CREATE SCHEMA "TestTableSchema";
END IF;
END $EF$;
""",
//
"""ALTER TABLE "TestTable" SET SCHEMA "TestTableSchema";""");
}
#endregion
#region Schema
public override async Task Create_schema()
{
await base.Create_schema();
AssertSql(
"""
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = 'SomeOtherSchema') THEN
CREATE SCHEMA "SomeOtherSchema";
END IF;
END $EF$;
""",
//
"""
CREATE TABLE "SomeOtherSchema"."People" (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
);
""");
}
[Fact]
public virtual async Task Create_schema_public_is_ignored()
{
await Test(
_ => { },
builder => builder.Entity("People")
.ToTable("People", "public")
.Property<int>("Id"),
model => Assert.Equal("public", Assert.Single(model.Tables).Schema));
AssertSql(
"""
CREATE TABLE public."People" (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
CONSTRAINT "PK_People" PRIMARY KEY ("Id")
);
""");
}
#endregion
#region Column
public override async Task Add_column_with_defaultValue_string()
{
await base.Add_column_with_defaultValue_string();
AssertSql("""ALTER TABLE "People" ADD "Name" text NOT NULL DEFAULT 'John Doe';""");
}
public override async Task Add_column_with_defaultValue_datetime()
{
// We default to mapping DateTime to 'timestamp with time zone', so we need to explicitly specify UTC
await Test(
builder => builder.Entity("People").Property<int>("Id"),
_ => { },
builder => builder.Entity("People").Property<DateTime>("Birthday")
.HasDefaultValue(new DateTime(2015, 4, 12, 17, 5, 0, DateTimeKind.Utc)),
model =>
{
var table = Assert.Single(model.Tables);
Assert.Equal(2, table.Columns.Count);
var birthdayColumn = Assert.Single(table.Columns, c => c.Name == "Birthday");
Assert.False(birthdayColumn.IsNullable);
});
AssertSql("""ALTER TABLE "People" ADD "Birthday" timestamp with time zone NOT NULL DEFAULT TIMESTAMPTZ '2015-04-12T17:05:00Z';""");
}
[Fact]
public override async Task Add_column_with_defaultValueSql()
{
await base.Add_column_with_defaultValueSql();
AssertSql("""ALTER TABLE "People" ADD "Sum" integer NOT NULL DEFAULT (1 + 2);""");
}
public override async Task Add_column_with_computedSql(bool? stored)
{
if (TestEnvironment.PostgresVersion.IsUnder(12))
{
await Assert.ThrowsAsync<NotSupportedException>(() => base.Add_column_with_computedSql(stored));
return;
}
if (stored != true)
{
// Non-stored generated columns aren't yet supported (PG12)
await Assert.ThrowsAsync<NotSupportedException>(() => base.Add_column_with_computedSql(stored));
return;
}
await base.Add_column_with_computedSql(stored: true);
AssertSql("""ALTER TABLE "People" ADD "Sum" text GENERATED ALWAYS AS ("X" + "Y") STORED;""");
}
public override async Task Add_column_with_required()
{
await base.Add_column_with_required();
AssertSql("""ALTER TABLE "People" ADD "Name" text NOT NULL DEFAULT '';""");
}
public override async Task Add_column_with_ansi()
{
await base.Add_column_with_ansi();
AssertSql("""ALTER TABLE "People" ADD "Name" text;""");
}
public override async Task Add_column_with_max_length()
{
await base.Add_column_with_max_length();
AssertSql("""ALTER TABLE "People" ADD "Name" character varying(30);""");
}
public override async Task Add_column_with_unbounded_max_length()
{
await base.Add_column_with_unbounded_max_length();
AssertSql("""ALTER TABLE "People" ADD "Name" text;""");
}
public override async Task Add_column_with_max_length_on_derived()
{
await base.Add_column_with_max_length_on_derived();
AssertSql();
}
public override async Task Add_column_with_fixed_length()
{
await base.Add_column_with_fixed_length();
AssertSql("""ALTER TABLE "People" ADD "Name" character(100);""");
}
public override async Task Add_column_with_comment()
{
await base.Add_column_with_comment();
AssertSql(
"""
ALTER TABLE "People" ADD "FullName" text;
COMMENT ON COLUMN "People"."FullName" IS 'My comment';
""");
}
public override async Task Add_column_with_collation()
{
await base.Add_column_with_collation();
AssertSql("""ALTER TABLE "People" ADD "Name" text COLLATE "POSIX";""");
}
public override async Task Add_column_computed_with_collation(bool stored)
{
if (TestEnvironment.PostgresVersion.IsUnder(12))
{
await Assert.ThrowsAsync<NotSupportedException>(() => base.Add_column_computed_with_collation(stored));
return;
}
if (stored != true)
{
// Non-stored generated columns aren't yet supported (PG12)
await Assert.ThrowsAsync<NotSupportedException>(() => base.Create_table_with_computed_column(stored));
return;
}
await base.Add_column_computed_with_collation(stored);
AssertSql("""ALTER TABLE "People" ADD "Name" text COLLATE "POSIX" GENERATED ALWAYS AS ('hello') STORED;""");
}
public override async Task Add_column_shared()
{
await base.Add_column_shared();
AssertSql();
}
[Fact]
public virtual async Task Add_column_with_upper_case_store_type()
{
// At least for now, it's the user's responsibility to quote store type name when needed,
// because it seems standard for people to specify either text or TEXT, and both should work.
await Test(
_ => { },
builder => builder.Entity("People").Property<string>("Name").HasColumnType("TEXT"),
model =>
{
var table = Assert.Single(model.Tables);
var column = Assert.Single(table.Columns, c => c.Name == "Name");
Assert.Equal("text", column.StoreType);
});
AssertSql(
"""
CREATE TABLE "People" (
"Name" TEXT
);
""");
}
[Fact]
public virtual async Task Add_column_with_identity_by_default()
{
await Test(
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.HasKey("Id");
}),
_ => { },
builder => builder.Entity("People").Property<int?>("SomeColumn")
.UseIdentityByDefaultColumn(),
model =>
{
var table = Assert.Single(model.Tables);
var column = Assert.Single(table.Columns, c => c.Name == "SomeColumn");
Assert.Equal(NpgsqlValueGenerationStrategy.IdentityByDefaultColumn, column[NpgsqlAnnotationNames.ValueGenerationStrategy]);
Assert.Null(column[NpgsqlAnnotationNames.IdentityOptions]);
});
AssertSql("""ALTER TABLE "People" ADD "SomeColumn" integer GENERATED BY DEFAULT AS IDENTITY;""");
}
[Fact]
public virtual async Task Add_column_with_identity_always()
{
await Test(
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.HasKey("Id");
}),
_ => { },
builder => builder.Entity("People").Property<int?>("SomeColumn")
.UseIdentityAlwaysColumn(),
model =>
{
var table = Assert.Single(model.Tables);
var column = Assert.Single(table.Columns, c => c.Name == "SomeColumn");
Assert.Equal(NpgsqlValueGenerationStrategy.IdentityAlwaysColumn, column[NpgsqlAnnotationNames.ValueGenerationStrategy]);
Assert.Null(column[NpgsqlAnnotationNames.IdentityOptions]);
});
AssertSql("""ALTER TABLE "People" ADD "SomeColumn" integer GENERATED ALWAYS AS IDENTITY;""");
}
[Fact]
public virtual async Task Add_column_with_identity_by_default_with_all_options()
{
await Test(
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.HasKey("Id");
}),
_ => { },
builder => builder.Entity("People").Property<int?>("SomeColumn")
.UseIdentityByDefaultColumn()
.HasIdentityOptions(
startValue: 5,
incrementBy: 2,
minValue: 3,
maxValue: 2000,
cyclic: true,
numbersToCache: 10),
model =>
{
var table = Assert.Single(model.Tables);
var column = Assert.Single(table.Columns, c => c.Name == "SomeColumn");
Assert.Equal(NpgsqlValueGenerationStrategy.IdentityByDefaultColumn, column[NpgsqlAnnotationNames.ValueGenerationStrategy]);
var options = IdentitySequenceOptionsData.Get(column);
Assert.Equal(5, options.StartValue);
Assert.Equal(2, options.IncrementBy);
Assert.Equal(3, options.MinValue);
Assert.Equal(2000, options.MaxValue);
Assert.True(options.IsCyclic);
Assert.Equal(10, options.NumbersToCache);
});
AssertSql(
"""
ALTER TABLE "People" ADD "SomeColumn" integer GENERATED BY DEFAULT AS IDENTITY (START WITH 5 INCREMENT BY 2 MINVALUE 3 MAXVALUE 2000 CYCLE CACHE 10);
""");
}
[Fact]
public virtual Task Add_column_optional_with_serial_not_supported()
=> TestThrows<NotSupportedException>(
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.HasKey("Id");
}),
_ => { },
builder => builder.Entity("People").Property<int?>("SomeColumn")
.UseSerialColumn());
[Fact]
public virtual async Task Add_column_required_with_serial()
{
await Test(
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.HasKey("Id");
}),
_ => { },
builder => builder.Entity("People").Property<int>("SomeColumn")
.UseSerialColumn(),
model =>
{
var table = Assert.Single(model.Tables);
var column = Assert.Single(table.Columns, c => c.Name == "SomeColumn");
Assert.Equal(NpgsqlValueGenerationStrategy.SerialColumn, column[NpgsqlAnnotationNames.ValueGenerationStrategy]);
Assert.Null(column[NpgsqlAnnotationNames.IdentityOptions]);
});
AssertSql("""ALTER TABLE "People" ADD "SomeColumn" serial NOT NULL;""");
}
[Fact]
public virtual async Task Add_column_required_with_identity_by_default()
{
await Test(
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.HasKey("Id");
}),
_ => { },
builder => builder.Entity("People").Property<int>("SomeColumn")
.UseIdentityByDefaultColumn(),
model =>
{
var table = Assert.Single(model.Tables);
var column = Assert.Single(table.Columns, c => c.Name == "SomeColumn");
Assert.Equal(NpgsqlValueGenerationStrategy.IdentityByDefaultColumn, column[NpgsqlAnnotationNames.ValueGenerationStrategy]);
Assert.Null(column[NpgsqlAnnotationNames.IdentityOptions]);
});
AssertSql("""ALTER TABLE "People" ADD "SomeColumn" integer GENERATED BY DEFAULT AS IDENTITY;""");
}
[Fact]
public virtual async Task Add_column_system()
{
// System columns (e.g. xmin) are implicitly always present. If an xmin property is added,
// nothing should happen.
await Test(
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.HasKey("Id");
}),
_ => { },
builder => builder.Entity("People").Property<uint>("xmin"),
model =>
{
var table = Assert.Single(model.Tables);
Assert.Equal("Id", Assert.Single(table.Columns).Name);
});
AssertSql();
}
[Fact]
public virtual async Task Add_column_with_huge_varchar()
{
// PostgreSQL doesn't allow varchar(x) with x > 10485760, so we map this to text.
// See #342 and https://www.postgresql.org/message-id/15790.1291824247%40sss.pgh.pa.us
await Test(
builder => builder.Entity(
"People", e =>
{
e.Property<int>("Id");
e.HasKey("Id");
}),
_ => { },
builder => builder.Entity("People").Property<string>("Name").HasMaxLength(10485761),
model =>
{
var table = Assert.Single(model.Tables);
var column = Assert.Single(table.Columns, c => c.Name == "Name");
Assert.Equal("text", column.StoreType);
});
AssertSql("""ALTER TABLE "People" ADD "Name" text;""");
}
[Fact]
public virtual async Task Add_column_with_compression_method()
{
if (TestEnvironment.PostgresVersion.IsUnder(14))
{
return;
}
await Test(
builder => builder.Entity("Blogs", e => e.Property<int>("Id")),
_ => { },
builder => builder.Entity("Blogs").Property<string>("Title").UseCompressionMethod("pglz"),
model =>
{
var table = Assert.Single(model.Tables);
var column = Assert.Single(table.Columns, c => c.Name == "Title");
Assert.Equal("pglz", column[NpgsqlAnnotationNames.CompressionMethod]);
});
AssertSql("""ALTER TABLE "Blogs" ADD "Title" text COMPRESSION pglz;""");
}
public override async Task Alter_column_change_type()
{
await base.Alter_column_change_type();
AssertSql("""ALTER TABLE "People" ALTER COLUMN "SomeColumn" TYPE bigint;""");
}
public override async Task Alter_column_make_required()
{
await base.Alter_column_make_required();
AssertSql(
"""
UPDATE "People" SET "SomeColumn" = '' WHERE "SomeColumn" IS NULL;
ALTER TABLE "People" ALTER COLUMN "SomeColumn" SET NOT NULL;
ALTER TABLE "People" ALTER COLUMN "SomeColumn" SET DEFAULT '';
""");
}
public override async Task Alter_column_make_required_with_null_data()
{
await base.Alter_column_make_required_with_null_data();
AssertSql(
"""
UPDATE "People" SET "SomeColumn" = '' WHERE "SomeColumn" IS NULL;
ALTER TABLE "People" ALTER COLUMN "SomeColumn" SET NOT NULL;
ALTER TABLE "People" ALTER COLUMN "SomeColumn" SET DEFAULT '';
""");
}
public override async Task Alter_column_make_required_with_index()
{