diff --git a/src/Migrator.Tests/Providers/Generic/Generic_ConstraintExistsBase.cs b/src/Migrator.Tests/Providers/Generic/Generic_ConstraintExistsBase.cs new file mode 100644 index 00000000..a55e1aaa --- /dev/null +++ b/src/Migrator.Tests/Providers/Generic/Generic_ConstraintExistsBase.cs @@ -0,0 +1,38 @@ +using System.Data; +using DotNetProjects.Migrator.Framework; +using Migrator.Tests.Providers.Base; +using NUnit.Framework; + +namespace Migrator.Tests.Providers.Generic; + +[TestFixture] +public abstract class Generic_ConstraintExistsBase : TransformationProviderBase +{ + /// + /// Should return true if foreign key exists. + /// + [Test] + public void ConstraintExists_ForeignKeyExists_ReturnsTrue() + { + // Arrange + var tableName = "Task"; + var fkName = "FK_Task_TaskGroup"; + + Provider.AddTable("Task", + new Column(name: "Id", type: DbType.Int32, property: ColumnProperty.PrimaryKey), + new Column(name: "TaskGroupId", type: DbType.Int32, property: ColumnProperty.Null) + ); + + Provider.AddTable("TaskGroup", + new Column(name: "Id", type: DbType.Int32, property: ColumnProperty.PrimaryKey) + ); + + Provider.AddForeignKey(name: fkName, childTable: tableName, childColumn: "TaskGroupId", parentTable: "TaskGroup", parentColumn: "Id"); + + // Act + var result = Provider.ConstraintExists(table: tableName, name: fkName); + + // Assert + Assert.That(result, Is.True); + } +} diff --git a/src/Migrator.Tests/Providers/OracleProvider/OracleTransformationProvider_ConstraintExistsTests.cs b/src/Migrator.Tests/Providers/OracleProvider/OracleTransformationProvider_ConstraintExistsTests.cs new file mode 100644 index 00000000..cd5f9a04 --- /dev/null +++ b/src/Migrator.Tests/Providers/OracleProvider/OracleTransformationProvider_ConstraintExistsTests.cs @@ -0,0 +1,16 @@ +using System.Threading.Tasks; +using Migrator.Tests.Providers.Generic; +using NUnit.Framework; + +namespace Migrator.Tests.Providers.OracleProvider; + +[TestFixture] +[Category("Oracle")] +public class OracleTransformationProvider_ConstraintExists_Tests : Generic_ConstraintExistsBase +{ + [SetUp] + public async Task SetUpAsync() + { + await BeginOracleTransactionAsync(); + } +} \ No newline at end of file diff --git a/src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_ConstraintExists.cs b/src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_ConstraintExists.cs new file mode 100644 index 00000000..358859be --- /dev/null +++ b/src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_ConstraintExists.cs @@ -0,0 +1,16 @@ +using System.Threading.Tasks; +using Migrator.Tests.Providers.Generic; +using NUnit.Framework; + +namespace Migrator.Tests.Providers.PostgreSQL; + +[TestFixture] +[Category("Postgre")] +public class PostgreSQLTransformationProvider_ConstraintExistsTests : Generic_ConstraintExistsBase +{ + [SetUp] + public async Task SetUpAsync() + { + await BeginPostgreSQLTransactionAsync(); + } +} diff --git a/src/Migrator.Tests/Providers/SQLServer/SQLServerTransformationProvider_ConstraintExistsTests.cs b/src/Migrator.Tests/Providers/SQLServer/SQLServerTransformationProvider_ConstraintExistsTests.cs new file mode 100644 index 00000000..5be60184 --- /dev/null +++ b/src/Migrator.Tests/Providers/SQLServer/SQLServerTransformationProvider_ConstraintExistsTests.cs @@ -0,0 +1,16 @@ +using System.Threading.Tasks; +using Migrator.Tests.Providers.Generic; +using NUnit.Framework; + +namespace Migrator.Tests.Providers.SQLServer; + +[TestFixture] +[Category("SqlServer")] +public class SQLServerTransformationProvider_ConstraintExistsTests : Generic_ConstraintExistsBase +{ + [SetUp] + public async Task SetUpAsync() + { + await BeginSQLServerTransactionAsync(); + } +} diff --git a/src/Migrator.Tests/Providers/SQLite/SQLiteTransformationProvider_AddForeignKeyTests.cs b/src/Migrator.Tests/Providers/SQLite/SQLiteTransformationProvider_AddForeignKeyTests.cs index 17b0221c..ad2f3f6c 100644 --- a/src/Migrator.Tests/Providers/SQLite/SQLiteTransformationProvider_AddForeignKeyTests.cs +++ b/src/Migrator.Tests/Providers/SQLite/SQLiteTransformationProvider_AddForeignKeyTests.cs @@ -1,3 +1,4 @@ +using System.Data; using System.Linq; using DotNetProjects.Migrator.Framework; using DotNetProjects.Migrator.Providers.Impl.SQLite; @@ -66,4 +67,26 @@ public void AddForeignKey_RenameParentColumWithForeignKeyAndData_ForeignKeyPoint var result = ((SQLiteTransformationProvider)Provider).CheckForeignKeyIntegrity(); Assert.That(result, Is.True); } + + [Test] + public void AddForeignKey_3_Success() + { + Provider.AddTable("Task", + new Column(name: "BinId", type: DbType.Int32, property: ColumnProperty.NotNull), + new Column(name: "CreationTimeStamp", type: DbType.DateTime2, property: ColumnProperty.NotNull), + new Column(name: "EstimatedPickTime", type: DbType.Int32, property: ColumnProperty.Null), + new Column(name: "Id", type: DbType.Int32, property: ColumnProperty.NotNull), + new Column(name: "Item", type: DbType.Int32, property: ColumnProperty.Null), + new Column(name: "Order", type: DbType.Int32, property: ColumnProperty.Null), + new Column(name: "TaskGroupId", type: DbType.Int32, property: ColumnProperty.Null) + ); + + Provider.AddTable("TaskGroup", + new Column(name: "CreationTimeStamp", type: DbType.DateTime2, property: ColumnProperty.NotNull), + new Column(name: "Id", type: DbType.Int32) + ); + + // TODO CK add more columns. + Provider.AddForeignKey(name: "FK_Task_TaskGroup", childTable: "Task", childColumn: "TaskGroupId", parentTable: "TaskGroup", parentColumn: "Id"); + } } diff --git a/src/Migrator.Tests/Providers/SQLite/SQLiteTransformationProvider_ConstraintExistsTests.cs b/src/Migrator.Tests/Providers/SQLite/SQLiteTransformationProvider_ConstraintExistsTests.cs new file mode 100644 index 00000000..a53f6f8e --- /dev/null +++ b/src/Migrator.Tests/Providers/SQLite/SQLiteTransformationProvider_ConstraintExistsTests.cs @@ -0,0 +1,16 @@ +using System.Threading.Tasks; +using Migrator.Tests.Providers.SQLite.Base; +using NUnit.Framework; + +namespace Migrator.Tests.Providers.SQLite; + +[TestFixture] +[Category("SQLite")] +public class SQLiteTransformationProvider_ConstraintExistsTests : SQLiteTransformationProviderTestBase +{ + [SetUp] + public async Task SetUpAsync() + { + await BeginSQLiteTransactionAsync(); + } +} diff --git a/src/Migrator/Providers/TransformationProvider.cs b/src/Migrator/Providers/TransformationProvider.cs index e46ac8c9..a44c25fc 100644 --- a/src/Migrator/Providers/TransformationProvider.cs +++ b/src/Migrator/Providers/TransformationProvider.cs @@ -240,7 +240,10 @@ public virtual string[] GetConstraints(string table) public virtual Column GetColumnByName(string table, string columnName) { var columns = GetColumns(table); - return columns.First(column => column.Name.Equals(columnName, StringComparison.OrdinalIgnoreCase)); + var column = columns.FirstOrDefault(x => x.Name.Equals(columnName, StringComparison.OrdinalIgnoreCase)) ?? + throw new Exception($"Cannot find column '{columnName}' in table '{table}'"); + + return column; } public virtual int GetColumnContentSize(string table, string columnName)