Skip to content

Commit eb16975

Browse files
JaBistDuNarrischJaBistDuNarrisch
authored andcommitted
Use MigratorDbType instead of DbType in some methods
1 parent 39459ab commit eb16975

File tree

2 files changed

+51
-43
lines changed

2 files changed

+51
-43
lines changed

src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_GetColumnsTypeTests.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public void GetColumns_DataTypeResolveSucceeds()
2525
const string stringColumnName2 = "stringcolumn2";
2626
const string binaryColumnName1 = "binarycolumn";
2727
const string doubleColumnName1 = "doublecolumn";
28+
const string intervalColumnName1 = "intervalcolumn";
2829

2930
// Should be extended by remaining types
3031
Provider.AddTable(testTableName,
@@ -38,9 +39,11 @@ public void GetColumns_DataTypeResolveSucceeds()
3839
new Column(stringColumnName1, DbType.String),
3940
new Column(stringColumnName2, DbType.String) { Size = 30 },
4041
new Column(binaryColumnName1, DbType.Binary),
41-
new Column(doubleColumnName1, DbType.Double)
42+
new Column(doubleColumnName1, DbType.Double),
43+
new Column(intervalColumnName1, MigratorDbType.Interval)
4244
);
4345

46+
4447
// Act
4548
var columns = Provider.GetColumns(testTableName);
4649

@@ -55,24 +58,26 @@ public void GetColumns_DataTypeResolveSucceeds()
5558
var stringColumn2 = columns.Single(x => x.Name == stringColumnName2);
5659
var binaryColumn1 = columns.Single(x => x.Name == binaryColumnName1);
5760
var doubleColumn1 = columns.Single(x => x.Name == doubleColumnName1);
61+
var intervalColumn1 = columns.Single(x => x.Name == intervalColumnName1);
5862

5963

6064
// Assert
61-
Assert.That(dateTimeColumn1.Type, Is.EqualTo(DbType.DateTime));
65+
Assert.That(dateTimeColumn1.MigratorDbType, Is.EqualTo(MigratorDbType.DateTime));
6266
Assert.That(dateTimeColumn1.Precision, Is.EqualTo(3));
63-
Assert.That(dateTimeColumn2.Type, Is.EqualTo(DbType.DateTime2));
67+
Assert.That(dateTimeColumn2.MigratorDbType, Is.EqualTo(MigratorDbType.DateTime2));
6468
Assert.That(dateTimeColumn2.Precision, Is.EqualTo(6));
65-
Assert.That(decimalColumn1.Type, Is.EqualTo(DbType.Decimal));
69+
Assert.That(decimalColumn1.MigratorDbType, Is.EqualTo(MigratorDbType.Decimal));
6670
Assert.That(decimalColumn1.Precision, Is.EqualTo(19));
6771
Assert.That(decimalColumn1.Scale, Is.EqualTo(5));
68-
Assert.That(guidColumn1.Type, Is.EqualTo(DbType.Guid));
69-
Assert.That(booleanColumn1.Type, Is.EqualTo(DbType.Boolean));
70-
Assert.That(int32Column1.Type, Is.EqualTo(DbType.Int32));
71-
Assert.That(int64column1.Type, Is.EqualTo(DbType.Int64));
72-
Assert.That(stringColumn1.Type, Is.EqualTo(DbType.String));
73-
Assert.That(stringColumn2.Type, Is.EqualTo(DbType.String));
72+
Assert.That(guidColumn1.MigratorDbType, Is.EqualTo(MigratorDbType.Guid));
73+
Assert.That(booleanColumn1.MigratorDbType, Is.EqualTo(MigratorDbType.Boolean));
74+
Assert.That(int32Column1.MigratorDbType, Is.EqualTo(MigratorDbType.Int32));
75+
Assert.That(int64column1.MigratorDbType, Is.EqualTo(MigratorDbType.Int64));
76+
Assert.That(stringColumn1.MigratorDbType, Is.EqualTo(MigratorDbType.String));
77+
Assert.That(stringColumn2.MigratorDbType, Is.EqualTo(MigratorDbType.String));
7478
Assert.That(stringColumn2.Size, Is.EqualTo(30));
75-
Assert.That(binaryColumn1.Type, Is.EqualTo(DbType.Binary));
76-
Assert.That(doubleColumn1.Type, Is.EqualTo(DbType.Double));
79+
Assert.That(binaryColumn1.MigratorDbType, Is.EqualTo(MigratorDbType.Binary));
80+
Assert.That(doubleColumn1.MigratorDbType, Is.EqualTo(MigratorDbType.Double));
81+
Assert.That(intervalColumn1.MigratorDbType, Is.EqualTo(MigratorDbType.Interval));
7782
}
7883
}

