Skip to content

Commit 0aee02f

Browse files
Ja bist du narrischJa bist du narrisch
authored andcommitted
Index handling (on recreation of tables, SQLiteInfo, unique)
1 parent 80884d0 commit 0aee02f

File tree

6 files changed

+290
-133
lines changed

6 files changed

+290
-133
lines changed

src/Migrator.Tests/Providers/SQLite/Base/SQLiteTransformationProviderTestBase.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
14
using DotNetProjects.Migrator.Providers.Impl.SQLite;
5+
using Migrator.Framework;
26
using Migrator.Providers.SQLite;
37
using Migrator.Tests.Settings;
48
using NUnit.Framework;

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Migrator.Framework;
55
using Migrator.Tests.Providers.SQLite.Base;
66
using NUnit.Framework;
7+
using NUnit.Framework.Legacy;
78

89
namespace Migrator.Tests.Providers.SQLite;
910

@@ -95,32 +96,33 @@ public void AddTable_AddingColumnPropertyUnique_AddsUniqe()
9596
{
9697
// Arrange
9798
var testTableName = "MyDefaultTestTable";
99+
var propertyName1 = "Color1";
100+
var propertyName2 = "Color2";
101+
var indexName = "MyIndexName";
98102

99103
_provider.AddTable(testTableName,
100-
new Column("Color", DbType.Int32, ColumnProperty.Unique)
104+
new Column(propertyName1, DbType.Int32, ColumnProperty.Unique),
105+
new Column(propertyName2, DbType.Int32)
101106
);
102107

108+
_provider.AddIndex(indexName, testTableName, [propertyName1, propertyName2]);
103109
var tableInfoBefore = ((SQLiteTransformationProvider)_provider).GetSQLiteTableInfo(testTableName);
104110

105-
// Act
106-
107-
// TODO In progress
108-
109-
// _provider.AddPrimaryKey("MyPrimaryKeyName", testTableName, "Id", "Color");
111+
_provider.ExecuteNonQuery($"INSERT INTO {testTableName} ({propertyName1}, {propertyName2}) VALUES (1, 2)");
110112

111-
// // Assert
112-
// Assert.That(tableInfoBefore.Columns.Single(x => x.Name == "Id").ColumnProperty.HasFlag(ColumnProperty.PrimaryKey), Is.False);
113-
// Assert.That(tableInfoBefore.Columns.Single(x => x.Name == "Color").ColumnProperty.HasFlag(ColumnProperty.PrimaryKey), Is.False);
114-
// Assert.That(tableInfoBefore.Columns.Single(x => x.Name == "NotAPrimaryKey").ColumnProperty.HasFlag(ColumnProperty.PrimaryKey), Is.False);
113+
// Act
114+
((SQLiteTransformationProvider)_provider).AddPrimaryKey("MyPrimaryKeyName", testTableName, [propertyName1]);
115115

116-
// var tableInfoAfter = ((SQLiteTransformationProvider)_provider).GetSQLiteTableInfo(testTableName);
117-
// var tableNames = ((SQLiteTransformationProvider)_provider).GetTables();
116+
// Assert
117+
var tableInfoAfter = ((SQLiteTransformationProvider)_provider).GetSQLiteTableInfo(testTableName);
118118

119-
// Assert.That(tableInfoAfter.Columns.Single(x => x.Name == "Id").ColumnProperty.HasFlag(ColumnProperty.PrimaryKey), Is.True);
120-
// Assert.That(tableInfoAfter.Columns.Single(x => x.Name == "Color").ColumnProperty.HasFlag(ColumnProperty.PrimaryKey), Is.True);
121-
// Assert.That(tableInfoAfter.Columns.Single(x => x.Name == "NotAPrimaryKey").ColumnProperty.HasFlag(ColumnProperty.PrimaryKey), Is.False);
119+
Assert.That(tableInfoBefore.Columns.Single(x => x.Name == propertyName1).ColumnProperty.HasFlag(ColumnProperty.PrimaryKey), Is.False);
120+
Assert.That(tableInfoBefore.Columns.Single(x => x.Name == propertyName2).ColumnProperty.HasFlag(ColumnProperty.PrimaryKey), Is.False);
121+
Assert.That(tableInfoAfter.Columns.Single(x => x.Name == propertyName1).ColumnProperty.HasFlag(ColumnProperty.PrimaryKey), Is.True);
122+
Assert.That(tableInfoAfter.Columns.Single(x => x.Name == propertyName2).ColumnProperty.HasFlag(ColumnProperty.PrimaryKey), Is.False);
122123

123-
// // Check for intermediate table residues.
124-
// Assert.That(tableNames.Where(x => x.Contains(testTableName)), Has.Exactly(1).Items);
124+
var indexAfter = tableInfoAfter.Indexes.Single();
125+
Assert.That(indexAfter.Name, Is.EqualTo(indexName));
126+
CollectionAssert.AreEquivalent(indexAfter.KeyColumns, new string[] { propertyName1, propertyName2 });
125127
}
126128
}

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

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using DotNetProjects.Migrator.Framework;
1+
using System.Linq;
22
using DotNetProjects.Migrator.Providers.Impl.SQLite;
33
using Migrator.Framework;
44
using Migrator.Tests.Providers.SQLite.Base;
@@ -11,17 +11,123 @@ namespace Migrator.Tests.Providers.SQLite;
1111
public class SQLiteTransformationProvider_GetColumnsTests : SQLiteTransformationProviderTestBase
1212
{
1313
[Test]
14-
public void CheckForeignKeyIntegrity_IntegrityViolated_ReturnsFalse()
14+
public void GetColumns_UniqueButNotPrimaryKey_ReturnsFalse()
1515
{
16+
// Arrange
1617
const string tableName = "GetColumnsTest";
18+
_provider.AddTable(tableName, new Column("Id", System.Data.DbType.Int32, ColumnProperty.Unique));
19+
20+
// Act
21+
var columns = _provider.GetColumns(tableName);
1722

23+
// Assert
24+
Assert.That(columns.Single().ColumnProperty, Is.EqualTo(ColumnProperty.Null | ColumnProperty.Unique));
25+
}
26+
27+
[Test]
28+
public void GetColumns_PrimaryAndUnique_ReturnsFalse()
29+
{
1830
// Arrange
19-
_provider.AddTable(tableName, new Column("Id", System.Data.DbType.Int32, ColumnProperty.Unique));
31+
const string tableName = "GetColumnsTest";
32+
_provider.AddTable(tableName, new Column("Id", System.Data.DbType.Int32, ColumnProperty.Unique | ColumnProperty.PrimaryKey));
33+
34+
// Act
35+
var columns = _provider.GetColumns(tableName);
36+
37+
// Assert
38+
Assert.That(columns.Single().ColumnProperty, Is.EqualTo(
39+
ColumnProperty.Null |
40+
ColumnProperty.Unique |
41+
ColumnProperty.PrimaryKey));
42+
}
43+
44+
[Test]
45+
public void GetColumns_Primary_ColumnPropertyOk()
46+
{
47+
// Arrange
48+
const string tableName = "GetColumnsTest";
49+
_provider.AddTable(tableName, new Column("Id", System.Data.DbType.Int32, ColumnProperty.PrimaryKey));
50+
_provider.GetColumns(tableName);
51+
52+
// Act
53+
var columns = _provider.GetColumns(tableName);
54+
55+
// Assert
56+
Assert.That(columns.Single().ColumnProperty, Is.EqualTo(
57+
ColumnProperty.Null |
58+
ColumnProperty.PrimaryKey));
59+
}
60+
61+
[Test]
62+
public void GetColumns_PrimaryKey_ContainsPrimaryKey()
63+
{
64+
// Arrange
65+
const string tableName = "GetColumnsTest";
66+
_provider.AddTable(tableName, new Column("Id", System.Data.DbType.Int32, ColumnProperty.PrimaryKey));
67+
68+
_provider.GetColumns(tableName);
69+
70+
// Act
71+
var columns = _provider.GetColumns(tableName);
72+
73+
// Assert
74+
Assert.That(columns.Single().ColumnProperty, Is.EqualTo(
75+
ColumnProperty.Null |
76+
ColumnProperty.PrimaryKey));
77+
}
78+
79+
[Test]
80+
public void GetColumns_PrimaryKeyOnTwoColumns_BothColumnsHavePrimaryKeyAndAreNotNull()
81+
{
82+
// Arrange
83+
const string tableName = "GetColumnsTest";
84+
85+
_provider.AddTable(tableName,
86+
new Column("Id", System.Data.DbType.Int32, ColumnProperty.PrimaryKey),
87+
new Column("Id2", System.Data.DbType.Int32, ColumnProperty.PrimaryKey)
88+
);
89+
90+
_provider.GetColumns(tableName);
91+
92+
// Act
93+
var columns = _provider.GetColumns(tableName);
94+
95+
// Assert
96+
Assert.That(columns[0].ColumnProperty, Is.EqualTo(ColumnProperty.PrimaryKey | ColumnProperty.NotNull));
97+
Assert.That(columns[1].ColumnProperty, Is.EqualTo(ColumnProperty.PrimaryKey | ColumnProperty.NotNull));
98+
}
99+
100+
[Test]
101+
public void GetColumns_AddUniqueWithTwoColumns_NoUniqueOnColumnLevel()
102+
{
103+
// Arrange
104+
const string tableName = "GetColumnsTest";
105+
_provider.AddTable(tableName, new Column("Bla1", System.Data.DbType.Int32), new Column("Bla2", System.Data.DbType.Int32));
106+
107+
_provider.AddUniqueConstraint("Index name not used in SQLite", tableName, "Bla1, Bla2");
20108

21109
// Act
22110
var columns = _provider.GetColumns(tableName);
23111

24112
// Assert
113+
Assert.That(columns[0].ColumnProperty, Is.EqualTo(ColumnProperty.Null));
114+
}
115+
116+
[Test, Description("Add index. Should be added and detected as index")]
117+
public void GetSQLiteTableInfo_GetIndexesAndColumnsWithIndex_NoUniqueOnTheColumnsAndIndexExists()
118+
{
119+
// Arrange
120+
const string tableName = "GetColumnsTest";
121+
_provider.AddTable(tableName, new Column("Bla1", System.Data.DbType.Int32), new Column("Bla2", System.Data.DbType.Int32));
122+
_provider.AddIndex("IndexName", tableName, ["Bla1", "Bla2"]);
123+
124+
// Act
125+
var sqliteInfo = ((SQLiteTransformationProvider)_provider).GetSQLiteTableInfo(tableName);
25126

127+
// Assert
128+
Assert.That(sqliteInfo.Columns[0].ColumnProperty, Is.EqualTo(ColumnProperty.Null));
129+
Assert.That(sqliteInfo.Columns[1].ColumnProperty, Is.EqualTo(ColumnProperty.Null));
130+
Assert.That(sqliteInfo.Uniques, Is.Empty);
131+
Assert.That(sqliteInfo.Indexes.Single().Unique, Is.False);
26132
}
27133
}

