Skip to content

Commit 5d3b31d

Browse files
JaBistDuNarrischJaBistDuNarrisch
authored andcommitted
Merge branch 'get-columns-postgre' into boolean-default-values-postgre
2 parents cfe6397 + e24fe5d commit 5d3b31d

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void GetColumns_DefaultValues_Succeeds()
2929
const string int32ColumnName1 = "int32column1";
3030
const string int64ColumnName1 = "int64column1";
3131
const string stringColumnName1 = "stringcolumn1";
32+
const string binaryColumnName1 = "binarycolumn1";
3233

3334
// Should be extended by remaining types
3435
Provider.AddTable(testTableName,
@@ -42,7 +43,8 @@ public void GetColumns_DefaultValues_Succeeds()
4243

4344
new Column(int32ColumnName1, DbType.Int32, defaultValue: 43),
4445
new Column(int64ColumnName1, DbType.Int64, defaultValue: 88),
45-
new Column(stringColumnName1, DbType.String, defaultValue: "Hello")
46+
new Column(stringColumnName1, DbType.String, defaultValue: "Hello"),
47+
new Column(binaryColumnName1, DbType.Binary, defaultValue: new byte[] { 12, 32, 34 })
4648
);
4749

4850
// Act
@@ -57,6 +59,7 @@ public void GetColumns_DefaultValues_Succeeds()
5759
var int32Column1 = columns.Single(x => x.Name == int32ColumnName1);
5860
var int64Column1 = columns.Single(x => x.Name == int64ColumnName1);
5961
var stringColumn1 = columns.Single(x => x.Name == stringColumnName1);
62+
var binarycolumn1 = columns.Single(x => x.Name == binaryColumnName1);
6063

6164
Assert.That(dateTimeColumn1.DefaultValue, Is.EqualTo(dateTimeDefaultValue));
6265
Assert.That(dateTimeColumn2.DefaultValue, Is.EqualTo(dateTimeDefaultValue));
@@ -66,6 +69,7 @@ public void GetColumns_DefaultValues_Succeeds()
6669
Assert.That(int32Column1.DefaultValue, Is.EqualTo(43));
6770
Assert.That(int64Column1.DefaultValue, Is.EqualTo(88));
6871
Assert.That(stringColumn1.DefaultValue, Is.EqualTo("Hello"));
72+
Assert.That(binarycolumn1.DefaultValue, Is.EqualTo(new byte[] { 12, 32, 34 }));
6973
}
7074

7175
[TestCase()]

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public void GetColumns_DataTypeResolveSucceeds()
2323
const string int64ColumnName1 = "int64column1";
2424
const string stringColumnName1 = "stringcolumn1";
2525
const string stringColumnName2 = "stringcolumn2";
26+
const string binaryColumnName1 = "binarycolumn";
2627

2728
// Should be extended by remaining types
2829
Provider.AddTable(testTableName,
@@ -34,7 +35,8 @@ public void GetColumns_DataTypeResolveSucceeds()
3435
new Column(int32ColumnName1, DbType.Int32),
3536
new Column(int64ColumnName1, DbType.Int64),
3637
new Column(stringColumnName1, DbType.String),
37-
new Column(stringColumnName2, DbType.String) { Size = 30 }
38+
new Column(stringColumnName2, DbType.String) { Size = 30 },
39+
new Column(binaryColumnName1, DbType.Binary)
3840
);
3941

4042
// Act
@@ -49,6 +51,7 @@ public void GetColumns_DataTypeResolveSucceeds()
4951
var int64column1 = columns.Single(x => x.Name == int64ColumnName1);
5052
var stringColumn1 = columns.Single(x => x.Name == stringColumnName1);
5153
var stringColumn2 = columns.Single(x => x.Name == stringColumnName2);
54+
var binaryColumn1 = columns.Single(x => x.Name == binaryColumnName1);
5255

5356

5457
// Assert
@@ -66,5 +69,6 @@ public void GetColumns_DataTypeResolveSucceeds()
6669
Assert.That(stringColumn1.Type, Is.EqualTo(DbType.String));
6770
Assert.That(stringColumn2.Type, Is.EqualTo(DbType.String));
6871
Assert.That(stringColumn2.Size, Is.EqualTo(30));
72+
Assert.That(binaryColumn1.Type, Is.EqualTo(DbType.Binary));
6973
}
7074
}

