Skip to content

Commit 18ee3ff

Browse files
JaBistDuNarrischJaBistDuNarrisch
authored andcommitted
CopyDataFromTableToTable OrderBy is now optional
1 parent 1acb08b commit 18ee3ff

File tree

7 files changed

+103
-11
lines changed

7 files changed

+103
-11
lines changed

src/Migrator.Tests/Providers/Generic/Generic_CopyDataFromTableToTableBase.cs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Migrator.Tests.Providers.Generic;
1010
public abstract class Generic_CopyDataFromTableToTableBase : TransformationProviderBase
1111
{
1212
[Test]
13-
public void CopyDataFromTableToTable_Success()
13+
public void CopyDataFromTableToTable_UsingOrderBy_Success()
1414
{
1515
// Arrange
1616
const string tableNameSource = "SourceTable";
@@ -72,4 +72,67 @@ public void CopyDataFromTableToTable_Success()
7272
x.Column2 == y.Column2 &&
7373
x.Column3 == y.Column3));
7474
}
75+
76+
[Test]
77+
public void CopyDataFromTableToTable_NotUsingOrderBy_Success()
78+
{
79+
// Arrange
80+
const string tableNameSource = "SourceTable";
81+
const string columnName1Source = "SourceColumn1";
82+
const string columnName2Source = "SourceColumn2";
83+
const string columnName3Source = "SourceColumn3";
84+
85+
const string tableNameTarget = "TargetTable";
86+
const string columnName1Target = "TargetColumn1";
87+
const string columnName2Target = "TargetColumn2";
88+
const string columnName3Target = "TargetColumn3";
89+
90+
Provider.AddTable(tableNameSource,
91+
new Column(columnName1Source, DbType.Int32),
92+
new Column(columnName2Source, DbType.String),
93+
new Column(columnName3Source, DbType.Int32)
94+
);
95+
96+
Provider.AddTable(tableNameTarget,
97+
new Column(columnName1Target, DbType.Int32),
98+
new Column(columnName2Target, DbType.String),
99+
new Column(columnName3Target, DbType.Int32)
100+
);
101+
102+
Provider.Insert(tableNameSource, [columnName1Source, columnName2Source, columnName3Source], [2, "Hello2", 22]);
103+
Provider.Insert(tableNameSource, [columnName1Source, columnName2Source, columnName3Source], [1, "Hello1", 11]);
104+
105+
// Act
106+
Provider.CopyDataFromTableToTable(
107+
tableNameSource,
108+
[columnName1Source, columnName2Source, columnName3Source],
109+
tableNameTarget,
110+
[columnName1Target, columnName2Target, columnName3Target]);
111+
112+
// Assert
113+
List<CopyDataFromTableToTableModel> targetRows = [];
114+
using (var cmd = Provider.CreateCommand())
115+
using (var reader = Provider.Select(cmd, tableNameTarget, [columnName1Target, columnName2Target, columnName3Target]))
116+
{
117+
while (reader.Read())
118+
{
119+
targetRows.Add(new CopyDataFromTableToTableModel
120+
{
121+
Column1 = reader.GetInt32(0),
122+
Column2 = reader.GetString(1),
123+
Column3 = reader.GetInt32(2),
124+
});
125+
}
126+
}
127+
128+
List<CopyDataFromTableToTableModel> expectedTargetRows = [
129+
new CopyDataFromTableToTableModel{ Column1 = 1, Column2 = "Hello1", Column3 = 11 },
130+
new CopyDataFromTableToTableModel{ Column1 = 2, Column2 = "Hello2", Column3 = 22 },
131+
];
132+
133+
Assert.That(targetRows, Is.EquivalentTo(expectedTargetRows).Using<CopyDataFromTableToTableModel>((x, y) =>
134+
x.Column1 == y.Column1 &&
135+
x.Column2 == y.Column2 &&
136+
x.Column3 == y.Column3));
137+
}
75138
}

src/Migrator/Framework/ITransformationProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,14 @@ public interface ITransformationProvider : IDisposable
346346

347347
/// <summary>
348348
/// Copies data from source table to target table using INSERT INTO...SELECT..FROM
349+
/// Be aware that the order of <paramref name="sourceColumnNames"/> and <paramref name="targetColumnNames"/> matters.
349350
/// </summary>
350351
/// <param name="sourceTableName"></param>
351352
/// <param name="sourceColumnNames"></param>
352353
/// <param name="targetTableName"></param>
353354
/// <param name="targetColumnNames"></param>
354355
/// <param name="orderBySourceColumns">Sort source by these columns. <paramref name="sourceColumnNames"/> must contain the <paramref name="orderBySourceColumns"/>.
355-
void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns);
356+
void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns = null);
356357

357358
/// <summary>
358359
/// Check to see if a primary key constraint exists on the table

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ protected override void ConfigureParameterWithValue(IDbDataParameter parameter,
818818
}
819819
}
820820

