Skip to content

Commit

Permalink
Defer changes to satisfy unique constraints (fixes #128)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Apr 19, 2022
1 parent ad7155e commit a59634a
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 104 deletions.
48 changes: 48 additions & 0 deletions SyncChanges.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Name nvarchar(200) null,
DateOfBirth datetime null,
Savings decimal null
)");
db.Execute(@"alter table Users add constraint Users_Name_Age_UQ unique (Name, Age)");
db.Execute(@"alter table Users
enable CHANGE_TRACKING
with (TRACK_COLUMNS_UPDATED = OFF)");
Expand Down Expand Up @@ -462,6 +463,53 @@ public void IntermediateTest()
}
}

[Test]
public void UniqueTest()
{
try
{
CreateUsersTable();

var sourceUser = new User { Name = "Michael Jordan", Age = 54, DateOfBirth = new DateTime(1963, 2, 17), Savings = 1.31m * 1e9m };
var sourceUser2 = new User { Name = "Michael Jordan", Age = 55, DateOfBirth = new DateTime(1963, 2, 17), Savings = 1.31m * 1e9m };

using (var db = GetDatabase(SourceDatabaseName))
{
db.Insert(sourceUser);
}

var synchronizer = new Synchronizer(TestConfig);
var success = synchronizer.Sync();

Assert.That(success, Is.True);

using (var db = GetDatabase(SourceDatabaseName))
{
db.Insert(sourceUser2);
sourceUser.Age = 56;
db.Update(sourceUser);
sourceUser2.Age = 54;
db.Update(sourceUser2);
}

success = synchronizer.Sync();

Assert.That(success, Is.True);

using (var db = GetDatabase(DestinationDatabaseName))
{
var users = db.Fetch<User>("select * from Users");
Assert.That(users.Count, Is.EqualTo(2));
Assert.That(users[0].Age, Is.EqualTo(56));
Assert.That(users[1].Age, Is.EqualTo(54));
}
}
finally
{
DropTable("Users");
}
}

[Test]
public void NullConfigTest()
{
Expand Down
1 change: 1 addition & 0 deletions SyncChanges/SyncChanges.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
Expand Down
Loading

0 comments on commit a59634a

Please sign in to comment.