We are updating our databases using the DacServices
. However sometimes new constraints get added that cause Constraint Violations, which require us to write a migration script to make sure the Data in the column is valid.
However with the DacServices.Deploy
if the ScriptNewConstraintValidation
option is true and the new constraint errors, I cannot figure out how to rollback the changes made by the DacServices.
We are using the AlwaysOn Availability so performing a backup/restore would be extremely difficult. Below is one way I have tried with no success.
var dacServices = new DacServices(dbConnection.ConnectionString);
var deploymentOptions = new DacDeployOptions
{
CreateNewDatabase = createNewDatabase,
ScriptDatabaseCompatibility = false,
ScriptDatabaseCollation = false,
IgnoreUserSettingsObjects = true,
DropPermissionsNotInSource = false,
DropRoleMembersNotInSource = false,
IgnorePermissions = true,
IgnoreRoleMembership = true,
GenerateSmartDefaults = true,
IncludeTransactionalScripts = true,
ScriptNewConstraintValidation = true
};
Microsoft.SqlServer.Dac.DacPackage dacPackage = DacPackage.Load(dacPath);
Console.WriteLine("Deploying DacPac");
CancellationTokenSource tokenSource = new CancellationTokenSource();
try
{
dacServices.Deploy(dacPackage, databaseName, true, deploymentOptions, tokenSource.Token);
}
catch(Exception)
{
tokenSource.Cancel();
}