Skip to content

Commit 886c454

Browse files
JaBistDuNarrischJaBistDuNarrisch
authored andcommitted
Extended type test for Postgre SQL
1 parent 6c887c3 commit 886c454

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ public void GetColumns_DataTypeResolveSucceeds()
1818
const string dateTimeColumnName1 = "datetimecolumn1";
1919
const string dateTimeColumnName2 = "datetimecolumn2";
2020
const string decimalColumnName1 = "decimalcolumn";
21+
const string guidColumnName1 = "guidcolumn1";
22+
const string booleanColumnName1 = "booleancolumn1";
23+
const string int32ColumnName1 = "int32column1";
2124

2225
Provider.AddTable(testTableName,
2326
new Column(dateTimeColumnName1, DbType.DateTime),
2427
new Column(dateTimeColumnName2, DbType.DateTime2),
25-
new Column(decimalColumnName1, DbType.Decimal)
28+
new Column(decimalColumnName1, DbType.Decimal),
29+
new Column(guidColumnName1, DbType.Guid),
30+
new Column(booleanColumnName1, DbType.Boolean),
31+
new Column(int32ColumnName1, DbType.Int32)
2632
);
2733

2834
// Act
@@ -31,6 +37,9 @@ public void GetColumns_DataTypeResolveSucceeds()
3137
var dateTimeColumn1 = columns.Single(x => x.Name == dateTimeColumnName1);
3238
var dateTimeColumn2 = columns.Single(x => x.Name == dateTimeColumnName2);
3339
var decimalColumn1 = columns.Single(x => x.Name == decimalColumnName1);
40+
var guidColumn1 = columns.Single(x => x.Name == guidColumnName1);
41+
var booleanColumn1 = columns.Single(x => x.Name == booleanColumnName1);
42+
var int32Column1 = columns.Single(x => x.Name == int32ColumnName1);
3443

3544
// Assert
3645
Assert.That(dateTimeColumn1.Type, Is.EqualTo(DbType.DateTime));
@@ -40,23 +49,13 @@ public void GetColumns_DataTypeResolveSucceeds()
4049
Assert.That(dateTimeColumn2.Precision, Is.EqualTo(6));
4150

4251
Assert.That(decimalColumn1.Type, Is.EqualTo(DbType.Decimal));
52+
Assert.That(decimalColumn1.Precision, Is.EqualTo(19));
53+
Assert.That(decimalColumn1.Scale, Is.EqualTo(5));
4354

44-
// Assert
45-
// using (var command = Provider.GetCommand())
46-
// {
47-
// using var reader = Provider.ExecuteQuery(command, $"SELECT max({propertyName1}) as max from {testTableName}");
48-
// reader.Read();
49-
50-
// var primaryKeyValue = reader.GetInt32(reader.GetOrdinal("max"));
51-
// Assert.That(primaryKeyValue, Is.EqualTo(2));
52-
// }
53-
54-
// // Act II
55-
// var exception = Assert.Throws<PostgresException>(() => Provider.Insert(testTableName, [propertyName1, propertyName2], [1, 888]));
55+
Assert.That(guidColumn1.Type, Is.EqualTo(DbType.Guid));
5656

57-
// // Assert II
58-
// Assert.That(exception.SqlState, Is.EqualTo("428C9"));
57+
Assert.That(booleanColumn1.Type, Is.EqualTo(DbType.Boolean));
5958

60-
throw new NotImplementedException();
59+
Assert.That(int32Column1.Type, Is.EqualTo(DbType.Int32));
6160
}
6261
}

src/Migrator/Framework/Column.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,14 @@ public DbType Type
147147

148148
public int Size { get; set; }
149149

150+
/// <summary>
151+
/// Gets or sets the precision for NUMERIC/DECIMAL
152+
/// </summary>
150153
public int? Precision { get; set; }
151154

155+
/// <summary>
156+
/// Gets or sets the scale for NUMERIC/DECIMAL
157+
/// </summary>
152158
public int? Scale { get; set; }
153159

154160
public ColumnProperty ColumnProperty { get; set; }

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Data;
1818
using System.Globalization;
1919
using System.Linq;
20+
using System.Reflection.Metadata.Ecma335;
2021
using System.Text;
2122
using Index = DotNetProjects.Migrator.Framework.Index;
2223

@@ -257,7 +258,7 @@ public override Column[] GetColumns(string table)
257258
stringBuilder.AppendLine(" DATETIME_PRECISION,");
258259
stringBuilder.AppendLine(" CHARACTER_MAXIMUM_LENGTH,");
259260
stringBuilder.AppendLine(" NUMERIC_PRECISION,");
260-
stringBuilder.AppendLine(" NUMERIC_SCALE,");
261+
stringBuilder.AppendLine(" NUMERIC_SCALE");
261262
stringBuilder.AppendLine($"FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'public' AND TABLE_NAME = lower('{table}');");
262263

263264
var columns = new List<Column>();
@@ -279,9 +280,12 @@ public override Column[] GetColumns(string table)
279280
var dataTypeString = reader.GetString(reader.GetOrdinal("DATA_TYPE"));
280281
var dateTimePrecision = reader.IsDBNull(dateTimePrecisionOrdinal) ? null : (int?)reader.GetInt32(dateTimePrecisionOrdinal);
281282
var characterMaximumLength = reader.IsDBNull(characterMaximumLengthOrdinal) ? null : (int?)reader.GetInt32(characterMaximumLengthOrdinal);
283+
var numericPrecision = reader.IsDBNull(numericPrecisionOrdinal) ? null : (int?)reader.GetInt32(numericPrecisionOrdinal);
284+
var numericScale = reader.IsDBNull(numericScaleOrdinal) ? null : (int?)reader.GetInt32(numericScaleOrdinal);
282285

283286
DbType dbType = 0;
284287
int? precision = null;
288+
int? scale = null;
285289
int? size = null;
286290

287291
if (new[] { "timestamptz", "timestamp with time zone" }.Contains(dataTypeString))
@@ -318,6 +322,8 @@ public override Column[] GetColumns(string table)
318322
else if (dataTypeString == "numeric")
319323
{
320324
dbType = DbType.Decimal;
325+
precision = numericPrecision;
326+
scale = numericScale;
321327
}
322328
else if (dataTypeString == "real")
323329
{
@@ -375,7 +381,8 @@ public override Column[] GetColumns(string table)
375381

376382
var column = new Column(columnName, dbType)
377383
{
378-
Precision = precision
384+
Precision = precision,
385+
Scale = scale
379386
};
380387

381388
column.ColumnProperty |= isNullable ? ColumnProperty.Null : ColumnProperty.NotNull;

0 commit comments

Comments
 (0)