Skip to content

Commit f2a0110

Browse files
Ja bist du narrischJa bist du narrisch
authored andcommitted
Implemented RemoveAllConstraints (whithout CHECK constraint)
1 parent 3ea24a0 commit f2a0110

File tree

5 files changed

+94
-12
lines changed

5 files changed

+94
-12
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void AddPrimaryKey_HavingColumnPropertyUniqueAndIndex_RebuildSucceeds()
101101
var indexName = "MyIndexName";
102102

103103
Provider.AddTable(testTableName,
104-
new Column(propertyName1, DbType.Int32, ColumnProperty.Unique),
104+
new Column(propertyName1, DbType.Int32, ColumnProperty.Unique | ColumnProperty.NotNull),
105105
new Column(propertyName2, DbType.Int32)
106106
);
107107

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public void GetColumns_PrimaryAndUnique_ReturnsFalse()
3636

3737
// Assert
3838
Assert.That(columns.Single().ColumnProperty, Is.EqualTo(
39-
ColumnProperty.Null |
4039
ColumnProperty.Unique |
4140
ColumnProperty.PrimaryKey));
4241
}
@@ -53,9 +52,7 @@ public void GetColumns_Primary_ColumnPropertyOk()
5352
var columns = Provider.GetColumns(tableName);
5453

5554
// Assert
56-
Assert.That(columns.Single().ColumnProperty, Is.EqualTo(
57-
ColumnProperty.Null |
58-
ColumnProperty.PrimaryKey));
55+
Assert.That(columns.Single().ColumnProperty, Is.EqualTo(ColumnProperty.PrimaryKey));
5956
}
6057

6158
[Test]
@@ -71,9 +68,7 @@ public void GetColumns_PrimaryKey_ContainsPrimaryKey()
7168
var columns = Provider.GetColumns(tableName);
7269

7370
// Assert
74-
Assert.That(columns.Single().ColumnProperty, Is.EqualTo(
75-
ColumnProperty.Null |
76-
ColumnProperty.PrimaryKey));
71+
Assert.That(columns.Single().ColumnProperty, Is.EqualTo(ColumnProperty.PrimaryKey));
7772
}
7873

7974
[Test]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Data;
3+
using System.Linq;
4+
using DotNetProjects.Migrator.Providers.Impl.SQLite;
5+
using Migrator.Framework;
6+
using Migrator.Tests.Providers.SQLite.Base;
7+
using NUnit.Framework;
8+
using NUnit.Framework.Legacy;
9+
10+
namespace Migrator.Tests.Providers.SQLite;
11+
12+
[TestFixture]
13+
[Category("SQLite")]
14+
public class SQLiteTransformationProvider_RemoveAllConstraints : SQLiteTransformationProviderTestBase
15+
{
16+
[Test]
17+
public void RemoveColumn_HavingNoCompositeIndexAndNoCompositeUniqueConstraint_Succeeds()
18+
{
19+
// Arrange
20+
const string testTableName = "MyDefaultTestTable";
21+
const string propertyName1 = "Color1";
22+
const string propertyName2 = "Color2";
23+
const string propertyName3 = "Color3";
24+
const string indexName = "MyIndexName";
25+
26+
Provider.AddTable(testTableName,
27+
new Column(propertyName1, DbType.Int32, ColumnProperty.PrimaryKey),
28+
new Column(propertyName2, DbType.Int32, ColumnProperty.Unique),
29+
new Column(propertyName3, DbType.Int32, ColumnProperty.Unique)
30+
);
31+
32+
Provider.AddIndex(indexName, testTableName, [propertyName1]);
33+
var tableInfoBefore = ((SQLiteTransformationProvider)Provider).GetSQLiteTableInfo(testTableName);
34+
35+
Provider.ExecuteNonQuery($"INSERT INTO {testTableName} ({propertyName1}, {propertyName2}) VALUES (1, 2)");
36+
37+
// Act
38+
Provider.RemoveAllConstraints(testTableName);
39+
Provider.ExecuteNonQuery($"INSERT INTO {testTableName} ({propertyName1}) VALUES (2)");
40+
41+
// Assert
42+
using var command = Provider.GetCommand();
43+
using var reader = Provider.ExecuteQuery(command, $"SELECT COUNT(*) as Count from {testTableName}");
44+
reader.Read();
45+
var count = reader.GetInt32(reader.GetOrdinal("Count"));
46+
Assert.That(count, Is.EqualTo(2));
47+
48+
var tableInfoAfter = ((SQLiteTransformationProvider)Provider).GetSQLiteTableInfo(testTableName);
49+
var sqlAfter = ((SQLiteTransformationProvider)Provider).GetSqlCreateTableScript(testTableName);
50+
51+
Assert.That(tableInfoBefore.Columns.Single(x => x.Name == propertyName1).ColumnProperty.HasFlag(ColumnProperty.PrimaryKey), Is.True);
52+
Assert.That(tableInfoBefore.Columns.Single(x => x.Name == propertyName2).ColumnProperty.HasFlag(ColumnProperty.Unique), Is.True);
53+
Assert.That(tableInfoBefore.Columns.Single(x => x.Name == propertyName3).ColumnProperty.HasFlag(ColumnProperty.Unique), Is.True);
54+
55+
Assert.That(tableInfoAfter.Columns.Single(x => x.Name == propertyName1).ColumnProperty.HasFlag(ColumnProperty.PrimaryKey), Is.False);
56+
Assert.That(tableInfoAfter.Columns.Single(x => x.Name == propertyName2).ColumnProperty.HasFlag(ColumnProperty.Unique), Is.False);
57+
Assert.That(tableInfoAfter.Columns.Single(x => x.Name == propertyName3).ColumnProperty.HasFlag(ColumnProperty.Unique), Is.False);
58+
59+
Assert.That(sqlAfter.Contains("unique", StringComparison.OrdinalIgnoreCase), Is.False);
60+
61+
var indexAfter = tableInfoAfter.Indexes.Single();
62+
63+
Assert.That(indexAfter.Name, Is.EqualTo(indexName));
64+
CollectionAssert.AreEquivalent(indexAfter.KeyColumns, new string[] { propertyName1 });
65+
}
66+
}

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,12 +754,12 @@ public override List<string> GetDatabases()
754754

