Search code examples
c#sql-serversql-server-data-tools

Determine if a Database is "Equal" to a DacPackage


Is there a way to use the SQL Server 2012 Microsoft.SqlServer.Dac Namespace to determine if a database has an identical schema to that described by a DacPackage object? I've looked at the API docs for DacPackage as well as DacServices, but not having any luck; am I missing something?


Solution

  • Here's what I've come up with, but I'm not really crazy about it. If anyone can point out any bugs, edge cases, or better approaches, I'd be much obliged.

    ...
    DacServices dacSvc = new DacServices(connectionString);
    string deployScript = dacSvc.GenerateDeployScript(myDacpac, @"aDb", deployOptions);
    
    if (DatabaseEqualsDacPackage(deployScript))
    {
      Console.WriteLine("The database and the DacPackage are equal");
    }
    ...
    bool DatabaseEqualsDacPackage(string deployScript)
    {
      string equalStr = string.Format("GO{0}USE [$(DatabaseName)];{0}{0}{0}GO{0}PRINT N'Update complete.'{0}GO", Environment.NewLine);
      return deployScript.Contains(equalStr);
    }
    ...
    

    What I really don't like about this approach is that it's entirely dependent upon the format of the generated deployment script, and therefore extremely brittle. Questions, comments and suggestions very welcome.