-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathNpgsqlMigrationsSqlGeneratorTest.cs
More file actions
637 lines (507 loc) · 17.4 KB
/
NpgsqlMigrationsSqlGeneratorTest.cs
File metadata and controls
637 lines (507 loc) · 17.4 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
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal;
using Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Operations;
namespace Microsoft.EntityFrameworkCore.Migrations;
public class NpgsqlMigrationsSqlGeneratorTest() : MigrationsSqlGeneratorTestBase(
NpgsqlTestHelpers.Instance,
new ServiceCollection().AddEntityFrameworkNpgsqlNetTopologySuite(),
NpgsqlTestHelpers.Instance.AddProviderOptions(
((IRelationalDbContextOptionsBuilderInfrastructure)
new NpgsqlDbContextOptionsBuilder(new DbContextOptionsBuilder()).UseNetTopologySuite())
.OptionsBuilder).Options)
{
#region Database
[Fact]
public virtual void CreateDatabaseOperation()
{
Generate(new NpgsqlCreateDatabaseOperation { Name = "Northwind" });
AssertSql(
"""
CREATE DATABASE "Northwind";
""");
}
[ConditionalFact]
public virtual void CreateDatabaseOperation_with_collation()
{
Generate(
new NpgsqlCreateDatabaseOperation { Name = "Northwind", Collation = "POSIX" });
AssertSql(
"""
CREATE DATABASE "Northwind"
LC_COLLATE "POSIX";
""");
}
[Fact]
public virtual void CreateDatabaseOperation_with_template()
{
Generate(new NpgsqlCreateDatabaseOperation { Name = "Northwind", Template = "MyTemplate" });
AssertSql(
"""
CREATE DATABASE "Northwind"
TEMPLATE "MyTemplate";
""");
}
[Fact]
public virtual void CreateDatabaseOperation_with_tablespace()
{
Generate(new NpgsqlCreateDatabaseOperation { Name = "some_db", Tablespace = "MyTablespace" });
AssertSql(
"""
CREATE DATABASE some_db
TABLESPACE "MyTablespace";
""");
}
[Fact]
public virtual void CreateDatabaseOperation_with_encoding()
{
Generate(new NpgsqlCreateDatabaseOperation { Name = "Northwind", Encoding = "UTF8" });
AssertSql(
"""
CREATE DATABASE "Northwind"
ENCODING "UTF8";
""");
}
#endregion
public override void AddColumnOperation_without_column_type()
{
base.AddColumnOperation_without_column_type();
AssertSql(
"""
ALTER TABLE "People" ADD "Alias" text NOT NULL;
""");
}
public override void AddColumnOperation_with_unicode_overridden()
{
base.AddColumnOperation_with_unicode_overridden();
AssertSql(
"""
ALTER TABLE "Person" ADD "Name" text;
""");
}
public override void AddColumnOperation_with_unicode_no_model()
{
base.AddColumnOperation_with_unicode_no_model();
AssertSql(
"""
ALTER TABLE "Person" ADD "Name" text;
""");
}
public override void AddColumnOperation_with_fixed_length_no_model()
{
base.AddColumnOperation_with_fixed_length_no_model();
AssertSql(
"""
ALTER TABLE "Person" ADD "Name" character(100);
""");
}
public override void AddColumnOperation_with_maxLength_overridden()
{
base.AddColumnOperation_with_maxLength_overridden();
AssertSql(
"""
ALTER TABLE "Person" ADD "Name" character varying(32);
""");
}
public override void AddColumnOperation_with_maxLength_no_model()
{
base.AddColumnOperation_with_maxLength_no_model();
AssertSql(
"""
ALTER TABLE "Person" ADD "Name" character varying(30);
""");
}
public override void AddColumnOperation_with_precision_and_scale_overridden()
{
base.AddColumnOperation_with_precision_and_scale_overridden();
AssertSql(
"""
ALTER TABLE "Person" ADD "Pi" numeric(15,10) NOT NULL;
""");
}
public override void AddColumnOperation_with_precision_and_scale_no_model()
{
base.AddColumnOperation_with_precision_and_scale_no_model();
AssertSql(
"""
ALTER TABLE "Person" ADD "Pi" numeric(20,7) NOT NULL;
""");
}
public override void AddForeignKeyOperation_without_principal_columns()
{
base.AddForeignKeyOperation_without_principal_columns();
AssertSql(
"""
ALTER TABLE "People" ADD FOREIGN KEY ("SpouseId") REFERENCES "People";
""");
}
public override void AlterColumnOperation_without_column_type()
{
base.AlterColumnOperation_without_column_type();
AssertSql(
"""
ALTER TABLE "People" ALTER COLUMN "LuckyNumber" TYPE integer;
""");
}
public override void RenameTableOperation_legacy()
{
base.RenameTableOperation_legacy();
AssertSql(
"""
ALTER TABLE dbo."People" RENAME TO "Person";
""");
}
public override void RenameTableOperation()
{
base.RenameTableOperation();
AssertSql(
"""
ALTER TABLE dbo."People" RENAME TO "Person";
""");
}
public override void SqlOperation()
{
base.SqlOperation();
AssertSql(
"""
-- I <3 DDL
""");
}
public override void InsertDataOperation_all_args_spatial()
{
base.InsertDataOperation_all_args_spatial();
AssertSql(
"""
INSERT INTO dbo."People" ("Id", "Full Name", "Geometry")
VALUES (0, NULL, NULL);
INSERT INTO dbo."People" ("Id", "Full Name", "Geometry")
VALUES (1, 'Daenerys Targaryen', NULL);
INSERT INTO dbo."People" ("Id", "Full Name", "Geometry")
VALUES (2, 'John Snow', NULL);
INSERT INTO dbo."People" ("Id", "Full Name", "Geometry")
VALUES (3, 'Arya Stark', NULL);
INSERT INTO dbo."People" ("Id", "Full Name", "Geometry")
VALUES (4, 'Harry Strickland', NULL);
INSERT INTO dbo."People" ("Id", "Full Name", "Geometry")
VALUES (5, 'The Imp', NULL);
INSERT INTO dbo."People" ("Id", "Full Name", "Geometry")
VALUES (6, 'The Kingslayer', NULL);
INSERT INTO dbo."People" ("Id", "Full Name", "Geometry")
VALUES (7, 'Aemon Targaryen', GEOMETRY 'SRID=4326;GEOMETRYCOLLECTION Z(LINESTRING Z(1.1 2.2 NaN, 2.2 2.2 NaN, 2.2 1.1 NaN, 7.1 7.2 NaN), LINESTRING Z(7.1 7.2 NaN, 20.2 20.2 NaN, 20.2 1.1 NaN, 70.1 70.2 NaN), MULTIPOINT Z((1.1 2.2 NaN), (2.2 2.2 NaN), (2.2 1.1 NaN)), POLYGON Z((1.1 2.2 NaN, 2.2 2.2 NaN, 2.2 1.1 NaN, 1.1 2.2 NaN)), POLYGON Z((10.1 20.2 NaN, 20.2 20.2 NaN, 20.2 10.1 NaN, 10.1 20.2 NaN)), POINT Z(1.1 2.2 3.3), MULTILINESTRING Z((1.1 2.2 NaN, 2.2 2.2 NaN, 2.2 1.1 NaN, 7.1 7.2 NaN), (7.1 7.2 NaN, 20.2 20.2 NaN, 20.2 1.1 NaN, 70.1 70.2 NaN)), MULTIPOLYGON Z(((10.1 20.2 NaN, 20.2 20.2 NaN, 20.2 10.1 NaN, 10.1 20.2 NaN)), ((1.1 2.2 NaN, 2.2 2.2 NaN, 2.2 1.1 NaN, 1.1 2.2 NaN))))');
""");
}
public override void InsertDataOperation_required_args()
{
base.InsertDataOperation_required_args();
AssertSql(
"""
INSERT INTO dbo."People" ("First Name")
VALUES ('John');
""");
}
public override void InsertDataOperation_required_args_composite()
{
base.InsertDataOperation_required_args_composite();
AssertSql(
"""
INSERT INTO dbo."People" ("First Name", "Last Name")
VALUES ('John', 'Snow');
""");
}
public override void InsertDataOperation_required_args_multiple_rows()
{
base.InsertDataOperation_required_args_multiple_rows();
AssertSql(
"""
INSERT INTO dbo."People" ("First Name")
VALUES ('John');
INSERT INTO dbo."People" ("First Name")
VALUES ('Daenerys');
""");
}
public override void DeleteDataOperation_all_args()
{
base.DeleteDataOperation_all_args();
AssertSql(
"""
DELETE FROM "People"
WHERE "First Name" = 'Hodor';
DELETE FROM "People"
WHERE "First Name" = 'Daenerys';
DELETE FROM "People"
WHERE "First Name" = 'John';
DELETE FROM "People"
WHERE "First Name" = 'Arya';
DELETE FROM "People"
WHERE "First Name" = 'Harry';
""");
}
public override void DeleteDataOperation_all_args_composite()
{
base.DeleteDataOperation_all_args_composite();
AssertSql(
"""
DELETE FROM "People"
WHERE "First Name" = 'Hodor' AND "Last Name" IS NULL;
DELETE FROM "People"
WHERE "First Name" = 'Daenerys' AND "Last Name" = 'Targaryen';
DELETE FROM "People"
WHERE "First Name" = 'John' AND "Last Name" = 'Snow';
DELETE FROM "People"
WHERE "First Name" = 'Arya' AND "Last Name" = 'Stark';
DELETE FROM "People"
WHERE "First Name" = 'Harry' AND "Last Name" = 'Strickland';
""");
}
public override void DeleteDataOperation_required_args()
{
base.DeleteDataOperation_required_args();
AssertSql(
"""
DELETE FROM "People"
WHERE "Last Name" = 'Snow';
""");
}
public override void DeleteDataOperation_required_args_composite()
{
base.DeleteDataOperation_required_args_composite();
AssertSql(
"""
DELETE FROM "People"
WHERE "First Name" = 'John' AND "Last Name" = 'Snow';
""");
}
public override void UpdateDataOperation_all_args()
{
base.UpdateDataOperation_all_args();
AssertSql(
"""
UPDATE "People" SET "Birthplace" = 'Winterfell', "House Allegiance" = 'Stark', "Culture" = 'Northmen'
WHERE "First Name" = 'Hodor';
UPDATE "People" SET "Birthplace" = 'Dragonstone', "House Allegiance" = 'Targaryen', "Culture" = 'Valyrian'
WHERE "First Name" = 'Daenerys';
""");
}
public override void UpdateDataOperation_all_args_composite()
{
base.UpdateDataOperation_all_args_composite();
AssertSql(
"""
UPDATE "People" SET "House Allegiance" = 'Stark'
WHERE "First Name" = 'Hodor' AND "Last Name" IS NULL;
UPDATE "People" SET "House Allegiance" = 'Targaryen'
WHERE "First Name" = 'Daenerys' AND "Last Name" = 'Targaryen';
""");
}
public override void UpdateDataOperation_all_args_composite_multi()
{
base.UpdateDataOperation_all_args_composite_multi();
AssertSql(
"""
UPDATE "People" SET "Birthplace" = 'Winterfell', "House Allegiance" = 'Stark', "Culture" = 'Northmen'
WHERE "First Name" = 'Hodor' AND "Last Name" IS NULL;
UPDATE "People" SET "Birthplace" = 'Dragonstone', "House Allegiance" = 'Targaryen', "Culture" = 'Valyrian'
WHERE "First Name" = 'Daenerys' AND "Last Name" = 'Targaryen';
""");
}
public override void UpdateDataOperation_all_args_multi()
{
base.UpdateDataOperation_all_args_multi();
AssertSql(
"""
UPDATE "People" SET "Birthplace" = 'Dragonstone', "House Allegiance" = 'Targaryen', "Culture" = 'Valyrian'
WHERE "First Name" = 'Daenerys';
""");
}
public override void UpdateDataOperation_required_args()
{
base.UpdateDataOperation_required_args();
AssertSql(
"""
UPDATE "People" SET "House Allegiance" = 'Targaryen'
WHERE "First Name" = 'Daenerys';
""");
}
public override void UpdateDataOperation_required_args_multiple_rows()
{
base.UpdateDataOperation_required_args_multiple_rows();
AssertSql(
"""
UPDATE "People" SET "House Allegiance" = 'Stark'
WHERE "First Name" = 'Hodor';
UPDATE "People" SET "House Allegiance" = 'Targaryen'
WHERE "First Name" = 'Daenerys';
""");
}
public override void UpdateDataOperation_required_args_composite()
{
base.UpdateDataOperation_required_args_composite();
AssertSql(
"""
UPDATE "People" SET "House Allegiance" = 'Targaryen'
WHERE "First Name" = 'Daenerys' AND "Last Name" = 'Targaryen';
""");
}
public override void UpdateDataOperation_required_args_composite_multi()
{
base.UpdateDataOperation_required_args_composite_multi();
AssertSql(
"""
UPDATE "People" SET "Birthplace" = 'Dragonstone', "House Allegiance" = 'Targaryen', "Culture" = 'Valyrian'
WHERE "First Name" = 'Daenerys' AND "Last Name" = 'Targaryen';
""");
}
public override void UpdateDataOperation_required_args_multi()
{
base.UpdateDataOperation_required_args_multi();
AssertSql(
"""
UPDATE "People" SET "Birthplace" = 'Dragonstone', "House Allegiance" = 'Targaryen', "Culture" = 'Valyrian'
WHERE "First Name" = 'Daenerys';
""");
}
public override void DefaultValue_with_line_breaks(bool isUnicode)
{
// https://github.com/npgsql/efcore.pg/issues/1478
}
public override void DefaultValue_with_line_breaks_2(bool isUnicode)
{
// https://github.com/npgsql/efcore.pg/issues/1478
}
public override void Sequence_restart_operation(long? startsAt)
{
base.Sequence_restart_operation(startsAt);
AssertSql(
startsAt.HasValue
? $"""
ALTER SEQUENCE dbo."TestRestartSequenceOperation" START WITH {startsAt};
ALTER SEQUENCE dbo."TestRestartSequenceOperation" RESTART;
"""
: """ALTER SEQUENCE dbo."TestRestartSequenceOperation" RESTART;""");
}
[Theory]
[InlineData(MigrationsSqlGenerationOptions.Default)]
[InlineData(MigrationsSqlGenerationOptions.Idempotent)]
public void Alter_column_change_serial_to_identity_idempotent(MigrationsSqlGenerationOptions options)
{
Generate(
modelBuilder =>
{
modelBuilder.HasAnnotation(CoreAnnotationNames.ProductVersion, "3.1.0");
modelBuilder.Entity<Person>().Property<int>("Id").UseSerialColumn();
},
[
new AlterColumnOperation
{
Table = "Person",
Name = "Id",
ClrType = typeof(int),
[NpgsqlAnnotationNames.ValueGenerationStrategy] =
NpgsqlValueGenerationStrategy.IdentityByDefaultColumn,
OldColumn = new AddColumnOperation
{
Table = "Person",
Name = "Id",
ClrType = typeof(int),
[NpgsqlAnnotationNames.ValueGenerationStrategy] =
NpgsqlValueGenerationStrategy.SerialColumn,
}
}
],
options);
AssertSql(
$"""
ALTER SEQUENCE "Person_Id_seq" RENAME TO "Person_Id_old_seq";
ALTER TABLE "Person" ALTER COLUMN "Id" DROP DEFAULT;
ALTER TABLE "Person" ALTER COLUMN "Id" ADD GENERATED BY DEFAULT AS IDENTITY;
{(options == MigrationsSqlGenerationOptions.Idempotent ? "PERFORM" : "SELECT")} * FROM setval('"Person_Id_seq"', nextval('"Person_Id_old_seq"'), false);
DROP SEQUENCE "Person_Id_old_seq";
""");
}
[Fact]
public void Create_schema_idempotent()
{
Generate(
_ => { },
[new EnsureSchemaOperation { Name = "some_schema" }],
MigrationsSqlGenerationOptions.Idempotent);
AssertSql(
"""
IF NOT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = 'some_schema') THEN
CREATE SCHEMA some_schema;
END IF;
""");
}
#region CockroachDB interleave-in-parent
// Note that we don't run tests against actual CockroachDB instances, so these are unit tests asserting on SQL
// only
[Fact]
public void CreateTableOperation_with_cockroach_interleave_in_parent()
{
var op =
new CreateTableOperation
{
Name = "People",
Schema = "dbo",
Columns =
{
new AddColumnOperation
{
Name = "Id",
Table = "People",
Schema = "dbo",
ClrType = typeof(int),
IsNullable = false
},
},
PrimaryKey = new AddPrimaryKeyOperation { Columns = ["Id"] }
};
var interleaveInParent = new CockroachDbInterleaveInParent(op);
interleaveInParent.ParentTableSchema = "my_schema";
interleaveInParent.ParentTableName = "my_parent";
interleaveInParent.InterleavePrefix = ["col_a", "col_b"];
Generate(op);
AssertSql(
"""
CREATE TABLE dbo."People" (
"Id" integer NOT NULL,
PRIMARY KEY ("Id")
)
INTERLEAVE IN PARENT my_schema.my_parent (col_a, col_b);
""");
}
#endregion CockroachDB interleave-in-parent
#pragma warning disable 618
[Fact]
public virtual void AddColumnOperation_serial_old_annotation_throws()
=> Assert.Throws<NotSupportedException>(
() =>
Generate(
new AddColumnOperation
{
Table = "People",
Name = "foo",
ClrType = typeof(int),
ColumnType = "int",
IsNullable = false,
[NpgsqlAnnotationNames.ValueGeneratedOnAdd] = true
}));
public override void InsertDataOperation_throws_for_unsupported_column_types()
=> Assert.Equal(
RelationalStrings.UnsupportedDataOperationStoreType("foo", "dbo.People.First Name"),
Assert.Throws<InvalidOperationException>(
() =>
Generate(
new InsertDataOperation
{
Table = "People",
Schema = "dbo",
Columns = ["First Name"],
ColumnTypes = ["foo"],
Values = new object?[,] { { null } }
})).Message);
#pragma warning restore 618
protected override string GetGeometryCollectionStoreType()
=> "GEOMETRY(GEOMETRYCOLLECTION)";
}