821-
public override void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns)
821+
public override void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns = null)
822822
{
823823
orderBySourceColumns ??= [];
824824

@@ -866,8 +866,15 @@ public override void CopyDataFromTableToTable(string sourceTableName, List<strin
866866
var targetColumnsJoined = string.Join(", ", targetColumnNamesQuoted);
867867
var orderBySourceColumnsJoined = string.Join(", ", orderBySourceColumnsQuoted);
868868

869+
var orderByComponent = !string.IsNullOrWhiteSpace(orderBySourceColumnsJoined) ? $"ORDER BY {orderBySourceColumnsJoined}" : null;
869870

870-
var sql = $"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted} ORDER BY {orderBySourceColumnsJoined}";
871+
List<string> sqlComponents =
872+
[
873+
$"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted}",
874+
orderByComponent
875+
];
876+
877+
var sql = string.Join(" ", sqlComponents.Where(x => x != null));
871878
ExecuteNonQuery(sql);
872879
}
873880

src/Migrator/Providers/Impl/PostgreSQL/PostgreSQLTransformationProvider.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ public override void UpdateTargetFromSource(string tableSourceNotQuoted, string
862862
ExecuteNonQuery(sql);
863863
}
864864

865-
public override void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns)
865+
public override void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns = null)
866866
{
867867
orderBySourceColumns ??= [];
868868

@@ -910,8 +910,15 @@ public override void CopyDataFromTableToTable(string sourceTableName, List<strin
910910
var targetColumnsJoined = string.Join(", ", targetColumnNamesQuoted);
911911
var orderBySourceColumnsJoined = string.Join(", ", orderBySourceColumnsQuoted);
912912

913+
var orderByComponent = !string.IsNullOrWhiteSpace(orderBySourceColumnsJoined) ? $"ORDER BY {orderBySourceColumnsJoined}" : null;
913914

914-
var sql = $"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted} ORDER BY {orderBySourceColumnsJoined}";
915+
List<string> sqlComponents =
916+
[
917+
$"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted}",
918+
orderByComponent
919+
];
920+
921+
var sql = string.Join(" ", sqlComponents.Where(x => x != null));
915922
ExecuteNonQuery(sql);
916923
}
917924

src/Migrator/Providers/Impl/SQLite/SQLiteTransformationProvider.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,7 +1777,7 @@ public override void AddCheckConstraint(string constraintName, string tableName,
17771777
RecreateTable(sqliteTableInfo);
17781778
}
17791779

1780-
public override void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns)
1780+
public override void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns = null)
17811781
{
17821782
orderBySourceColumns ??= [];
17831783

@@ -1825,8 +1825,15 @@ public override void CopyDataFromTableToTable(string sourceTableName, List<strin
18251825
var targetColumnsJoined = string.Join(", ", targetColumnNamesQuoted);
18261826
var orderBySourceColumnsJoined = string.Join(", ", orderBySourceColumnsQuoted);
18271827

1828+
var orderByComponent = !string.IsNullOrWhiteSpace(orderBySourceColumnsJoined) ? $"ORDER BY {orderBySourceColumnsJoined}" : null;
18281829

1829-
var sql = $"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted} ORDER BY {orderBySourceColumnsJoined}";
1830+
List<string> sqlComponents =
1831+
[
1832+
$"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted}",
1833+
orderByComponent
1834+
];
1835+
1836+
var sql = string.Join(" ", sqlComponents.Where(x => x != null));
18301837
ExecuteNonQuery(sql);
18311838
}
18321839

src/Migrator/Providers/Impl/SqlServer/SqlServerTransformationProvider.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected virtual void CreateConnection(string providerName)
6969
Dialect.RegisterProperty(ColumnProperty.CaseSensitive, "COLLATE " + collationString.Replace("_CI_", "_CS_"));
7070
}
7171

72-
public override void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns)
72+
public override void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns = null)
7373
{
7474
orderBySourceColumns ??= [];
7575

@@ -117,8 +117,15 @@ public override void CopyDataFromTableToTable(string sourceTableName, List<strin
117117
var targetColumnsJoined = string.Join(", ", targetColumnNamesQuoted);
118118
var orderBySourceColumnsJoined = string.Join(", ", orderBySourceColumnsQuoted);
119119

120+
var orderByComponent = !string.IsNullOrWhiteSpace(orderBySourceColumnsJoined) ? $"ORDER BY {orderBySourceColumnsJoined}" : null;
120121

121-
var sql = $"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted} ORDER BY {orderBySourceColumnsJoined}";
122+
List<string> sqlComponents =
123+
[
124+
$"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted}",
125+
orderByComponent
126+
];
127+
128+
var sql = string.Join(" ", sqlComponents.Where(x => x != null));
122129
ExecuteNonQuery(sql);
123130
}
124131

src/Migrator/Providers/TransformationProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2233,7 +2233,7 @@ protected void ValidateIndex(string tableName, Index index)
22332233
}
22342234
}
22352235

2236-
public virtual void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns)
2236+
public virtual void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns = null)
22372237
{
22382238
throw new NotImplementedException();
22392239
}

0 commit comments

Comments
 (0)