Skip to content

Commit

Permalink
#193 If there are check constraints that look like NOT NULL constrain…
Browse files Browse the repository at this point in the history
…ts, name the constraint in the ADD COLUMN
  • Loading branch information
martinjw committed Oct 10, 2024
1 parent 501cc14 commit 732e4d5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
18 changes: 17 additions & 1 deletion DatabaseSchemaReader/SqlGen/MigrationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public string AddTable(DatabaseTable databaseTable)
return tableGenerator.Write().Trim();
}

private string SanitizeExpression(string expression)
{
return expression.Replace("\"", "").Replace("[", "").Replace("]", "");
}

public virtual string AddColumn(DatabaseTable databaseTable, DatabaseColumn databaseColumn)
{
var tableGenerator = CreateTableGenerator(databaseTable);
Expand All @@ -84,10 +89,21 @@ public virtual string AddColumn(DatabaseTable databaseTable, DatabaseColumn data
{
addColumn += " DEFAULT CURRENT_TIMESTAMP";
}
//is there a named check constraint?
//check constraints don't have columns, so simply try to find the column name in the expression
//column name may be quoted or [bracketed] but so we do a dumb sanitize
var notNullExpression = $"{databaseColumn.Name} IS NOT NULL";
var nnc = databaseTable.CheckConstraints.FirstOrDefault(cc =>
SanitizeExpression(cc.Expression).Equals(notNullExpression, StringComparison.OrdinalIgnoreCase));
if (nnc != null)
{
addColumn += " CONSTRAINT " + Escape(nnc.Name);
}
//make sure the NOT NULL is AFTER the default
if (!addColumn.EndsWith(" NOT NULL"))
{
addColumn = addColumn.Replace(" NOT NULL ", " ") + " NOT NULL";

addColumn = $"{addColumn.Replace(" NOT NULL ", " ")} NOT NULL";
}
}
return string.Format(CultureInfo.InvariantCulture,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,34 @@ public void TestOracleNoSchema()
Assert.IsTrue(sql.StartsWith("ALTER TABLE \"Orders\" ADD \"COUNTRY\" NVARCHAR2 (20)", StringComparison.OrdinalIgnoreCase), "names should be quoted correctly");
}


[TestMethod]
public void TestOracleWithCheckConstraint()
{

//arrange
var migration = new DdlGeneratorFactory(SqlType.Oracle).MigrationGenerator();
migration.EscapeNames = false;
migration.IncludeSchema = false;

var table = MigrationCommon.CreateTestTable("Orders");
table.SchemaOwner = "dbo";
table.CheckConstraints.Add(new DatabaseConstraint
{
Name = "COUNTRY_NN",
ConstraintType = ConstraintType.Check,
Expression = "\"COUNTRY\" IS NOT NULL"
});
var column = CreateNewColumn();
column.Nullable = false;

//act
var sql = migration.AddColumn(table, column);

//assert
Assert.IsTrue(sql.StartsWith("ALTER TABLE Orders ADD COUNTRY NVARCHAR2 (20) DEFAULT '' CONSTRAINT COUNTRY_NN NOT NULL", StringComparison.OrdinalIgnoreCase), "default constraint added");
}

[TestMethod]
public void TestMySqlWithSchema()
{
Expand Down
4 changes: 2 additions & 2 deletions DatabaseSchemaViewer/DatabaseSchemaViewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Reference Include="Oracle.ManagedDataAccess, Version=4.122.21.1, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=MSIL">
<HintPath>..\packages\Oracle.ManagedDataAccess.21.14.0\lib\net462\Oracle.ManagedDataAccess.dll</HintPath>
<Reference Include="Oracle.ManagedDataAccess, Version=4.122.23.1, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=MSIL">
<HintPath>..\packages\Oracle.ManagedDataAccess.23.6.0\lib\net472\Oracle.ManagedDataAccess.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions DatabaseSchemaViewer/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@
<DbProviderFactories>
<remove invariant="Oracle.ManagedDataAccess.Client"/>
<add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"
type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.23.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</DbProviderFactories>
</system.data>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<publisherPolicy apply="no"/>
<assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/>
<bindingRedirect oldVersion="4.121.0.0 - 4.65535.65535.65535" newVersion="4.122.19.1"/>
<bindingRedirect oldVersion="4.121.0.0 - 4.65535.65535.65535" newVersion="4.122.23.1"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
Expand Down
2 changes: 1 addition & 1 deletion DatabaseSchemaViewer/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Oracle.ManagedDataAccess" version="21.14.0" targetFramework="net40" />
<package id="Oracle.ManagedDataAccess" version="23.6.0" targetFramework="net48" />
</packages>

0 comments on commit 732e4d5

Please sign in to comment.