src/Migrator/Providers/Impl/PostgreSQL/PostgreSQLTransformationProvider.cs

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using System.Data;
1818
using System.Globalization;
1919
using System.Linq;
20-
using System.Runtime.InteropServices;
2120
using System.Text;
2221
using System.Text.RegularExpressions;
2322
using Index = DotNetProjects.Migrator.Framework.Index;
@@ -181,11 +180,11 @@ public override void ChangeColumn(string table, Column column)
181180

182181
var change1 = string.Format("{0} TYPE {1}", QuoteColumnNameIfRequired(mapper.Name), mapper.type);
183182

184-
if ((oldColumn.Type == DbType.Int16 || oldColumn.Type == DbType.Int32 || oldColumn.Type == DbType.Int64 || oldColumn.Type == DbType.Decimal) && column.Type == DbType.Boolean)
183+
if ((oldColumn.MigratorDbType == MigratorDbType.Int16 || oldColumn.MigratorDbType == MigratorDbType.Int32 || oldColumn.MigratorDbType == MigratorDbType.Int64 || oldColumn.MigratorDbType == MigratorDbType.Decimal) && column.MigratorDbType == MigratorDbType.Boolean)
185184
{
186185
change1 += string.Format(" USING CASE {0} WHEN 1 THEN true ELSE false END", QuoteColumnNameIfRequired(mapper.Name));
187186
}
188-
else if (column.Type == DbType.Boolean)
187+
else if (column.MigratorDbType == MigratorDbType.Boolean)
189188
{
190189
change1 += string.Format(" USING CASE {0} WHEN '1' THEN true ELSE false END", QuoteColumnNameIfRequired(mapper.Name));
191190
}
@@ -286,19 +285,19 @@ public override Column[] GetColumns(string table)
286285
var numericPrecision = reader.IsDBNull(numericPrecisionOrdinal) ? null : (int?)reader.GetInt32(numericPrecisionOrdinal);
287286
var numericScale = reader.IsDBNull(numericScaleOrdinal) ? null : (int?)reader.GetInt32(numericScaleOrdinal);
288287

289-
DbType dbType = 0;
288+
MigratorDbType dbType = 0;
290289
int? precision = null;
291290
int? scale = null;
292291
int? size = null;
293292

294293
if (new[] { "timestamptz", "timestamp with time zone" }.Contains(dataTypeString))
295294
{
296-
dbType = DbType.DateTimeOffset;
295+
dbType = MigratorDbType.DateTimeOffset;
297296
precision = dateTimePrecision;
298297
}
299298
else if (dataTypeString == "double precision")
300299
{
301-
dbType = DbType.Double;
300+
dbType = MigratorDbType.Double;
302301
scale = numericScale;
303302
precision = numericPrecision;
304303
}
@@ -307,77 +306,81 @@ public override Column[] GetColumns(string table)
307306
// 6 is the maximum in PostgreSQL
308307
if (dateTimePrecision > 5)
309308
{
310-
dbType = DbType.DateTime2;
309+
dbType = MigratorDbType.DateTime2;
311310
}
312311
else
313312
{
314-
dbType = DbType.DateTime;
313+
dbType = MigratorDbType.DateTime;
315314
}
316315

