Skip to content

Commit 1acb08b

Browse files
JaBistDuNarrischJaBistDuNarrisch
authored andcommitted
Updated Oracle Primary+Identity
1 parent ecc9ae5 commit 1acb08b

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed
Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,47 @@
1+
using System.Collections.Generic;
2+
using System.Data;
3+
using System.Linq;
4+
using DotNetProjects.Migrator.Framework;
15
using Migrator.Tests.Providers.Base;
26
using NUnit.Framework;
37

48
namespace Migrator.Tests.Providers.Generic;
59

610
[TestFixture]
7-
[Category("SQLite")]
8-
public class Generic_AddPrimaryTestsBase : TransformationProviderBase
9-
{ }
11+
public abstract class Generic_AddPrimaryTestsBase : TransformationProviderBase
12+
{
13+
[Test]
14+
public void AddPrimaryKey_IdentityColumnWithData_Success()
15+
{
16+
// Arrange
17+
const string tableName = "TestTable";
18+
const string columnName1 = "TestColumn1";
19+
const string columnName2 = "TestColumn2";
20+
21+
Provider.AddTable(tableName,
22+
new Column(columnName1, DbType.Int32, property: ColumnProperty.Identity | ColumnProperty.PrimaryKey),
23+
new Column(columnName2, DbType.String)
24+
);
25+
26+
// Act
27+
Provider.Insert(tableName, [columnName2], ["Hello"]);
28+
Provider.Insert(tableName, [columnName2], ["Hello2"]);
29+
30+
// Assert
31+
32+
List<(int, string)> list = [];
33+
34+
using var cmd = Provider.CreateCommand();
35+
using var reader = Provider.Select(cmd, tableName, [columnName1, columnName2]);
36+
37+
while (reader.Read())
38+
{
39+
list.Add((reader.GetInt32(0), reader.GetString(1)));
40+
}
41+
42+
list = list.OrderBy(x => x.Item1).ToList();
43+
44+
Assert.That(list[0].Item1, Is.EqualTo(1));
45+
Assert.That(list[1].Item1, Is.EqualTo(2));
46+
}
47+
}

src/Migrator/Framework/ITransformationProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ public interface ITransformationProvider : IDisposable
272272
/// <param name="table">The name of the table that will get the primary key.</param>
273273
/// <param name="columns">The name of the column or columns that are in the primary key.</param>
274274
void AddPrimaryKey(string name, string table, params string[] columns);
275+
275276
void AddPrimaryKeyNonClustered(string name, string table, params string[] columns);
276277
/// <summary>
277278
/// Add a constraint to a table

src/Migrator/Providers/Impl/Oracle/OracleTransformationProvider.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -887,9 +887,12 @@ public override void AddTable(string name, params IDbField[] fields)
887887

888888
base.AddTable(name, fields);
889889

890-
if (columns.Any(c => c.ColumnProperty == ColumnProperty.PrimaryKeyWithIdentity))
890+
// Should be refactored
891+
if (columns.Any(c => c.ColumnProperty == ColumnProperty.PrimaryKeyWithIdentity ||
892+
(c.ColumnProperty.HasFlag(ColumnProperty.Identity) && c.ColumnProperty.HasFlag(ColumnProperty.PrimaryKey))))
891893
{
892-
var identityColumn = columns.First(c => c.ColumnProperty == ColumnProperty.PrimaryKeyWithIdentity);
894+
var identityColumn = columns.First(c => c.ColumnProperty == ColumnProperty.PrimaryKeyWithIdentity ||
895+
(c.ColumnProperty.HasFlag(ColumnProperty.Identity) && c.ColumnProperty.HasFlag(ColumnProperty.PrimaryKey)));
893896

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

0 commit comments

Comments
 (0)