diff --git a/src/Migrator.Tests/Providers/Generic/Generic_CopyDataFromTableToTableBase.cs b/src/Migrator.Tests/Providers/Generic/Generic_CopyDataFromTableToTableBase.cs index 9d387916..b06e059f 100644 --- a/src/Migrator.Tests/Providers/Generic/Generic_CopyDataFromTableToTableBase.cs +++ b/src/Migrator.Tests/Providers/Generic/Generic_CopyDataFromTableToTableBase.cs @@ -10,7 +10,7 @@ namespace Migrator.Tests.Providers.Generic; public abstract class Generic_CopyDataFromTableToTableBase : TransformationProviderBase { [Test] - public void CopyDataFromTableToTable_Success() + public void CopyDataFromTableToTable_UsingOrderBy_Success() { // Arrange const string tableNameSource = "SourceTable"; @@ -72,4 +72,67 @@ public void CopyDataFromTableToTable_Success() x.Column2 == y.Column2 && x.Column3 == y.Column3)); } + + [Test] + public void CopyDataFromTableToTable_NotUsingOrderBy_Success() + { + // Arrange + const string tableNameSource = "SourceTable"; + const string columnName1Source = "SourceColumn1"; + const string columnName2Source = "SourceColumn2"; + const string columnName3Source = "SourceColumn3"; + + const string tableNameTarget = "TargetTable"; + const string columnName1Target = "TargetColumn1"; + const string columnName2Target = "TargetColumn2"; + const string columnName3Target = "TargetColumn3"; + + Provider.AddTable(tableNameSource, + new Column(columnName1Source, DbType.Int32), + new Column(columnName2Source, DbType.String), + new Column(columnName3Source, DbType.Int32) + ); + + Provider.AddTable(tableNameTarget, + new Column(columnName1Target, DbType.Int32), + new Column(columnName2Target, DbType.String), + new Column(columnName3Target, DbType.Int32) + ); + + Provider.Insert(tableNameSource, [columnName1Source, columnName2Source, columnName3Source], [2, "Hello2", 22]); + Provider.Insert(tableNameSource, [columnName1Source, columnName2Source, columnName3Source], [1, "Hello1", 11]); + + // Act + Provider.CopyDataFromTableToTable( + tableNameSource, + [columnName1Source, columnName2Source, columnName3Source], + tableNameTarget, + [columnName1Target, columnName2Target, columnName3Target]); + + // Assert + List targetRows = []; + using (var cmd = Provider.CreateCommand()) + using (var reader = Provider.Select(cmd, tableNameTarget, [columnName1Target, columnName2Target, columnName3Target])) + { + while (reader.Read()) + { + targetRows.Add(new CopyDataFromTableToTableModel + { + Column1 = reader.GetInt32(0), + Column2 = reader.GetString(1), + Column3 = reader.GetInt32(2), + }); + } + } + + List expectedTargetRows = [ + new CopyDataFromTableToTableModel{ Column1 = 1, Column2 = "Hello1", Column3 = 11 }, + new CopyDataFromTableToTableModel{ Column1 = 2, Column2 = "Hello2", Column3 = 22 }, + ]; + + Assert.That(targetRows, Is.EquivalentTo(expectedTargetRows).Using((x, y) => + x.Column1 == y.Column1 && + x.Column2 == y.Column2 && + x.Column3 == y.Column3)); + } } \ No newline at end of file diff --git a/src/Migrator/Framework/ITransformationProvider.cs b/src/Migrator/Framework/ITransformationProvider.cs index fcfcdbcc..63fd04cd 100644 --- a/src/Migrator/Framework/ITransformationProvider.cs +++ b/src/Migrator/Framework/ITransformationProvider.cs @@ -346,13 +346,14 @@ public interface ITransformationProvider : IDisposable /// /// Copies data from source table to target table using INSERT INTO...SELECT..FROM + /// Be aware that the order of and matters. /// /// /// /// /// /// Sort source by these columns. must contain the . - void CopyDataFromTableToTable(string sourceTableName, List sourceColumnNames, string targetTableName, List targetColumnNames, List orderBySourceColumns); + void CopyDataFromTableToTable(string sourceTableName, List sourceColumnNames, string targetTableName, List targetColumnNames, List orderBySourceColumns = null); /// /// Check to see if a primary key constraint exists on the table diff --git a/src/Migrator/Providers/Impl/Oracle/OracleTransformationProvider.cs b/src/Migrator/Providers/Impl/Oracle/OracleTransformationProvider.cs index d157facf..bcd0016f 100644 --- a/src/Migrator/Providers/Impl/Oracle/OracleTransformationProvider.cs +++ b/src/Migrator/Providers/Impl/Oracle/OracleTransformationProvider.cs @@ -818,7 +818,7 @@ protected override void ConfigureParameterWithValue(IDbDataParameter parameter, } } - public override void CopyDataFromTableToTable(string sourceTableName, List sourceColumnNames, string targetTableName, List targetColumnNames, List orderBySourceColumns) + public override void CopyDataFromTableToTable(string sourceTableName, List sourceColumnNames, string targetTableName, List targetColumnNames, List orderBySourceColumns = null) { orderBySourceColumns ??= []; @@ -866,8 +866,15 @@ public override void CopyDataFromTableToTable(string sourceTableName, List sqlComponents = + [ + $"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted}", + orderByComponent + ]; + + var sql = string.Join(" ", sqlComponents.Where(x => x != null)); ExecuteNonQuery(sql); } diff --git a/src/Migrator/Providers/Impl/PostgreSQL/PostgreSQLTransformationProvider.cs b/src/Migrator/Providers/Impl/PostgreSQL/PostgreSQLTransformationProvider.cs index bc1d98ab..b6abb333 100644 --- a/src/Migrator/Providers/Impl/PostgreSQL/PostgreSQLTransformationProvider.cs +++ b/src/Migrator/Providers/Impl/PostgreSQL/PostgreSQLTransformationProvider.cs @@ -862,7 +862,7 @@ public override void UpdateTargetFromSource(string tableSourceNotQuoted, string ExecuteNonQuery(sql); } - public override void CopyDataFromTableToTable(string sourceTableName, List sourceColumnNames, string targetTableName, List targetColumnNames, List orderBySourceColumns) + public override void CopyDataFromTableToTable(string sourceTableName, List sourceColumnNames, string targetTableName, List targetColumnNames, List orderBySourceColumns = null) { orderBySourceColumns ??= []; @@ -910,8 +910,15 @@ public override void CopyDataFromTableToTable(string sourceTableName, List sqlComponents = + [ + $"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted}", + orderByComponent + ]; + + var sql = string.Join(" ", sqlComponents.Where(x => x != null)); ExecuteNonQuery(sql); } diff --git a/src/Migrator/Providers/Impl/SQLite/SQLiteTransformationProvider.cs b/src/Migrator/Providers/Impl/SQLite/SQLiteTransformationProvider.cs index 81f766f2..83d5aca7 100644 --- a/src/Migrator/Providers/Impl/SQLite/SQLiteTransformationProvider.cs +++ b/src/Migrator/Providers/Impl/SQLite/SQLiteTransformationProvider.cs @@ -1777,7 +1777,7 @@ public override void AddCheckConstraint(string constraintName, string tableName, RecreateTable(sqliteTableInfo); } - public override void CopyDataFromTableToTable(string sourceTableName, List sourceColumnNames, string targetTableName, List targetColumnNames, List orderBySourceColumns) + public override void CopyDataFromTableToTable(string sourceTableName, List sourceColumnNames, string targetTableName, List targetColumnNames, List orderBySourceColumns = null) { orderBySourceColumns ??= []; @@ -1825,8 +1825,15 @@ public override void CopyDataFromTableToTable(string sourceTableName, List sqlComponents = + [ + $"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted}", + orderByComponent + ]; + + var sql = string.Join(" ", sqlComponents.Where(x => x != null)); ExecuteNonQuery(sql); } diff --git a/src/Migrator/Providers/Impl/SqlServer/SqlServerTransformationProvider.cs b/src/Migrator/Providers/Impl/SqlServer/SqlServerTransformationProvider.cs index 9f675da3..9aac8aa8 100644 --- a/src/Migrator/Providers/Impl/SqlServer/SqlServerTransformationProvider.cs +++ b/src/Migrator/Providers/Impl/SqlServer/SqlServerTransformationProvider.cs @@ -69,7 +69,7 @@ protected virtual void CreateConnection(string providerName) Dialect.RegisterProperty(ColumnProperty.CaseSensitive, "COLLATE " + collationString.Replace("_CI_", "_CS_")); } - public override void CopyDataFromTableToTable(string sourceTableName, List sourceColumnNames, string targetTableName, List targetColumnNames, List orderBySourceColumns) + public override void CopyDataFromTableToTable(string sourceTableName, List sourceColumnNames, string targetTableName, List targetColumnNames, List orderBySourceColumns = null) { orderBySourceColumns ??= []; @@ -117,8 +117,15 @@ public override void CopyDataFromTableToTable(string sourceTableName, List sqlComponents = + [ + $"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted}", + orderByComponent + ]; + + var sql = string.Join(" ", sqlComponents.Where(x => x != null)); ExecuteNonQuery(sql); } diff --git a/src/Migrator/Providers/TransformationProvider.cs b/src/Migrator/Providers/TransformationProvider.cs index 34ce47cd..2298eadd 100644 --- a/src/Migrator/Providers/TransformationProvider.cs +++ b/src/Migrator/Providers/TransformationProvider.cs @@ -2233,7 +2233,7 @@ protected void ValidateIndex(string tableName, Index index) } } - public virtual void CopyDataFromTableToTable(string sourceTableName, List sourceColumnNames, string targetTableName, List targetColumnNames, List orderBySourceColumns) + public virtual void CopyDataFromTableToTable(string sourceTableName, List sourceColumnNames, string targetTableName, List targetColumnNames, List orderBySourceColumns = null) { throw new NotImplementedException(); }