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
@@ -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
{
/// <summary>
/// Should return true if foreign key exists.
/// </summary>
[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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Data;
using System.Linq;
using DotNetProjects.Migrator.Framework;
using DotNetProjects.Migrator.Providers.Impl.SQLite;
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
5 changes: 4 additions & 1 deletion src/Migrator/Providers/TransformationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@
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)
Expand Down Expand Up @@ -413,7 +416,7 @@
// Remove the primary key notation if compound primary key because we'll add it back later
if (compoundPrimaryKey && column.IsPrimaryKey)
{
column.ColumnProperty = column.ColumnProperty ^ ColumnProperty.PrimaryKey;

Check warning on line 419 in src/Migrator/Providers/TransformationProvider.cs

View workflow job for this annotation

GitHub Actions / build

'ColumnProperty.PrimaryKey' is obsolete: 'Use AddPrimaryKey instead.'
column.ColumnProperty = column.ColumnProperty | ColumnProperty.NotNull; // PK is always not-null
}

Expand Down Expand Up @@ -531,9 +534,9 @@

public virtual void ChangeColumn(string table, Column column)
{
var isUniqueSet = column.ColumnProperty.IsSet(ColumnProperty.Unique);

Check warning on line 537 in src/Migrator/Providers/TransformationProvider.cs

View workflow job for this annotation

GitHub Actions / build

'ColumnProperty.Unique' is 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.'

column.ColumnProperty = column.ColumnProperty.Clear(ColumnProperty.Unique);

Check warning on line 539 in src/Migrator/Providers/TransformationProvider.cs

View workflow job for this annotation

GitHub Actions / build

'ColumnProperty.Unique' is 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.'

var mapper = _dialect.GetAndMapColumnProperties(column);

Expand Down Expand Up @@ -949,7 +952,7 @@
#endif

string sqlText;
var file = (new System.Uri(assembly.CodeBase)).AbsolutePath;

Check warning on line 955 in src/Migrator/Providers/TransformationProvider.cs

View workflow job for this annotation

GitHub Actions / build

'Assembly.CodeBase' is obsolete: 'Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility. Use Assembly.Location.' (https://aka.ms/dotnet-warnings/SYSLIB0012)
using (var reader = File.OpenText(file))
{
sqlText = reader.ReadToEnd();
Expand Down
Loading