Skip to content

Commit 3f61417

Browse files
JaBistDuNarrischJaBistDuNarrisch
authored andcommitted
Added checkConstraint test
1 parent 03febdd commit 3f61417

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

src/Migrator.Tests/Providers/SQLite/SQLiteTransformationProvider_GetCheckConstraintsTests.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Data.SQLite;
32
using DotNetProjects.Migrator.Framework;
43
using DotNetProjects.Migrator.Providers.Impl.SQLite;
@@ -42,20 +41,4 @@ public void GetCheckConstraints_AddCheckConstraintsViaAddTable_CreatesTableCorre
4241
var createScript = ((SQLiteTransformationProvider)Provider).GetSqlCreateTableScript(tableName);
4342
Assert.That(createScript, Is.EqualTo("CREATE TABLE MyTableName (MyColumnName INTEGER NULL, CONSTRAINT MyCheckConstraint1 CHECK (MyColumnName > 10), CONSTRAINT MyCheckConstraint2 CHECK (MyColumnName < 100))"));
4443
}
45-
46-
[Test]
47-
public void CheckForeignKeyIntegrity_IntegrityOk_ReturnsTrue()
48-
{
49-
// Arrange
50-
AddTableWithPrimaryKey();
51-
Provider.ExecuteNonQuery("INSERT INTO Test (Id, name) VALUES (1, 'my name')");
52-
Provider.ExecuteNonQuery("INSERT INTO TestTwo (TestId) VALUES (1)");
53-
Provider.AddForeignKey(name: "FKName", childTable: "TestTwo", childColumn: "TestId", parentTable: "Test", parentColumn: "Id", constraint: ForeignKeyConstraintType.Cascade);
54-
55-
// Act
56-
var result = ((SQLiteTransformationProvider)Provider).CheckForeignKeyIntegrity();
57-
58-
// Assert
59-
Assert.That(result, Is.True);
60-
}
6144
}

src/Migrator.Tests/Providers/SQLite/SQLiteTransformationProvider_RemoveColumnTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,23 @@ public void RemoveColumn_HavingAForeignKeyPointingFromTableToParentAndForeignKey
265265
var valid = ((SQLiteTransformationProvider)Provider).CheckForeignKeyIntegrity();
266266
Assert.That(valid, Is.True);
267267
}
268+
269+
[Test]
270+
public void RemoveColumn_ColumnExistsInCheckConstraintString_Throws()
271+
{
272+
const string tableName = "MyTableName";
273+
const string columnName = "MyColumnName";
274+
const string checkConstraint1 = "MyCheckConstraint1";
275+
276+
// Arrange
277+
Provider.AddTable(tableName,
278+
new Column(columnName, System.Data.DbType.Int32),
279+
new CheckConstraint(checkConstraint1, $"{columnName} > 10")
280+
);
281+
282+
var checkConstraints = ((SQLiteTransformationProvider)Provider).GetCheckConstraints(tableName);
283+
284+
// Act/Assert
285+
Assert.Throws<MigrationException>(() => Provider.RemoveColumn(tableName, columnName));
286+
}
268287
}

src/Migrator/Providers/Impl/SQLite/SQLiteTransformationProvider.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
using System.Linq;
88
using System.Text;
99
using System.Text.RegularExpressions;
10-
using System.Xml.Linq;
1110
using ForeignKeyConstraint = DotNetProjects.Migrator.Framework.ForeignKeyConstraint;
1211
using Index = DotNetProjects.Migrator.Framework.Index;
12+
using DotNetProjects.Migrator.Framework.Extensions;
1313

1414
namespace DotNetProjects.Migrator.Providers.Impl.SQLite;
1515

@@ -407,9 +407,11 @@ public override void RemoveColumn(string tableName, string column)
407407

408408
var sqliteInfoMainTable = GetSQLiteTableInfo(tableName);
409409

410-
if (sqliteInfoMainTable.CheckConstraints.Any(x => x.CheckConstraintString.Equals(column, StringComparison.OrdinalIgnoreCase)))
410+
var checkConstraints = sqliteInfoMainTable.CheckConstraints;
411+
412+
if (checkConstraints.Any(x => x.CheckConstraintString.Contains(column, StringComparison.OrdinalIgnoreCase)))
411413
{
412-
throw new Exception("A check constraint contains the column you want to remove. Remove the check constraint first");
414+
throw new MigrationException("A check constraint contains the column you want to remove. Remove the check constraint first");
413415
}
414416

415417
if (!sqliteInfoMainTable.ColumnMappings.Any(x => x.OldName == column))

0 commit comments

Comments
 (0)