Skip to content

Commit a1d1fbb

Browse files
Ja bist du narrischJa bist du narrisch
authored andcommitted
PrimaryKey in ColumnProperty changed
1 parent fa5cdf5 commit a1d1fbb

18 files changed

+152
-33
lines changed

src/Migrator.Tests/Providers/Base/TransformationProviderSimpleBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void AddDefaultTable()
4848
{
4949
Provider.AddTable("TestTwo",
5050
new Column("Id", DbType.Int32, ColumnProperty.PrimaryKey),
51-
new Column("TestId", DbType.Int32, ColumnProperty.ForeignKey)
51+
new Column("TestId", DbType.Int32)
5252
);
5353
}
5454

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using DotNetProjects.Migrator.Providers.Impl.SQLite;
2+
using NUnit.Framework;
3+
4+
namespace Migrator.Tests.Providers.SQLite.SQLiteReader;
5+
6+
[TestFixture]
7+
[Category("SQLite")]
8+
public class SQLiteReaderTests
9+
{
10+
private SQLiteCreateTableScriptReader _sqliteCreateTableScriptReader = new SQLiteCreateTableScriptReader();
11+
12+
[Test]
13+
public void GetParenthesisContent()
14+
{
15+
// Arrange
16+
var testScript = "CREATE TABLE \"TestTwo\" (Id INTEGER NOT NULL PRIMARY KEY, TestId INTEGER NULL, CONSTRAINT FKName FOREIGN KEY (TestId) REFERENCES Test(IdNew))";
17+
18+
// Act
19+
var parenthesisContent = _sqliteCreateTableScriptReader.GetParenthesisContent(testScript);
20+
21+
// Assert
22+
Assert.That(parenthesisContent, Is.EqualTo("Id INTEGER NOT NULL PRIMARY KEY, TestId INTEGER NULL, CONSTRAINT FKName FOREIGN KEY (TestId) REFERENCES Test(IdNew)"));
23+
}
24+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace Migrator.Tests.Providers.SQLite;
1212
[Category("SQLite")]
1313
public class SQLiteTransformationProvider_AddColumnTests : SQLiteTransformationProviderTestBase
1414
{
15-
1615
/// <summary>
1716
/// We use a NULL column as new column here. NOT NULL will fail as expected. The user should handle that on his own.
1817
/// </summary>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public void AddForeignKey()
3030
Assert.That(foreignKeyConstraints.Single().ParentTable, Is.EqualTo("Test"));
3131
Assert.That(foreignKeyConstraints.Single().ChildColumns.Single(), Is.EqualTo("TestId"));
3232
Assert.That(foreignKeyConstraints.Single().ParentColumns.Single(), Is.EqualTo("Id"));
33-
// Cascade is not supported
33+
34+
// Cascade is not supported in this migrator see https://github.com/dotnetprojects/Migrator.NET/issues/33
35+
// TODO add cascade tests as soon as it is supported.
3436

3537
Assert.That(tableSQLCreateScript, Does.Contain("CREATE TABLE \"TestTwo\""));
3638
Assert.That(tableSQLCreateScript, Does.Contain(", CONSTRAINT FKName FOREIGN KEY (TestId) REFERENCES Test(Id))"));

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,4 @@ public void AddTable_MiscellaneousColumns_Succeeds()
113113
Assert.That(sqliteInfo.Columns.First().Name, Is.EqualTo(columnName1));
114114
Assert.That(sqliteInfo.Columns[1].Name, Is.EqualTo(columnName2));
115115
}
116-
117-
118-
}
116+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Data;
2+
using DotNetProjects.Migrator.Providers.Impl.SQLite;
3+
using Migrator.Framework;
4+
using Migrator.Tests.Providers.SQLite.Base;
5+
using NUnit.Framework;
6+
7+
namespace Migrator.Tests.Providers.SQLite;
8+
9+
[TestFixture]
10+
[Category("SQLite")]
11+
public class SQLiteTransformationProvider_PropertyColumnIdentityTests : SQLiteTransformationProviderTestBase
12+
{
13+
[Test]
14+
public void AddPrimaryIdentity_Succeeds()
15+
{
16+
// Arrange
17+
const string testTableName = "MyDefaultTestTable";
18+
const string propertyName1 = "Color1";
19+
const string propertyName2 = "Color2";
20+
21+
Provider.AddTable(testTableName,
22+
new Column(propertyName1, DbType.Int32, ColumnProperty.PrimaryKey | ColumnProperty.Identity),
23+
new Column(propertyName2, DbType.Int32, ColumnProperty.NotNull)
24+
);
25+
26+
var sql = ((SQLiteTransformationProvider)Provider).GetSqlCreateTableScript(testTableName);
27+
28+
Assert.That(sql, Does.Contain("Color1 INTEGER NOT NULL PRIMARY KEY"));
29+
}
30+
}

