in SampleTests/TestFiltering.cs [290:337]
public void TestIncludePlanFiltererInDacpac()
{
// Given a model with objects that use "dev", "test" and "prod" schemas
// and the contributor information built in
var model = CreateTestModel();
string existingPackagePath = GetTestFilePath("includesContributor.dacpac");
Console.WriteLine("Build dacpac to \n" + existingPackagePath);
DacPackageExtensions.BuildPackage(existingPackagePath, model, new PackageMetadata(), new PackageOptions()
{
DeploymentContributors = new[] { new DeploymentContributorInformation() { ExtensionId = PlanFilterer.PlanFiltererContributorId } }
});
DacServices services = new DacServices("Server=(localdb)\\MSSQLLocalDB;Integrated Security=true;");
// When publishing to production (filtering to exclude "dev" and "test" schemas)
string productionDbName = "ProductionDB";
using (DacPackage package = DacPackage.Load(existingPackagePath, DacSchemaModelStorageType.Memory))
{
DacDeployOptions options = new DacDeployOptions();
// Specify the filter to use and what arguments it needs. Note that this is a little limited by
// having to pass string-based arguments. This could be worked around by serializing arguments to a
// file and passing the file path to the contributor if you need to do anything advanced.
options.AdditionalDeploymentContributorArguments =
PlanFilterer.BuildPlanFiltererArgumentString("SchemaBasedFilter", new Dictionary<string, string>()
{
{"Schema1", "dev"},
{"Schema2", "test"},
});
// For test purposes, always create a new database (otherwise previous failures might mess up our result)
options.CreateNewDatabase = true;
// Run the deployment with the options as specified
services.Deploy(package, productionDbName, upgradeExisting: true, options: options);
}
// Then expect only the "prod" schema objects to remain in the new package
// Extract the dacpac back from the database and ensure that only production elements are there
string extractedPackagePath = GetTestFilePath("extracted.dacpac");
services.Extract(extractedPackagePath, productionDbName, "AppName", new Version(1, 0));
var extractedModel = _trash.Add(new TSqlModel(extractedPackagePath, DacSchemaModelStorageType.Memory));
Assert.AreEqual(TopLevelProdElementCount, CountTablesViewsAndSchemas(extractedModel));
AssertAllObjectsHaveSchemaName(extractedModel, "prod");
}