755755
public override bool ConstraintExists(string table, string name)
756756
{
757-
return false;
757+
throw new NotSupportedException("SQLite does not offer constraint names e.g. for unique, check constraints. You need to use alternative ways.");
758758
}
759759

760760
public override string[] GetConstraints(string table)
761761
{
762-
return [];
762+
throw new NotSupportedException("SQLite does not offer constraint names e.g. for unique, check constraints You need to drop them using alternative ways.");
763763
}
764764

765765
public override string[] GetTables()
@@ -1042,6 +1042,27 @@ protected override string GetPrimaryKeyConstraintName(string table)
10421042
throw new NotImplementedException();
10431043
}
10441044

1045+
public override void RemoveAllConstraints(string table)
1046+
{
1047+
RemovePrimaryKey(table);
1048+
1049+
var sqliteTableInfo = GetSQLiteTableInfo(table);
1050+
1051+
// Remove unique constraints
1052+
sqliteTableInfo.Uniques = [];
1053+
1054+
foreach (var column in sqliteTableInfo.Columns)
1055+
{
1056+
column.ColumnProperty &= ~ColumnProperty.PrimaryKey;
1057+
column.ColumnProperty &= ~ColumnProperty.Unique;
1058+
}
1059+
1060+
// TODO CHECK is not implemented yet
1061+
// https://github.com/dotnetprojects/Migrator.NET/issues/64
1062+
1063+
RecreateTable(sqliteTableInfo);
1064+
}
1065+
10451066
public override void RemovePrimaryKey(string tableName)
10461067
{
10471068
if (!TableExists(tableName))

src/Migrator/Providers/TransformationProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,15 @@ public virtual void RemoveConstraint(string table, string name)
225225
{
226226
if (TableExists(table) && ConstraintExists(table, name))
227227
{
228-
ExecuteNonQuery(String.Format("ALTER TABLE {0} DROP CONSTRAINT {1}", QuoteTableNameIfRequired(table), QuoteConstraintNameIfRequired(name)));
228+
ExecuteNonQuery(string.Format("ALTER TABLE {0} DROP CONSTRAINT {1}", QuoteTableNameIfRequired(table), QuoteConstraintNameIfRequired(name)));
229229
}
230230
}
231231

232232
public virtual void RemoveAllConstraints(string table)
233233
{
234234
foreach (var constraint in GetConstraints(table))
235235
{
236-
this.RemoveConstraint(table, constraint);
236+
RemoveConstraint(table, constraint);
237237
}
238238
}
239239

0 commit comments

Comments
 (0)