src/Migrator/Framework/Column.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public bool IsPrimaryKey
164164
{
165165
get { return (ColumnProperty & ColumnProperty.PrimaryKey) == ColumnProperty.PrimaryKey; }
166166
}
167+
167168
public bool IsPrimaryKeyNonClustered
168169
{
169170
get { return (ColumnProperty & ColumnProperty.PrimaryKeyNonClustered) == ColumnProperty.PrimaryKeyNonClustered; }

src/Migrator/Framework/ColumnProperty.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,55 +13,59 @@ public enum ColumnProperty
1313
/// <summary>
1414
/// Null is allowable
1515
/// </summary>
16-
Null = 1,
16+
Null = 1 << 0,
1717

1818
/// <summary>
1919
/// Null is not allowable
2020
/// </summary>
21-
NotNull = 2,
21+
NotNull = 1 << 1,
2222

2323
/// <summary>
2424
/// Identity column, autoinc
2525
/// </summary>
26-
Identity = 4,
26+
Identity = 1 << 2,
2727

2828
/// <summary>
2929
/// Unique Column. This is marked being obsolete since you cannot add a name for the constraint which makes it difficult to remove the constraint again.
3030
/// </summary>
3131
[Obsolete("Use method 'AddUniqueConstraint' instead. This is marked being obsolete since you cannot add a name for the constraint which makes it difficult to remove the constraint again.")]
32-
Unique = 8,
32+
Unique = 1 << 3,
3333

3434
/// <summary>
3535
/// Indexed Column
3636
/// </summary>
37-
Indexed = 16,
37+
Indexed = 1 << 4,
3838

3939
/// <summary>
40-
/// Unsigned Column
40+
/// Unsigned Column. Not used in SQLite (there is only)
4141
/// </summary>
42-
Unsigned = 32,
42+
Unsigned = 1 << 5,
4343

44-
CaseSensitive = 64,
44+
/// <summary>
45+
/// CaseSensitive. Currently only used in SQLite, MySQL and SQL Server
46+
/// </summary>
47+
CaseSensitive = 1 << 6,
4548

4649
/// <summary>
4750
/// Foreign Key
4851
/// </summary>
49-
ForeignKey = Unsigned | Null,
52+
[Obsolete("Use method 'AddForeignKey' instead. The flag does not make sense on column level.")]
53+
ForeignKey = 1 << 7,
5054

5155
/// <summary>
5256
/// Primary Key
5357
/// </summary>
54-
PrimaryKey = 128 | Unsigned | NotNull,
58+
PrimaryKey = 1 << 8,
5559

5660
/// <summary>
57-
/// Primary key. Make the column a PrimaryKey and unsigned
61+
/// Primary key with identity. Will be removed soon.
5862
/// </summary>
59-
PrimaryKeyWithIdentity = PrimaryKey | Identity,
63+
PrimaryKeyWithIdentity = 1 << 9 | PrimaryKey | Identity,
6064

6165
/// <summary>
62-
/// Primary key. Make the column a PrimaryKey and unsigned
66+
/// Primary key. Will be removed soon.
6367
/// </summary>
64-
PrimaryKeyNonClustered = 256 | PrimaryKey
68+
PrimaryKeyNonClustered = 1 << 10 | PrimaryKey
6569
}
6670

6771
public static class ColumnPropertyExtensions

src/Migrator/Framework/SchemaBuilder/SchemaBuilder.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ public SchemaBuilder WithSize(int size)
5353

5454
public IForeignKeyOptions AsForeignKey()
5555
{
56-
_currentColumn.ColumnProperty = ColumnProperty.ForeignKey;
57-
5856
return this;
5957
}
6058

src/Migrator/Providers/ColumnPropertiesMapper.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public virtual void MapColumnPropertiesWithoutDefault(Column column)
143143

144144
AddForeignKey(column, vals);
145145

146-
columnSql = String.Join(" ", vals.ToArray());
146+
columnSql = string.Join(" ", vals.ToArray());
147147
}
148148

149149
protected virtual void AddCaseSensitive(Column column, List<string> vals)
@@ -154,11 +154,14 @@ protected virtual void AddCaseSensitive(Column column, List<string> vals)
154154
protected virtual void AddDefaultValue(Column column, List<string> vals)
155155
{
156156
if (column.DefaultValue != null)
157+
{
157158
vals.Add(dialect.Default(column.DefaultValue));
159+
}
158160
}
159161

160162
protected virtual void AddForeignKey(Column column, List<string> vals)
161163
{
164+
// TODO Does that really make sense?
162165
AddValueIfSelected(column, ColumnProperty.ForeignKey, vals);
163166
}
164167

@@ -204,13 +207,17 @@ protected virtual void AddNotNull(Column column, List<string> vals)
204207
protected virtual void AddUnsigned(Column column, List<string> vals)
205208
{
206209
if (dialect.IsUnsignedCompatible(column.Type))
210+
{
207211
AddValueIfSelected(column, ColumnProperty.Unsigned, vals);
212+
}
208213
}
209214

210215
protected virtual void AddIdentity(Column column, List<string> vals)
211216
{
212217
if (!dialect.IdentityNeedsType)
218+
{
213219
AddValueIfSelected(column, ColumnProperty.Identity, vals);
220+
}
214221
}
215222

216223
protected virtual void AddType(List<string> vals)

0 commit comments

Comments
 (0)