From 991dc6d9d9276533085ac6269d93511574cee4ff Mon Sep 17 00:00:00 2001 From: Spartak Timchev Date: Fri, 18 Sep 2020 13:02:42 +0300 Subject: [PATCH 1/6] Fix for Issue #106 --- EF6.PG/NpgsqlMigrationSqlGenerator.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/EF6.PG/NpgsqlMigrationSqlGenerator.cs b/EF6.PG/NpgsqlMigrationSqlGenerator.cs index c3f26e0..09686b1 100644 --- a/EF6.PG/NpgsqlMigrationSqlGenerator.cs +++ b/EF6.PG/NpgsqlMigrationSqlGenerator.cs @@ -551,6 +551,11 @@ void AppendColumn(ColumnModel column, StringBuilder sql) break; } } + else if (column.IsNullable != null && !column.IsNullable.Value && column.ClrDefaultValue != null) + { + sql.Append(" DEFAULT "); + AppendValue(column.ClrDefaultValue, sql); + } } void AppendColumnType(ColumnModel column, StringBuilder sql, bool setSerial) From 3bcdb98152f2f97e0b1d633b2b497381cee4a687 Mon Sep 17 00:00:00 2001 From: Spartak Timchev Date: Sat, 19 Sep 2020 23:59:49 +0300 Subject: [PATCH 2/6] Update EF6.PG/NpgsqlMigrationSqlGenerator.cs Co-authored-by: Shay Rojansky --- EF6.PG/NpgsqlMigrationSqlGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EF6.PG/NpgsqlMigrationSqlGenerator.cs b/EF6.PG/NpgsqlMigrationSqlGenerator.cs index 09686b1..3883b1f 100644 --- a/EF6.PG/NpgsqlMigrationSqlGenerator.cs +++ b/EF6.PG/NpgsqlMigrationSqlGenerator.cs @@ -551,7 +551,7 @@ void AppendColumn(ColumnModel column, StringBuilder sql) break; } } - else if (column.IsNullable != null && !column.IsNullable.Value && column.ClrDefaultValue != null) + else if (column.IsNullable == false && column.ClrDefaultValue != null) { sql.Append(" DEFAULT "); AppendValue(column.ClrDefaultValue, sql); From 55bef3994515d0f18727aeeb1469fb0fb0b622b6 Mon Sep 17 00:00:00 2001 From: Spartak Timchev Date: Tue, 22 Sep 2020 13:29:29 +0300 Subject: [PATCH 3/6] Update NpgsqlMigrationSqlGenerator.cs --- EF6.PG/NpgsqlMigrationSqlGenerator.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/EF6.PG/NpgsqlMigrationSqlGenerator.cs b/EF6.PG/NpgsqlMigrationSqlGenerator.cs index 3883b1f..e41601a 100644 --- a/EF6.PG/NpgsqlMigrationSqlGenerator.cs +++ b/EF6.PG/NpgsqlMigrationSqlGenerator.cs @@ -292,6 +292,11 @@ protected virtual void Convert(AlterColumnOperation alterColumnOperation) throw new NotImplementedException("Not supporting creating IsIdentity for " + alterColumnOperation.Column.Type); } } + else if(alterColumnOperation.Column.IsNullable==false && alterColumnOperation.Column.ClrDefaultValue != null) + { + sql.Append(" SET DEFAULT "); + AppendValue(alterColumnOperation.Column.ClrDefaultValue, sql); + } else sql.Append(" DROP DEFAULT"); AddStatment(sql); From 2e4316df47965534e2371c080b5f75703e5c85cd Mon Sep 17 00:00:00 2001 From: Spartak Timchev Date: Tue, 22 Sep 2020 13:45:56 +0300 Subject: [PATCH 4/6] Tests added Added tests for AddColumnOperation and AlterColumnOperation for non-nullable columns without explicitly specified default value. --- EF6.PG.Tests/EntityFrameworkMigrationTests.cs | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/EF6.PG.Tests/EntityFrameworkMigrationTests.cs b/EF6.PG.Tests/EntityFrameworkMigrationTests.cs index 3fde9fb..54e8796 100644 --- a/EF6.PG.Tests/EntityFrameworkMigrationTests.cs +++ b/EF6.PG.Tests/EntityFrameworkMigrationTests.cs @@ -217,6 +217,20 @@ public void DatabaseExistsCreateDelete() [Test] public void AddColumnOperation() + { + var operations = new List(); + operations.Add(new AddColumnOperation("tableName", new ColumnModel(PrimitiveTypeKind.Double) + { + Name = "columnName", + IsNullable = false + })); + var statments = new NpgsqlMigrationSqlGenerator().Generate(operations, _backendVersion.ToString()); + Assert.AreEqual(1, statments.Count()); + Assert.AreEqual("ALTER TABLE \"tableName\" ADD \"columnName\" float8 NOT NULL DEFAULT 0", statments.ElementAt(0).Sql); + } + + [Test] + public void AddColumnOperationNullable() { var operations = new List(); operations.Add(new AddColumnOperation("tableName", new ColumnModel(PrimitiveTypeKind.Double) @@ -225,7 +239,7 @@ public void AddColumnOperation() })); var statments = new NpgsqlMigrationSqlGenerator().Generate(operations, _backendVersion.ToString()); Assert.AreEqual(1, statments.Count()); - Assert.AreEqual("ALTER TABLE \"tableName\" ADD \"columnName\" float8", statments.ElementAt(0).Sql); + Assert.AreEqual("ALTER TABLE \"tableName\" ADD \"columnName\" float8 DEFAULT 0", statments.ElementAt(0).Sql); } [Test] @@ -262,7 +276,23 @@ public void AlterColumnOperation() var operations = new List(); operations.Add(new AlterColumnOperation("tableName", new ColumnModel(PrimitiveTypeKind.Double) { - Name = "columnName" + Name = "columnName", + IsNullable = false + }, false)); + var statments = new NpgsqlMigrationSqlGenerator().Generate(operations, _backendVersion.ToString()); + Assert.AreEqual(3, statments.Count()); + Assert.AreEqual("ALTER TABLE \"tableName\" ALTER COLUMN \"columnName\" TYPE float8", statments.ElementAt(0).Sql); + Assert.AreEqual("ALTER TABLE \"tableName\" ALTER COLUMN \"columnName\" SET NOT NULL", statments.ElementAt(1).Sql); + Assert.AreEqual("ALTER TABLE \"tableName\" ALTER COLUMN \"columnName\" SET DEFAULT 0", statments.ElementAt(2).Sql); + } + + [Test] + public void AlterColumnOperationNullable() + { + var operations = new List(); + operations.Add(new AlterColumnOperation("tableName", new ColumnModel(PrimitiveTypeKind.Double) + { + Name = "columnName", }, false)); var statments = new NpgsqlMigrationSqlGenerator().Generate(operations, _backendVersion.ToString()); Assert.AreEqual(3, statments.Count()); From d7304f6e854c6c40e4319dbdbfc7b3f5c6914dc0 Mon Sep 17 00:00:00 2001 From: Spartak Timchev Date: Tue, 22 Sep 2020 14:27:04 +0300 Subject: [PATCH 5/6] Small error on AddColumnOperationNullable --- EF6.PG.Tests/EntityFrameworkMigrationTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EF6.PG.Tests/EntityFrameworkMigrationTests.cs b/EF6.PG.Tests/EntityFrameworkMigrationTests.cs index 54e8796..0178c3b 100644 --- a/EF6.PG.Tests/EntityFrameworkMigrationTests.cs +++ b/EF6.PG.Tests/EntityFrameworkMigrationTests.cs @@ -239,7 +239,7 @@ public void AddColumnOperationNullable() })); var statments = new NpgsqlMigrationSqlGenerator().Generate(operations, _backendVersion.ToString()); Assert.AreEqual(1, statments.Count()); - Assert.AreEqual("ALTER TABLE \"tableName\" ADD \"columnName\" float8 DEFAULT 0", statments.ElementAt(0).Sql); + Assert.AreEqual("ALTER TABLE \"tableName\" ADD \"columnName\" float8", statments.ElementAt(0).Sql); } [Test] From 04670efdb095fc905d6de9f0d944f46954ee8334 Mon Sep 17 00:00:00 2001 From: Spartak Timchev Date: Tue, 22 Sep 2020 14:44:05 +0300 Subject: [PATCH 6/6] Fix for CreateTableOperation test --- EF6.PG.Tests/EntityFrameworkMigrationTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EF6.PG.Tests/EntityFrameworkMigrationTests.cs b/EF6.PG.Tests/EntityFrameworkMigrationTests.cs index 0178c3b..224bafe 100644 --- a/EF6.PG.Tests/EntityFrameworkMigrationTests.cs +++ b/EF6.PG.Tests/EntityFrameworkMigrationTests.cs @@ -365,7 +365,7 @@ public void CreateTableOperation() Assert.AreEqual("CREATE SCHEMA IF NOT EXISTS \"someSchema\"", statments.ElementAt(0).Sql); else Assert.AreEqual("CREATE SCHEMA \"someSchema\"", statments.ElementAt(0).Sql); - Assert.AreEqual("CREATE TABLE \"someSchema\".\"someTable\"(\"SomeString\" varchar(233) NOT NULL,\"AnotherString\" text,\"SomeBytes\" bytea,\"SomeLong\" serial8,\"SomeDateTime\" timestamp)", statments.ElementAt(1).Sql); + Assert.AreEqual("CREATE TABLE \"someSchema\".\"someTable\"(\"SomeString\" varchar(233) NOT NULL DEFAULT '',\"AnotherString\" text,\"SomeBytes\" bytea,\"SomeLong\" serial8,\"SomeDateTime\" timestamp)", statments.ElementAt(1).Sql); } [Test]