src/Migrator/Providers/Dialect.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ public virtual string Default(object defaultValue)
363363
// .ToString("N") does not exist in old .NET version
364364
defaultValue = Convert.ToString(defaultValue, CultureInfo.InvariantCulture);
365365
}
366+
else if (defaultValue is byte[] byteArray)
367+
{
368+
var convertedString = BitConverter.ToString(byteArray).Replace("-", "").ToLower();
369+
defaultValue = $"'\\x{convertedString}'";
370+
}
366371

367372
return string.Format("DEFAULT {0}", defaultValue);
368373
}

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,13 @@ public override Column[] GetColumns(string table)
368368
dbType = DbType.String;
369369
size = characterMaximumLength;
370370
}
371+
else if (dataTypeString == "bytea")
372+
{
373+
dbType = DbType.Binary;
374+
}
371375
else if (dataTypeString == "character" || dataTypeString.StartsWith("character("))
372376
{
373-
throw new NotSupportedException("Data type 'character' detected. We do not support 'character'. Use 'text' or 'character varying' instead");
377+
throw new NotSupportedException("Data type 'character' detected. 'character' is not supported. Use 'text' or 'character varying' instead.");
374378
}
375379
else
376380
{
@@ -427,7 +431,7 @@ public override Column[] GetColumns(string table)
427431

428432
if (!match.Success)
429433
{
430-
throw new Exception("Postgre default value for date time: We expect single quotes around the date time string.");
434+
throw new Exception("Postgre default value for date time: Single quotes around the date time string are expected.");
431435
}
432436

433437
var timeString = match.Value;
@@ -450,7 +454,7 @@ public override Column[] GetColumns(string table)
450454

451455
if (!match.Success)
452456
{
453-
throw new Exception("Postgre default value for uniqueidentifier: We expected single quotes around the Guid string.");
457+
throw new Exception("Postgre default value for uniqueidentifier: Single quotes around the Guid string are expected.");
454458
}
455459

456460
column.DefaultValue = Guid.Parse(match.Value);
@@ -472,7 +476,7 @@ public override Column[] GetColumns(string table)
472476

473477
if (!match.Success)
474478
{
475-
throw new Exception("Postgre default value for date time: We expected single quotes around the date time string.");
479+
throw new Exception("Postgre default value for date time: Single quotes around the date time string are expected.");
476480
}
477481

478482
column.DefaultValue = match.Value;
@@ -482,6 +486,37 @@ public override Column[] GetColumns(string table)
482486
throw new NotImplementedException();
483487
}
484488
}
489+
else if (column.Type == DbType.Binary)
490+
{
491+
if (defaultValueString.StartsWith("'"))
492+
{
493+
var match = stripSingleQuoteRegEx.Match(defaultValueString);
494+
495+
if (!match.Success)
496+
{
497+
throw new Exception("Postgre default value for bytea: Single quotes around the bytea string are expected.");
498+
}
499+
500+
var singleQuoteString = match.Value;
501+
502+
if (!singleQuoteString.StartsWith("\\x"))
503+
{
504+
throw new Exception(@"Postgre \x notation expected.");
505+
}
506+
507+
var hexString = singleQuoteString.Substring(2);
508+
509+
// Not available in old .NET version: Convert.FromHexString(hexString);
510+
511+
column.DefaultValue = Enumerable.Range(0, hexString.Length / 2)
512+
.Select(x => Convert.ToByte(hexString.Substring(x * 2, 2), 16))
513+
.ToArray();
514+
}
515+
else
516+
{
517+
throw new NotImplementedException();
518+
}
519+
}
485520
else
486521
{
487522
throw new NotImplementedException();

src/Migrator/Providers/TransformationProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,7 @@ public virtual void AddColumnDefaultValue(string table, string column, object de
17881788
{
17891789
if (defaultValueDateTime.Kind != DateTimeKind.Utc)
17901790
{
1791-
throw new Exception("We only accept UTC values as default DateTime values.");
1791+
throw new Exception("Only UTC values are accepted as default DateTime values.");
17921792
}
17931793
}
17941794

0 commit comments

Comments
 (0)