-
Notifications
You must be signed in to change notification settings - Fork 3
/
PostgreSqlMigrationSqlGeneretorTest.cs
485 lines (384 loc) · 18 KB
/
PostgreSqlMigrationSqlGeneretorTest.cs
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
using System;
using System.Data.Entity.Migrations.Model;
using System.Data.Metadata.Edm;
using System.Data.Spatial;
using System.Globalization;
using System.Threading;
using NUnit.Framework;
namespace System.Data.Entity.Migrations.Sql.Test
{
[TestFixture]
public class PostgreSqlMigrationSqlGeneretorTest
{
[TestCase(null, "", "NOT NULL")]
[TestCase(false, "SET NOT NULL", "DROP NOT NULL")]
[TestCase(true, "DROP NOT NULL", "SET NOT NULL")]
public void GenerateAlterColumn(bool? nullable, string contain, string notContain)
{
var migrationProvider = new PostgreSqlMigrationSqlGenerator();
var column = new ColumnModel(PrimitiveTypeKind.String)
{
Name = "C",
MaxLength = 120,
IsNullable = nullable
};
var alterColumnOperation = new AlterColumnOperation("dbo.T", column, false);
var sql = migrationProvider.Generate(new[] { alterColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
const string returnSqlBase = "ALTER TABLE \"dbo\".\"T\" ALTER COLUMN \"C\" ";
Assert.IsNotNull(sql);
Assert.True(sql.Contains(returnSqlBase));
Assert.True(sql.Contains(returnSqlBase + "TYPE varchar(120)"));
Assert.True(sql.Contains(returnSqlBase + contain));
Assert.False(sql.Contains(returnSqlBase + notContain));
}
[Test]
public void GenerateShouldOutputInvariantDecimalsWhenNonInvariantCulture()
{
var migrationProvider = new PostgreSqlMigrationSqlGenerator();
var addColumnOperation
= new AddColumnOperation(
"T",
new ColumnModel(PrimitiveTypeKind.Binary)
{
Name = "C",
DefaultValue = 123.45m
});
var lastCulture = Thread.CurrentThread.CurrentCulture;
try
{
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("nl-NL");
var sql = migrationProvider.Generate(new[] { addColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains("ALTER TABLE \"T\" ADD \"C\" bytea DEFAULT 123,45"));
}
finally
{
Thread.CurrentThread.CurrentCulture = lastCulture;
}
}
[Test]
public void GenerateCanOutputAddTimestampColumnOperation()
{
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var addColumnOperation
= new AddColumnOperation(
"T",
new ColumnModel(PrimitiveTypeKind.Binary)
{
IsNullable = false,
Name = "C",
IsTimestamp = true
});
var sql = migrationSqlGenerator.Generate(new[] { addColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains("ALTER TABLE \"T\" ADD \"C\" bytea NOT NULL"));
}
[Test]
public void GenerateCanOutputAddTimestampStoreTypeColumnOperation()
{
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var addColumnOperation
= new AddColumnOperation(
"T",
new ColumnModel(PrimitiveTypeKind.Binary)
{
IsNullable = false,
Name = "C",
StoreType = "timestamp"
});
var sql = migrationSqlGenerator.Generate(new[] { addColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains("ALTER TABLE \"T\" ADD \"C\" timestamp NOT NULL"));
}
[Test]
public void GenerateCanOutputDropIndexOperation()
{
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var dropIndexOperation = new DropIndexOperation
{
Table = "dbo.Custumers"
};
dropIndexOperation.Columns.Add("Id");
var sql = migrationSqlGenerator.Generate(new[] { dropIndexOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains("DROP INDEX \"dbo\".\"IX_dbo.Custumers_Id\""));
}
[Test]
public void GenerateCanOutputDropPrimaryKeyOperation()
{
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var dropPrimaryKeyOperation = new DropPrimaryKeyOperation
{
Table = "T"
};
var sql = migrationSqlGenerator.Generate(new[] { dropPrimaryKeyOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains("ALTER TABLE \"T\" DROP CONSTRAINT \"PK_T\""));
}
[Test]
public void GenerateCanOutputAddPrimaryKeyOperation()
{
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var addPrimaryKeyOperation = new AddPrimaryKeyOperation
{
Table = "T"
};
addPrimaryKeyOperation.Columns.Add("c1");
addPrimaryKeyOperation.Columns.Add("c2");
var sql = migrationSqlGenerator.Generate(new[] { addPrimaryKeyOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains("ALTER TABLE \"T\" ADD CONSTRAINT \"PK_T\" PRIMARY KEY (\"c1\", \"c2\")"));
}
[Test]
public void GenerateCanOutputDropColumn()
{
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var dropColumnOperation = new DropColumnOperation("Customers", "Foo");
var sql = migrationSqlGenerator.Generate(new[] { dropColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains("ALTER TABLE \"Customers\" DROP COLUMN \"Foo\""));
}
[Test]
public void GenerateCanOutputTimestampColumn()
{
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var createTableOperation = new CreateTableOperation("Customers");
var column = new ColumnModel(PrimitiveTypeKind.Binary)
{
Name = "Version",
IsTimestamp = true
};
createTableOperation.Columns.Add(column);
var sql = migrationSqlGenerator.Generate(new[] { createTableOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains("\"Version\" bytea"));
}
[Test]
public void GenerateCanOutputCustomSqlOperation()
{
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var sql = migrationSqlGenerator.Generate(new[] { new SqlOperation("insert into foo") }, "9.2").Join(
s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains(@"insert into foo"));
}
[Test]
public void GenerateCanOutputCreateTableStatement()
{
var createTableOperation = new CreateTableOperation("dbo.Customers");
var idColumn = new ColumnModel(PrimitiveTypeKind.Int32)
{
Name = "Id",
IsNullable = true,
IsIdentity = true
};
createTableOperation.Columns.Add(idColumn);
createTableOperation.Columns.Add(
new ColumnModel(PrimitiveTypeKind.String)
{
Name = "Name",
IsNullable = false
});
createTableOperation.PrimaryKey = new AddPrimaryKeyOperation();
createTableOperation.PrimaryKey.Columns.Add(idColumn.Name);
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var sql = migrationSqlGenerator.Generate(new[] { createTableOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(
sql.Contains("CREATE TABLE \"dbo\".\"Customers\" (\r\n \"Id\" serial,\r\n \"Name\" text NOT NULL,\r\n CONSTRAINT \"PK_dbo.Customers\" PRIMARY KEY (\"Id\")\r\n)"));
}
[Test]
public void GenerateCanOutputCreateIndexStatement()
{
var createTableOperation = new CreateTableOperation("Customers");
var idColumn = new ColumnModel(PrimitiveTypeKind.Int32)
{
Name = "Id",
IsNullable = true,
IsIdentity = true
};
createTableOperation.Columns.Add(idColumn);
createTableOperation.Columns.Add(
new ColumnModel(PrimitiveTypeKind.String)
{
Name = "Name",
IsNullable = false
});
createTableOperation.PrimaryKey = new AddPrimaryKeyOperation();
createTableOperation.PrimaryKey.Columns.Add(idColumn.Name);
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var createIndexOperation = new CreateIndexOperation
{
Table = createTableOperation.Name,
IsUnique = true
};
createIndexOperation.Columns.Add(idColumn.Name);
var sql
= migrationSqlGenerator.Generate(
new[]
{
createIndexOperation
},
"9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(
sql.Contains(
@"CREATE UNIQUE INDEX ""IX_Customers_Id"" ON ""Customers""(""Id"")"));
}
[Test]
public void GenerateCanOutputAddFkStatement()
{
var addForeignKeyOperation = new AddForeignKeyOperation
{
PrincipalTable = "Customers",
DependentTable = "Orders",
CascadeDelete = true
};
addForeignKeyOperation.PrincipalColumns.Add("CustomerId");
addForeignKeyOperation.DependentColumns.Add("CustomerId");
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var sql = migrationSqlGenerator.Generate(new[] { addForeignKeyOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(
sql.Contains(
"ALTER TABLE \"Orders\" ADD CONSTRAINT \"FK_Orders_Customers_CustomerId\" FOREIGN KEY (\"CustomerId\") REFERENCES \"Customers\" (\"CustomerId\") ON DELETE CASCADE"));
}
[Test]
public void GenerateCanOutputDropTableStatement()
{
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var sql = migrationSqlGenerator.Generate(new[] { new DropTableOperation("dbo.Customers") }, "9.2").Join(
s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains("DROP TABLE \"dbo\".\"Customers\""));
}
[Test]
public void GenerateCanOutputDeleteHistoryOperation()
{
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var sql = migrationSqlGenerator.Generate(new[] { new DeleteHistoryOperation("__MigrationHistory", "201212120818356_AddHouses") }, "9.2").Join(
s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains("DELETE FROM \"dbo\".\"__MigrationHistory\" WHERE \"MigrationId\" = '201212120818356_AddHouses'"));
}
[TestCase(PrimitiveTypeKind.Guid, "uuid")]
[TestCase(PrimitiveTypeKind.Int16, "smallserial")]
[TestCase(PrimitiveTypeKind.Int32, "serial")]
[TestCase(PrimitiveTypeKind.Int64, "bigserial")]
[TestCase(PrimitiveTypeKind.String, "text")]
public void GenerateCanOutputAddColumnStatement(PrimitiveTypeKind type, string typeName)
{
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var column = new ColumnModel(type)
{
Name = "Bar",
IsIdentity = true
};
var addColumnOperation = new AddColumnOperation("Foo", column);
var sql = migrationSqlGenerator.Generate(
new[] { addColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains(string.Format("ALTER TABLE \"Foo\" ADD \"Bar\" {0}", typeName)));
}
[Test]
public void GenerateCanOutputAddColumnStatementWithCustomStoreType()
{
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var column = new ColumnModel(PrimitiveTypeKind.String)
{
Name = "Bar",
StoreType = "character",
MaxLength = 15
};
var addColumnOperation = new AddColumnOperation("Foo", column);
var sql = migrationSqlGenerator.Generate(new[] { addColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains("ALTER TABLE \"Foo\" ADD \"Bar\" character(15)"));
}
[Test]
public void GenerateCanOutputAddGeometryColumnOperationWithDefaultValue()
{
var operation
= new AddColumnOperation(
"T",
new ColumnModel(PrimitiveTypeKind.Geometry)
{
IsNullable = false,
Name = "C",
DefaultValue = DbGeometry.FromText("POINT (8 9)")
});
var sql = new PostgreSqlMigrationSqlGenerator().Generate(new[] { operation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.AreEqual("ALTER TABLE \"T\" ADD \"C\" point NOT NULL DEFAULT 'SRID=0;POINT (8 9)'", sql);
}
[Test]
public void GenerateCanOutputAddGeometryColumnOperationWithImplicitDefaultValue()
{
var operation
= new AddColumnOperation(
"T",
new ColumnModel(PrimitiveTypeKind.Geometry)
{
IsNullable = false,
Name = "C"
});
var sql = new PostgreSqlMigrationSqlGenerator().Generate(new[] { operation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.AreEqual("ALTER TABLE \"T\" ADD \"C\" point NOT NULL DEFAULT 'SRID=0;POINT (0 0)'", sql);
}
[Test]
public void GenerateCanOutputAddGeometryColumnOperationWithSqlDefaultValue()
{
var operation
= new AddColumnOperation(
"T",
new ColumnModel(PrimitiveTypeKind.Geometry)
{
IsNullable = false,
Name = "C",
DefaultValueSql = "'POINT (8 9)'"
});
var sql = new PostgreSqlMigrationSqlGenerator().Generate(new[] { operation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.AreEqual("ALTER TABLE \"T\" ADD \"C\" point NOT NULL DEFAULT 'POINT (8 9)'", sql);
}
[Test]
public void GenerateCanOutputAlterGeometryColumnOperationWithNoDefaultValue()
{
var operation
= new AlterColumnOperation(
"T",
new ColumnModel(PrimitiveTypeKind.Geometry)
{
IsNullable = false,
Name = "C",
},
isDestructiveChange: false);
var sql = new PostgreSqlMigrationSqlGenerator().Generate(new[] { operation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.AreEqual(
"ALTER TABLE \"T\" ALTER COLUMN \"C\" TYPE point;ALTER TABLE \"T\" ALTER COLUMN \"C\" SET NOT NULL;", sql);
}
[Test]
public void GenerateCanOutputAddColumnStatementWithExplicitDefaultValue()
{
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var column = new ColumnModel(PrimitiveTypeKind.Guid)
{
Name = "Bar",
IsNullable = false,
DefaultValue = 42
};
var addColumnOperation = new AddColumnOperation("Foo", column);
var sql = migrationSqlGenerator.Generate(new[] { addColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains("ALTER TABLE \"Foo\" ADD \"Bar\" uuid NOT NULL DEFAULT 42"));
}
[Test]
public void GenerateCanOutputAddColumnStatementWithExplicitDefaultValueSql()
{
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var column = new ColumnModel(PrimitiveTypeKind.Guid)
{
Name = "Bar",
IsNullable = false,
DefaultValueSql = "42"
};
var addColumnOperation = new AddColumnOperation("Foo", column);
var sql = migrationSqlGenerator.Generate(new[] { addColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains("ALTER TABLE \"Foo\" ADD \"Bar\" uuid NOT NULL DEFAULT 42"));
}
[Test]
public void GenerateCanOutputAddColumnStatementWhenNonNullableAndNoDefaultProvided()
{
var migrationSqlGenerator = new PostgreSqlMigrationSqlGenerator();
var column = new ColumnModel(PrimitiveTypeKind.Int32)
{
Name = "Bar",
IsNullable = false
};
var addColumnOperation = new AddColumnOperation("Foo", column);
var sql = migrationSqlGenerator.Generate(new[] { addColumnOperation }, "9.2").Join(s => s.Sql, Environment.NewLine);
Assert.True(sql.Contains("ALTER TABLE \"Foo\" ADD \"Bar\" integer NOT NULL DEFAULT 0"));
}
}
}