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
Expand Up @@ -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";
Expand Down Expand Up @@ -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<CopyDataFromTableToTableModel> 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<CopyDataFromTableToTableModel> expectedTargetRows = [
new CopyDataFromTableToTableModel{ Column1 = 1, Column2 = "Hello1", Column3 = 11 },
new CopyDataFromTableToTableModel{ Column1 = 2, Column2 = "Hello2", Column3 = 22 },
];

Assert.That(targetRows, Is.EquivalentTo(expectedTargetRows).Using<CopyDataFromTableToTableModel>((x, y) =>
x.Column1 == y.Column1 &&
x.Column2 == y.Column2 &&
x.Column3 == y.Column3));
}
}
3 changes: 2 additions & 1 deletion src/Migrator/Framework/ITransformationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,14 @@ public interface ITransformationProvider : IDisposable

/// <summary>
/// Copies data from source table to target table using INSERT INTO...SELECT..FROM
/// Be aware that the order of <paramref name="sourceColumnNames"/> and <paramref name="targetColumnNames"/> matters.
/// </summary>
/// <param name="sourceTableName"></param>
/// <param name="sourceColumnNames"></param>
/// <param name="targetTableName"></param>
/// <param name="targetColumnNames"></param>
/// <param name="orderBySourceColumns">Sort source by these columns. <paramref name="sourceColumnNames"/> must contain the <paramref name="orderBySourceColumns"/>.
void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns);
void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns = null);

/// <summary>
/// Check to see if a primary key constraint exists on the table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ protected override void ConfigureParameterWithValue(IDbDataParameter parameter,
}
}

public override void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns)
public override void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns = null)
{
orderBySourceColumns ??= [];

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

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

var sql = $"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted} ORDER BY {orderBySourceColumnsJoined}";
List<string> sqlComponents =
[
$"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted}",
orderByComponent
];

var sql = string.Join(" ", sqlComponents.Where(x => x != null));
ExecuteNonQuery(sql);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ public override void UpdateTargetFromSource(string tableSourceNotQuoted, string
ExecuteNonQuery(sql);
}

public override void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns)
public override void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns = null)
{
orderBySourceColumns ??= [];

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

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

var sql = $"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted} ORDER BY {orderBySourceColumnsJoined}";
List<string> sqlComponents =
[
$"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted}",
orderByComponent
];

var sql = string.Join(" ", sqlComponents.Where(x => x != null));
ExecuteNonQuery(sql);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@
}

[Obsolete]
public override void AddTable(string table, string engine, string columns)

Check warning on line 896 in src/Migrator/Providers/Impl/SQLite/SQLiteTransformationProvider.cs

View workflow job for this annotation

GitHub Actions / build

Obsolete member 'SQLiteTransformationProvider.AddTable(string, string, string)' overrides non-obsolete member 'TransformationProvider.AddTable(string, string, string)'
{
throw new NotSupportedException();
}
Expand Down Expand Up @@ -1777,7 +1777,7 @@
RecreateTable(sqliteTableInfo);
}

public override void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns)
public override void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns = null)
{
orderBySourceColumns ??= [];

Expand Down Expand Up @@ -1825,8 +1825,15 @@
var targetColumnsJoined = string.Join(", ", targetColumnNamesQuoted);
var orderBySourceColumnsJoined = string.Join(", ", orderBySourceColumnsQuoted);

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

var sql = $"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted} ORDER BY {orderBySourceColumnsJoined}";
List<string> sqlComponents =
[
$"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted}",
orderByComponent
];

var sql = string.Join(" ", sqlComponents.Where(x => x != null));
ExecuteNonQuery(sql);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns)
public override void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns = null)
{
orderBySourceColumns ??= [];

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

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

var sql = $"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted} ORDER BY {orderBySourceColumnsJoined}";
List<string> sqlComponents =
[
$"INSERT INTO {targetTableNameQuoted} ({targetColumnsJoined}) SELECT {sourceColumnsJoined} FROM {sourceTableNameQuoted}",
orderByComponent
];

var sql = string.Join(" ", sqlComponents.Where(x => x != null));
ExecuteNonQuery(sql);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Migrator/Providers/TransformationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2233,7 +2233,7 @@ protected void ValidateIndex(string tableName, Index index)
}
}

public virtual void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns)
public virtual void CopyDataFromTableToTable(string sourceTableName, List<string> sourceColumnNames, string targetTableName, List<string> targetColumnNames, List<string> orderBySourceColumns = null)
{
throw new NotImplementedException();
}
Expand Down
Loading