private TSqlModel CreateFilteredModel(SchemaBasedFilter schemaFilter, TSqlModel model) { ModelFilterer modelFilterer = new ModelFilterer(schemaFilter); TSqlModel filteredModel = modelFilterer.CreateFilteredModel(model); _trash.Add(filteredModel); return(filteredModel); }
public void TestSchemaFilterExcludingSchemas() { // Given a model with objects that use "dev", "test" and "prod" schemas var model = CreateTestModel(); // When deploying to production (filtering to exclude "dev" and "test" schemas) var schemaFilter = new SchemaBasedFilter("dev", "test"); var filteredModel = CreateFilteredModel(schemaFilter, model); // Then expect only the "prod" schema objects to remain Assert.AreEqual(TopLevelProdElementCount, CountTablesViewsAndSchemas(filteredModel)); AssertAllObjectsHaveSchemaName(filteredModel, "prod"); }
public void TestSchemaFilterIncludingSchemas() { // Given a model with objects that use "dev", "test" and "prod" schemas var model = CreateTestModel(); // When filtering to include "dev" and "production" schemas var schemaFilter = new SchemaBasedFilter("dev", "prod"); schemaFilter.Filtering = SchemaBasedFilter.FilterType.Include; var filteredModel = CreateFilteredModel(schemaFilter, model); // Then expect only "test" schema objects to be excluded Assert.AreEqual(TopLevelProdElementCount + TopLevelDevElementCount, CountTablesViewsAndSchemas(filteredModel)); }
public void TestValidationErrorsIfExcludeReferencedSchema() { // Given a set of scripts with "dev", "test" and "production" filters var model = CreateTestModel(); // When filtering to only incude "test" filters var schemaFilter = new SchemaBasedFilter("test"); schemaFilter.Filtering = SchemaBasedFilter.FilterType.Include; var filteredModel = CreateFilteredModel(schemaFilter, model); // Then expect validation of the model to fail since "test" objects depend on elements in the "prod" schema IList <DacModelMessage> validationResults = filteredModel.Validate(); Assert.IsTrue(validationResults.Any(message => message.MessageType == DacMessageType.Error), "Expect validation errors"); }
public void TestUpdateDacpacWithFilteredModel() { // Given a model with objects that use "dev", "test" and "prod" schemas var model = CreateTestModel(); string existingPackagePath = GetTestFilePath("original.dacpac"); BuildPackage(model, existingPackagePath); // When saving a dacpac for deployment to production (filtering to exclude "dev" and "test" schemas) var schemaFilter = new SchemaBasedFilter("dev", "test"); ModelFilterer modelFilterer = new ModelFilterer(schemaFilter); modelFilterer.UpdateDacpacModelWithFilter(existingPackagePath); // Then expect only the "prod" schema objects to remain in the new package var filteredModel = _trash.Add(new TSqlModel(existingPackagePath)); Assert.AreEqual(TopLevelProdElementCount, CountTablesViewsAndSchemas(filteredModel)); }
/// <summary> /// Runs the model filtering example. This shows how to filter a model and save a new /// dacpac with the updated model. You can also update the model in the existing dacpac; /// the unit tests in TestFiltering.cs show how this is performed. /// </summary> public static void RunFilteringExample() { // Given a model with objects that use "dev", "test" and "prod" schemas string devPackagePath = GetFilePathInCurrentDirectory("dev.dacpac"); var scripts = SampleScripts; string productionPackagePath; using (TSqlModel model = new TSqlModel(SqlServerVersion.Sql110, new TSqlModelOptions())) { AddScriptsToModel(model, scripts); Console.WriteLine("Saving test scripts to package '" + devPackagePath + "'"); DacPackageExtensions.BuildPackage(devPackagePath, model, new PackageMetadata()); Console.WriteLine("Objects found in original package: '" + devPackagePath + "'"); PrintTablesViewsAndSchemas(model); productionPackagePath = GetFilePathInCurrentDirectory("production.dacpac"); // When saving a dacpac for deployment to production (filtering to exclude "dev" and "test" schemas) var schemaFilter = new SchemaBasedFilter(model.CollationComparer, new [] { "dev", "test" }); ModelFilterer modelFilterer = new ModelFilterer(schemaFilter); Console.WriteLine("Creating filtered 'production' package: '" + productionPackagePath + "'"); modelFilterer.CreateFilteredDacpac(devPackagePath, productionPackagePath); } // Then expect only the "prod" schema objects to remain in the new package using (TSqlModel filteredModel = new TSqlModel(productionPackagePath)) { Console.WriteLine("Objects found in filtered package: '" + productionPackagePath + "'"); PrintTablesViewsAndSchemas(filteredModel); } // If we publish the dacpac to a database, we can see that only the production schema is // present (debug into this method or view the console output listing only production elements) PublishProductionDacpacAndVerifyContents(productionPackagePath); }
public static void Run() { Directory.CreateDirectory(GetTestDir(_ModelCompareDacsPath)); Directory.CreateDirectory(GetTestDir(_compareResultsPath)); _sourceDacpacPath = GetTestFilePath(_ModelCompareDacsPath, "sourceDatabase.dacpac"); DacPackageExtensions.BuildPackage(_sourceDacpacPath, CreateTestModel(_testSourceScripts), new PackageMetadata()); _destinationDacPacPath = GetTestFilePath(_ModelCompareDacsPath, "destinationDatabase.dacpac"); DacPackageExtensions.BuildPackage(_destinationDacPacPath, CreateTestModel(_testDestinationScripts), new PackageMetadata()); using (TSqlModel sourcemodel = new TSqlModel(_sourceDacpacPath), destmodel = new TSqlModel(_destinationDacPacPath)) { // Filtering to exclude log schema var schemaFilter = new SchemaBasedFilter("log"); ModelFilterer modelFilterer = new ModelFilterer(schemaFilter); TSqlModel smodel = modelFilterer.CreateFilteredModel(sourcemodel); TSqlModel dmodel = modelFilterer.CreateFilteredModel(destmodel); CompareTSqlModels(smodel, dmodel); } }
/// <summary> /// Runs the model filtering example. This shows how to filter a model and save a new /// dacpac with the updated model. You can also update the model in the existing dacpac; /// the unit tests in TestFiltering.cs show how this is performed. /// </summary> public static void RunFilteringExample() { // Given a model with objects that use "dev", "test" and "prod" schemas string devPackagePath = GetFilePathInCurrentDirectory("dev.dacpac"); var scripts = SampleScripts; using (TSqlModel model = new TSqlModel(SqlServerVersion.Sql110, new TSqlModelOptions())) { AddScriptsToModel(model, scripts); Console.WriteLine("Saving test scripts to package '"+devPackagePath+"'"); DacPackageExtensions.BuildPackage(devPackagePath, model, new PackageMetadata()); Console.WriteLine("Objects found in original package: '" + devPackagePath + "'"); PrintTablesViewsAndSchemas(model); } string productionPackagePath = GetFilePathInCurrentDirectory("production.dacpac"); // When saving a dacpac for deployment to production (filtering to exclude "dev" and "test" schemas) var schemaFilter = new SchemaBasedFilter("dev", "test"); ModelFilterer modelFilterer = new ModelFilterer(schemaFilter); Console.WriteLine("Creating filtered 'production' package: '"+productionPackagePath+"'"); modelFilterer.CreateFilteredDacpac(devPackagePath, productionPackagePath); // Then expect only the "prod" schema objects to remain in the new package using (TSqlModel filteredModel = new TSqlModel(productionPackagePath)) { Console.WriteLine("Objects found in filtered package: '" + productionPackagePath + "'"); PrintTablesViewsAndSchemas(filteredModel); } // If we publish the dacpac to a database, we can see that only the production schema is // present (debug into this method or view the console output listing only production elements) PublishProductionDacpacAndVerifyContents(productionPackagePath); }