Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ public void AddColumn_HavingColumnPropertyUniqueAndIndex_RebuildSucceeds()
CollectionAssert.AreEquivalent(indexAfter.KeyColumns, new string[] { propertyName1, propertyName2 });
}

/// <summary>
/// NOT NULL is implicitly set by the migrator for non-composite primary key
/// </summary>
[Test]
public void AddColumn_HavingNullInPrimaryKey_HasNULLAfterAddAnotherColumn()
public void AddColumn_HavingNullInPrimaryKey_HasNotNullAfterAddAnotherColumn()
{
// Arrange/Act
Provider.ExecuteNonQuery("CREATE TABLE Common_Language (LanguageID TEXT PRIMARY KEY)");
Expand All @@ -73,7 +76,7 @@ public void AddColumn_HavingNullInPrimaryKey_HasNULLAfterAddAnotherColumn()
var columnProperty = tableInfo.Columns.Single(x => x.Name == "LanguageID").ColumnProperty;

// Assert
Assert.That(script, Does.Contain("LanguageID TEXT NULL PRIMARY KEY"));
Assert.That(script, Does.Contain("LanguageID TEXT NOT NULL PRIMARY KEY"));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Data;
using System.Data.SQLite;
using System.Threading.Tasks;
using DotNetProjects.Migrator.Framework;
using DotNetProjects.Migrator.Providers.Impl.SQLite;
Expand Down Expand Up @@ -27,7 +29,10 @@ public void AddPrimaryKey_ColumnsInOtherOrderThanInColumnsList_Success()
const string tableName = "TestTable";
const string primaryKeyName = $"PK_{tableName}";

Provider.AddTable(tableName, new Column(columnName1, DbType.String), new Column(columnName2, DbType.Int32), new Column(columnName3, DbType.Int32));
Provider.AddTable(tableName,
new Column(columnName1, DbType.String),
new Column(columnName2, DbType.Int32),
new Column(columnName3, DbType.Int32));

// Act
Provider.AddPrimaryKey(name: primaryKeyName, table: tableName, columns: [columnName3, columnName2]);
Expand All @@ -37,4 +42,55 @@ public void AddPrimaryKey_ColumnsInOtherOrderThanInColumnsList_Success()

Assert.That(createTableScript, Does.Contain("PRIMARY KEY (TestColumn3, TestColumn2))"));
}

[Test]
public void AddPrimaryKey_ColumnGuidNonComposite_ThrowsOnDuplicatesAndNulls()
{
const string tableName = "MyTableName";
const string columnName1 = "Column1";
var guid = Guid.NewGuid();

// Arrange/Act
Provider.AddTable(tableName,
new Column(columnName1, DbType.Guid, ColumnProperty.PrimaryKey)
);

Provider.Insert(tableName, [columnName1], [guid]);
Assert.Throws<SQLiteException>(() => Provider.Insert(tableName, [columnName1], [guid]));
Assert.Throws<SQLiteException>(() => Provider.Insert(tableName, [columnName1], [null]));
}

[Test]
public void AddPrimaryKey_ColumnGuidComposite_ThrowsOnDuplicatesAndNulls()
{
// Arrange
const string columnName1 = "TestColumn1";
const string columnName2 = "TestColumn2";
const string tableName = "TestTable";
const string primaryKeyName = $"PK_{tableName}";
var guid = Guid.NewGuid();
var guid2 = Guid.NewGuid();

Provider.AddTable(tableName,
new Column(columnName1, DbType.Guid),
new Column(columnName2, DbType.Guid));

// Act
Provider.AddPrimaryKey(name: primaryKeyName, table: tableName, columns: [columnName1, columnName2]);

// This is a normal SQLite behavior!
// NULL != NULL
// (A, NULL) != (A, NULL)
// Duplicates! You need to set NotNull if you want to prevent it!
Provider.Insert(tableName, [columnName1, columnName2], [guid, null]);

Provider.Insert(tableName, [columnName1, columnName2], [guid, null]);
Provider.Insert(tableName, [columnName1, columnName2], [guid, null]);

Provider.Insert(tableName, [columnName1, columnName2], [null, guid]);
Provider.Insert(tableName, [columnName1, columnName2], [null, guid]);

Provider.Insert(tableName, [columnName1, columnName2], [guid2, guid2]);
Assert.Throws<SQLiteException>(() => Provider.Insert(tableName, [columnName1, columnName2], [guid2, guid2]));
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data.SQLite;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -89,7 +90,7 @@ public void AddTable_SinglePrimaryKey_ContainsNull()
var createScript = ((SQLiteTransformationProvider)Provider).GetSqlCreateTableScript(tableName);

// In SQLite an INTEGER PRIMARY KEY column is NOT NULL implicitly (see insert asserts above)
Assert.That(createScript, Is.EqualTo("CREATE TABLE MyTableName (Column1 INTEGER PRIMARY KEY, Column2 INTEGER NOT NULL)"));
Assert.That(createScript, Is.EqualTo("CREATE TABLE MyTableName (Column1 INTEGER NOT NULL PRIMARY KEY, Column2 INTEGER NOT NULL)"));

var sqliteInfo = ((SQLiteTransformationProvider)Provider).GetSQLiteTableInfo(tableName);
Assert.That(sqliteInfo.Columns.First().Name, Is.EqualTo(columnName1));
Expand Down Expand Up @@ -124,4 +125,58 @@ public void AddTable_MiscellaneousColumns_Succeeds()
Assert.That(sqliteInfo.Columns.First().Name, Is.EqualTo(columnName1));
Assert.That(sqliteInfo.Columns[1].Name, Is.EqualTo(columnName2));
}

