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
44 changes: 41 additions & 3 deletions src/Migrator.Tests/Providers/Generic/Generic_AddPrimaryKey.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,47 @@
using System.Collections.Generic;
using System.Data;
using System.Linq;
using DotNetProjects.Migrator.Framework;
using Migrator.Tests.Providers.Base;
using NUnit.Framework;

namespace Migrator.Tests.Providers.Generic;

[TestFixture]
[Category("SQLite")]
public class Generic_AddPrimaryTestsBase : TransformationProviderBase
{ }
public abstract class Generic_AddPrimaryTestsBase : TransformationProviderBase
{
[Test]
public void AddPrimaryKey_IdentityColumnWithData_Success()
{
// Arrange
const string tableName = "TestTable";
const string columnName1 = "TestColumn1";
const string columnName2 = "TestColumn2";

Provider.AddTable(tableName,
new Column(columnName1, DbType.Int32, property: ColumnProperty.Identity | ColumnProperty.PrimaryKey),
new Column(columnName2, DbType.String)
);

// Act
Provider.Insert(tableName, [columnName2], ["Hello"]);
Provider.Insert(tableName, [columnName2], ["Hello2"]);

// Assert

List<(int, string)> list = [];

using var cmd = Provider.CreateCommand();
using var reader = Provider.Select(cmd, tableName, [columnName1, columnName2]);

while (reader.Read())
{
list.Add((reader.GetInt32(0), reader.GetString(1)));
}

list = list.OrderBy(x => x.Item1).ToList();

Assert.That(list[0].Item1, Is.EqualTo(1));
Assert.That(list[1].Item1, Is.EqualTo(2));
}
}
1 change: 1 addition & 0 deletions src/Migrator/Framework/ITransformationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ public interface ITransformationProvider : IDisposable
/// <param name="table">The name of the table that will get the primary key.</param>
/// <param name="columns">The name of the column or columns that are in the primary key.</param>
void AddPrimaryKey(string name, string table, params string[] columns);

void AddPrimaryKeyNonClustered(string name, string table, params string[] columns);
/// <summary>
/// Add a constraint to a table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -887,9 +887,12 @@ public override void AddTable(string name, params IDbField[] fields)

base.AddTable(name, fields);

if (columns.Any(c => c.ColumnProperty == ColumnProperty.PrimaryKeyWithIdentity))
// Should be refactored
if (columns.Any(c => c.ColumnProperty == ColumnProperty.PrimaryKeyWithIdentity ||
(c.ColumnProperty.HasFlag(ColumnProperty.Identity) && c.ColumnProperty.HasFlag(ColumnProperty.PrimaryKey))))
{
var identityColumn = columns.First(c => c.ColumnProperty == ColumnProperty.PrimaryKeyWithIdentity);
var identityColumn = columns.First(c => c.ColumnProperty == ColumnProperty.PrimaryKeyWithIdentity ||
(c.ColumnProperty.HasFlag(ColumnProperty.Identity) && c.ColumnProperty.HasFlag(ColumnProperty.PrimaryKey)));

var seqTName = name.Length > 21 ? name.Substring(0, 21) : name;
if (seqTName.EndsWith("_"))
Expand Down
Loading