Data Compare Two Database Backups
zaliis
Posts: 14
I am trying to do a data compare between two sql server backups. the TableDifferences collection is always empty. I know there are differences because I added six records myself and had our DBA check both backups using his RedGate client. Could you please look over this code and see if I made any mistakes.
public class RedgateTester { public void CompareDatabaseBackups(String sourceBackupFile, String targetBackupFile) { using (Database dbSource = GetBackupDatabaseSource(false, sourceBackupFile)) { using (Database dbTarget = GetBackupDatabaseSource(false, targetBackupFile)) { Differences redgateDifferences = dbSource.CompareWith(dbTarget, Options.Default); Boolean schemaDifferences = redgateDifferences.Any(e => e.Type != RedGate.Shared.ComparisonInterfaces.DifferenceType.Equal); if (schemaDifferences) { throw new Exception("There are Schema Differences here. Data Compare will not occur."); } } } using (Database dbSource = GetBackupDatabaseSource(true, sourceBackupFile)) { using (Database dbTarget = GetBackupDatabaseSource(true, targetBackupFile)) { using (ComparisonSession csComparison = new ComparisonSession()) { SchemaMappings smMappings = new SchemaMappings(); smMappings.CreateMappings(dbSource, dbTarget); foreach (TableMapping tmMapping in smMappings.TableMappings) { tmMapping.Include = true; } csComparison.CompareDatabases(dbSource, dbTarget, smMappings); foreach (TableDifference difference in csComparison.TableDifferences) { if (difference.TableMapping.Include) // included difference { DifferencesSummary summary = difference.DifferencesSummary; Console.WriteLine(String.Format("Table {0} ", difference.TargetTable(true).Name)); Console.WriteLine(String.Format("Live Only {0}", summary.DifferenceCount(Row.RowType.In1))); Console.WriteLine(String.Format("Script Folder Only {0}", summary.DifferenceCount(Row.RowType.In2))); Console.WriteLine(String.Format("In both but Different {0}", summary.DifferenceCount(Row.RowType.Different))); Console.WriteLine(String.Format("In both and the Same {0}", summary.DifferenceCount(Row.RowType.Same))); } } } } } } BackupSetDatabase GetBackupDatabaseSource(Boolean registerForData, params String[] backupFiles) { List<string> files = new List<string>(); files.AddRange(backupFiles); BackupSetDatabase backupDatabase = new BackupSetDatabase(); IList<RedGate.Shared.SQL.BackupReaderInterfaces.IBackupSet> backupSets = backupDatabase.GetBackupSets(files, null); String[] passwords = new string[0]; BackupDatabaseSource backupDatabaseSource = new BackupDatabaseSource(files, passwords, backupSets[0]); Console.WriteLine("Registering backup " + files[0]); if (registerForData) { backupDatabase.RegisterForDataCompare(backupDatabaseSource.ToConnectionProperties(), Options.Default); } else { backupDatabase.Register(backupDatabaseSource.ToConnectionProperties(), Options.Default); } return backupDatabase; } }
Comments
Even using the stock sample code with no modifications (and some row changes on WidgetLive), I get absolutely no differences - the TableMappings collection is always empty.
I'm starting to think that using backup comparison is simply broken in this version.
The first thing I'd check is what the Data Compare GUI shows- we can only compare tables that have something unique to work with; basically a PK or a unique index.
If the Data Compare GUI seems ok, then it sounds like there's something odd going on so we can do some more testing.
Redgate Software
Thanks.
Thank you for your reply. Per your advice, I attempted the same comparison using SQL Data Compare 10.4. This worked correctly for both emulating WidgetDev->WidgetLive comparison, as well as my own situation.
Here are the steps to reproduce the failure in the example included in SQL Comparison SDK 1.5:
1. Install SQL Comparison SDK 10 (10.5.0.7)
2. Open "C:\Program Files (x86)\Red Gate\SQL Comparison SDK 10\GettingStarted.exe" and click "SQL creation script for sample databases"
3. Logged in under an administrative account, run the supplied script on SQL Server (for reference, I have tried this on SQL Server 2008 (10.0.2531) and SSDT's LocalDB (11.0.3000))
4. Open the C# SQL Data Compare API samples folder from the GettingStarted.exe
5. Open Visual Studio 2008 as an Administrator (so it has write access to the installation folder)
6. Open "SQLDataCompareCodeSnippets.csproj" in the newly opened Visual Studio instance.
7. Edit Program.cs, changing the DevServerName and LiveServerName to the server from step 3.
8. Run the example program.
9. Run option c) Backup Comparison.
Here is where the first problem occurs. A DllNotFound exception is thrown on line 37 of BackupComparisonExample.cs:
I worked around this by grabbing version 1.0.85.0 of Sqlite.Interop.dll from http://system.data.sqlite.org/downloads/1.0.85.0/sqlite-netFx35-static-binary-Win32-2008-1.0.85.0.zip, and placing Sqlite.Interop.dll in the bin/Debug/x86/ folder as a temporary workaround.
At this point, the example runs, but no differences are found. Placing a breakpoint on line 65 of BackupComparisonExample.cs, you can inspect session.TableDifferences and see it contains no items.
I'll see if I can work out anything
Redgate Software
I'll see if I can work out anything
Redgate Software
I have tested with a set of script folders too and these seem to work OK, so you might want to try that as an alternative.
You'd basically change the registration from the Backup code to:
The rest is pretty much the same. You can create scripts of the schema using SQL Compare (and the SDK has a snippet example I think), and SQL Data Compare will sync from the DB > Script folder to populate it. That folder can then be deployed with an SDK app instead of a backup.
I'll post back as and when I get a theory on the Backup file.
Redgate Software
I'll give scripting the backup database file schema to a folder a shot as a workaround. Thank you for the idea and for researching this.
Have you heard anything from the developers on this? Would my best bet possibly be to file a support request?
Thank you.
Robert
*edit - I've logged this under ref. CSD-191
Redgate Software