Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Migration Strategy to account for all PK values #2022

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [8.4.0] - Unreleased

- Add Ordering to Filters
- Update Migration strategy to account for all Primary Keys when moving from staging -> live

## [8.3.1] - 2024-10-22

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
namespace Rdmp.Core.DataLoad.Engine.Migration.QueryBuilding;

/// <summary>
/// Migrates from STAGING to LIVE a single table (with a MigrationColumnSet). This is an UPSERT (new replaces old) operation achieved (in SQL) with MERGE and
/// UPDATE (based on primary key). Both tables must be on the same server. A MERGE sql statement will be created using LiveMigrationQueryHelper and executed
/// within a transaction.
/// Migrates from STAGING to LIVE a single table (with a MigrationColumnSet). This is an UPSERT (new replaces old)
/// operation achieved (in SQL) with MERGE and
/// UPDATE (based on primary key). Both tables must be on the same server. A MERGE sql statement will be created
/// using LiveMigrationQueryHelper and executed
/// within a transaction.
/// </summary>
public class OverwriteMigrationStrategy : DatabaseMigrationStrategy
{
Expand Down Expand Up @@ -97,8 +99,9 @@ CrossDatabaseMergeCommandTo..ToTable.Age is null
columnsToMigrate.DestinationTable.GetFullyQualifiedName()))));

sbInsert.AppendLine("WHERE");
sbInsert.AppendLine(
$"{columnsToMigrate.DestinationTable.GetFullyQualifiedName()}.{syntax.EnsureWrapped(columnsToMigrate.PrimaryKeys.First().GetRuntimeName())} IS NULL");
sbInsert.AppendLine(string.Join(" AND ",
columnsToMigrate.PrimaryKeys.Select(pk =>
$"{columnsToMigrate.DestinationTable.GetFullyQualifiedName()}.{syntax.EnsureWrapped(pk.GetRuntimeName())} IS NULL")));

//right at the end of the SELECT
if (columnsToMigrate.DestinationTable.Database.Server.DatabaseType == DatabaseType.MySql)
Expand Down Expand Up @@ -196,7 +199,10 @@ CrossDatabaseMergeCommandTo..ToTable.Age is null
}
}

private static string GetORLine(DiscoveredColumn c, IQuerySyntaxHelper syntax) => string.Format(
"(t1.{0} <> t2.{0} OR (t1.{0} is null AND t2.{0} is not null) OR (t2.{0} is null AND t1.{0} is not null))",
syntax.EnsureWrapped(c.GetRuntimeName()));
private static string GetORLine(DiscoveredColumn c, IQuerySyntaxHelper syntax)
{
return string.Format(
"(t1.{0} <> t2.{0} OR (t1.{0} is null AND t2.{0} is not null) OR (t2.{0} is null AND t1.{0} is not null))",
syntax.EnsureWrapped(c.GetRuntimeName()));
}
}
2 changes: 1 addition & 1 deletion SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@

[assembly: AssemblyVersion("8.4.0")]
[assembly: AssemblyFileVersion("8.4.0")]
[assembly: AssemblyInformationalVersion("8.4.0")]
[assembly: AssemblyInformationalVersion("8.4.0")]