/// <summary>
/// NOT NULL is implicitly set by SQLite
/// </summary>
[Test]
public void AddTable_GuidPrimaryKeyOneColumnPKImplicitlyUsingNotNull_ThrowsOnNullAndOnDuplicates()
{
const string tableName = "MyTableName";
const string columnName1 = "Column1";
var guid = Guid.NewGuid();

// Arrange/Act
Provider.AddTable(tableName,
new Column(columnName1, System.Data.DbType.Guid, ColumnProperty.PrimaryKey)
);

Provider.Insert(tableName, [columnName1], [guid]);
Assert.Throws<SQLiteException>(() => Provider.Insert(tableName, [columnName1], [guid]));

// The migrator sets NotNull on PrimaryKey (non composite) so this line throws.
Assert.Throws<SQLiteException>(() => Provider.Insert(tableName, [columnName1], [null]));
}

/// <summary>
/// Composite PK with Guids
/// </summary>
[Test]
public void AddTable_GuidPrimaryKeyCompositeWithGuid_DoesNotThrowOnDuplicateNULLEntries()
{
const string tableName = "MyTableName";
const string columnName1 = "Column1";
const string columnName2 = "Column2";
var guid = Guid.NewGuid();
var guid2 = Guid.NewGuid();

// Arrange/Act
Provider.AddTable(tableName,
new Column(columnName1, System.Data.DbType.Guid, ColumnProperty.PrimaryKey),
new Column(columnName2, System.Data.DbType.Guid, ColumnProperty.PrimaryKey)
);

// This is a normal SQLite behavior!
// NULL != NULL
// (A, NULL) != (A, NULL)
// Duplicates! You need to set NotNull if you want to prevent it!
Provider.Insert(tableName, [columnName1, columnName2], [guid, null]);
Provider.Insert(tableName, [columnName1, columnName2], [guid, null]);

Provider.Insert(tableName, [columnName1, columnName2], [null, guid]);
Provider.Insert(tableName, [columnName1, columnName2], [null, guid]);

Provider.Insert(tableName, [columnName1, columnName2], [guid2, guid2]);
Assert.Throws<SQLiteException>(() => Provider.Insert(tableName, [columnName1, columnName2], [guid2, guid2]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public void AddPrimaryIdentity_Succeeds()
var sql = ((SQLiteTransformationProvider)Provider).GetSqlCreateTableScript(testTableName);

// NOT NULL implicitly set in SQLite
Assert.That(sql, Does.Contain("Color1 INTEGER PRIMARY KEY"));
Assert.That(sql, Does.Contain("Color1 INTEGER NOT NULL PRIMARY KEY"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@
}

[Obsolete]
public override void AddTable(string table, string engine, string columns)

Check warning on line 896 in src/Migrator/Providers/Impl/SQLite/SQLiteTransformationProvider.cs

View workflow job for this annotation

GitHub Actions / build

Obsolete member 'SQLiteTransformationProvider.AddTable(string, string, string)' overrides non-obsolete member 'TransformationProvider.AddTable(string, string, string)'
{
throw new NotSupportedException();
}
Expand Down Expand Up @@ -1390,6 +1390,13 @@

foreach (var column in columns)
{
if (!hasCompoundPrimaryKey && column.IsPrimaryKey)
{
// We implicitly set NOT NULL for non-composite primary keys like in other RDBMS.
column.ColumnProperty = column.ColumnProperty.Clear(ColumnProperty.Null);
column.ColumnProperty = column.ColumnProperty.Set(ColumnProperty.NotNull);
}

if (hasCompoundPrimaryKey && column.IsPrimaryKey)
{
// We remove PrimaryKey here and readd it as compound later ("...PRIMARY KEY(column1,column2)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,6 @@ ORDER BY
var schemaNameOrdinal = reader.GetOrdinal("SchemaName");
var tableNameOrdinal = reader.GetOrdinal("TableName");



while (reader.Read())
{
var indexItem = new IndexItem
Expand Down
Loading