src/Migrator/Framework/ColumnProperty.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,32 @@ namespace Migrator.Framework
99
public enum ColumnProperty
1010
{
1111
None = 0,
12+
1213
/// <summary>
1314
/// Null is allowable
1415
/// </summary>
1516
Null = 1,
17+
1618
/// <summary>
1719
/// Null is not allowable
1820
/// </summary>
1921
NotNull = 2,
22+
2023
/// <summary>
2124
/// Identity column, autoinc
2225
/// </summary>
2326
Identity = 4,
27+
2428
/// <summary>
2529
/// Unique Column
2630
/// </summary>
2731
Unique = 8,
32+
2833
/// <summary>
2934
/// Indexed Column
3035
/// </summary>
3136
Indexed = 16,
37+
3238
/// <summary>
3339
/// Unsigned Column
3440
/// </summary>
@@ -39,14 +45,17 @@ public enum ColumnProperty
3945
/// Foreign Key
4046
/// </summary>
4147
ForeignKey = Unsigned | Null,
48+
4249
/// <summary>
4350
/// Primary Key
4451
/// </summary>
4552
PrimaryKey = 128 | Unsigned | NotNull,
53+
4654
/// <summary>
4755
/// Primary key. Make the column a PrimaryKey and unsigned
4856
/// </summary>
4957
PrimaryKeyWithIdentity = PrimaryKey | Identity,
58+
5059
/// <summary>
5160
/// Primary key. Make the column a PrimaryKey and unsigned
5261
/// </summary>

src/Migrator/Providers/Impl/SQLite/Models/PragmaTableInfoItem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public class PragmaTableInfoItem
2323
public bool NotNull { get; set; }
2424

2525
/// <summary>
26-
/// Gets or sets the default value as SQL or NULL
26+
/// Gets or sets the default value or NULL
2727
/// </summary>
28-
public string DfltValue { get; set; }
28+
public object DfltValue { get; set; }
2929

3030
/// <summary>
3131
/// Gets or set the position in the primary key (1-based) 0 if not part of the primary key.

0 commit comments

Comments
 (0)