317316
precision = dateTimePrecision;
318317
}
319318
else if (dataTypeString == "smallint")
320319
{
321-
dbType = DbType.Int16;
320+
dbType = MigratorDbType.Int16;
322321
}
323322
else if (dataTypeString == "integer")
324323
{
325-
dbType = DbType.Int32;
324+
dbType = MigratorDbType.Int32;
326325
}
327326
else if (dataTypeString == "bigint")
328327
{
329-
dbType = DbType.Int64;
328+
dbType = MigratorDbType.Int64;
330329
}
331330
else if (dataTypeString == "numeric")
332331
{
333-
dbType = DbType.Decimal;
332+
dbType = MigratorDbType.Decimal;
334333
precision = numericPrecision;
335334
scale = numericScale;
336335
}
337336
else if (dataTypeString == "real")
338337
{
339-
dbType = DbType.Single;
338+
dbType = MigratorDbType.Single;
339+
}
340+
else if (dataTypeString == "interval")
341+
{
342+
dbType = MigratorDbType.Interval;
340343
}
341344
else if (dataTypeString == "money")
342345
{
343-
dbType = DbType.Currency;
346+
dbType = MigratorDbType.Currency;
344347
}
345348
else if (dataTypeString == "date")
346349
{
347-
dbType = DbType.Date;
350+
dbType = MigratorDbType.Date;
348351
}
349352
else if (dataTypeString == "byte")
350353
{
351-
dbType = DbType.Binary;
354+
dbType = MigratorDbType.Binary;
352355
}
353356
else if (dataTypeString == "uuid")
354357
{
355-
dbType = DbType.Guid;
358+
dbType = MigratorDbType.Guid;
356359
}
357360
else if (dataTypeString == "xml")
358361
{
359-
dbType = DbType.Xml;
362+
dbType = MigratorDbType.Xml;
360363
}
361364
else if (dataTypeString == "time")
362365
{
363-
dbType = DbType.Time;
366+
dbType = MigratorDbType.Time;
364367
}
365368
else if (dataTypeString == "interval")
366369
{
367370
throw new NotImplementedException();
368371
}
369372
else if (dataTypeString == "boolean")
370373
{
371-
dbType = DbType.Boolean;
374+
dbType = MigratorDbType.Boolean;
372375
}
373376
else if (dataTypeString == "text" || dataTypeString == "character varying")
374377
{
375-
dbType = DbType.String;
378+
dbType = MigratorDbType.String;
376379
size = characterMaximumLength;
377380
}
378381
else if (dataTypeString == "bytea")
379382
{
380-
dbType = DbType.Binary;
383+
dbType = MigratorDbType.Binary;
381384
}
382385
else if (dataTypeString == "character" || dataTypeString.StartsWith("character("))
383386
{
@@ -400,19 +403,19 @@ public override Column[] GetColumns(string table)
400403

401404
if (defaultValueString != null)
402405
{
403-
if (column.Type == DbType.Int16 || column.Type == DbType.Int32 || column.Type == DbType.Int64)
406+
if (column.MigratorDbType == MigratorDbType.Int16 || column.MigratorDbType == MigratorDbType.Int32 || column.MigratorDbType == MigratorDbType.Int64)
404407
{
405408
column.DefaultValue = long.Parse(defaultValueString.ToString());
406409
}
407-
else if (column.Type == DbType.UInt16 || column.Type == DbType.UInt32 || column.Type == DbType.UInt64)
410+
else if (column.MigratorDbType == MigratorDbType.UInt16 || column.MigratorDbType == MigratorDbType.UInt32 || column.MigratorDbType == MigratorDbType.UInt64)
408411
{
409412
column.DefaultValue = ulong.Parse(defaultValueString.ToString());
410413
}
411-
else if (column.Type == DbType.Double || column.Type == DbType.Single)
414+
else if (column.MigratorDbType == MigratorDbType.Double || column.MigratorDbType == MigratorDbType.Single)
412415
{
413416
column.DefaultValue = double.Parse(defaultValueString.ToString(), CultureInfo.InvariantCulture);
414417
}
415-
else if (column.Type == DbType.Boolean)
418+
else if (column.MigratorDbType == MigratorDbType.Boolean)
416419
{
417420
var truthy = new[] { "TRUE", "YES", "'true'", "on", "'on'", "t", "'t'" };
418421
var falsy = new[] { "FALSE", "NO", "'false'", "off", "'off'", "f", "'f'" };
@@ -430,7 +433,7 @@ public override Column[] GetColumns(string table)
430433
throw new NotImplementedException($"Cannot interpret the given default value in column '{column.Name}'");
431434
}
432435
}
433-
else if (column.Type == DbType.DateTime || column.Type == DbType.DateTime2)
436+
else if (column.MigratorDbType == MigratorDbType.DateTime || column.MigratorDbType == MigratorDbType.DateTime2)
434437
{
435438
if (defaultValueString.StartsWith("'"))
436439
{
@@ -453,7 +456,7 @@ public override Column[] GetColumns(string table)
453456
throw new NotImplementedException();
454457
}
455458
}
456-
else if (column.Type == DbType.Guid)
459+
else if (column.MigratorDbType == MigratorDbType.Guid)
457460
{
458461
if (defaultValueString.StartsWith("'"))
459462
{
@@ -471,11 +474,11 @@ public override Column[] GetColumns(string table)
471474
throw new NotImplementedException();
472475
}
473476
}
474-
else if (column.Type == DbType.Decimal)
477+
else if (column.MigratorDbType == MigratorDbType.Decimal)
475478
{
476479
column.DefaultValue = decimal.Parse(defaultValueString, CultureInfo.InvariantCulture);
477480
}
478-
else if (column.Type == DbType.String)
481+
else if (column.MigratorDbType == MigratorDbType.String)
479482
{
480483
if (defaultValueString.StartsWith("'"))
481484
{
@@ -493,7 +496,7 @@ public override Column[] GetColumns(string table)
493496
throw new NotImplementedException();
494497
}
495498
}
496-
else if (column.Type == DbType.Binary)
499+
else if (column.MigratorDbType == MigratorDbType.Binary)
497500
{
498501
if (defaultValueString.StartsWith("'"))
499502
{

0 commit comments

Comments
 (0)