public void TestDbLocationModifierForImport() { // Given database name, and path to save to string dbName = TestContext.TestName; string dataFolder = GetTestDir(); string filePrefix = "mydb"; string bacpacPath = Path.Combine(dataFolder, dbName + ".bacpac"); string mdfFilePath = Path.Combine(dataFolder, filePrefix + "_Primary.mdf"); string ldfFilePath = Path.Combine(dataFolder, filePrefix + "_Primary.ldf"); // Delete any existing artifacts from a previous run TestUtils.DropDbAndDeleteFiles(dbName, mdfFilePath, ldfFilePath); SqlTestDB importedDb = null; try { // Create a DB and export SqlTestDB db = _trash.Add(TestUtils.CreateTestDatabase(TestUtils.DefaultInstanceInfo, "MyOriginalDb")); db.Execute(CreateOneTable); db.ExportBacpac(bacpacPath); // When deploying using the location modifying contributor DacImportOptions options = new DacImportOptions(); options.ImportContributors = DbLocationModifier.ContributorId; options.ImportContributorArguments = Utils.BuildContributorArguments(new Dictionary <string, string>() { { DbLocationModifier.DbSaveDataLocationArg, dataFolder }, { DbLocationModifier.DbSaveLogDataLocationArg, dataFolder }, { DbLocationModifier.DbFilePrefixArg, filePrefix }, }); importedDb = SqlTestDB.CreateFromBacpac(TestUtils.DefaultInstanceInfo, bacpacPath, options, true); // Then expect the database to be saved under that path AssertDeploySucceeded(importedDb.BuildConnectionString(), importedDb.DatabaseName); Assert.IsTrue(File.Exists(mdfFilePath)); Assert.IsTrue(File.Exists(ldfFilePath)); // Note: for a real application, after creating the DB on the server they may want to // detach it and reattach using the database path. We are not doing this since it's // not relevant to this test } finally { if (importedDb != null) { importedDb.Dispose(); } TestUtils.DeleteIfExists(bacpacPath); TestUtils.DeleteIfExists(mdfFilePath); TestUtils.DeleteIfExists(ldfFilePath); } }
private static DacImportOptions FillDefaultImportOptionsForTest(DacImportOptions importOptions) { DacImportOptions result = new DacImportOptions(); if (importOptions != null) { result.CommandTimeout = importOptions.CommandTimeout; result.ImportContributorArguments = importOptions.ImportContributorArguments; result.ImportContributors = importOptions.ImportContributors; } return(result); }
private static void ImportBacpac( string connectionString, string targetDatabaseName, string bacpacFileFullPath) { var ds = new DacServices(connectionString); using var package = BacPackage.Load(bacpacFileFullPath); var options = new DacImportOptions { CommandTimeout = DefaultCommandTimeout }; ds.Message += (object sender, DacMessageEventArgs eventArgs) => Console.WriteLine(eventArgs.Message.Message); ds.ImportBacpac(package, targetDatabaseName, options); }
private static DacImportOptions FillDefaultImportOptionsForTest(DacImportOptions importOptions) { DacImportOptions result = new DacImportOptions(); if (importOptions != null) { result.CommandTimeout = importOptions.CommandTimeout; result.ImportContributorArguments = importOptions.ImportContributorArguments; result.ImportContributors = importOptions.ImportContributors; } return result; }
public static bool TryCreateFromBacpac(InstanceInfo instance, string bacpacPath, out SqlTestDB db, out string error, DacImportOptions importOptions = null, bool dropDatabaseOnCleanup = false) { error = null; string dbName = string.Empty; try { dbName = Path.GetFileNameWithoutExtension(bacpacPath); importOptions = FillDefaultImportOptionsForTest(importOptions); db = SqlTestDB.CreateFromBacpac(instance, bacpacPath, importOptions, dropDatabaseOnCleanup); return true; } catch (Exception ex) { error = ExceptionText.GetText(ex); db = null; bool dbCreated = SafeDatabaseExists(instance, dbName); if (dbCreated) { db = new SqlTestDB(instance, dbName, dropDatabaseOnCleanup); } return false; } }
public static SqlTestDB CreateFromBacpac(InstanceInfo instance, string bacpacPath, DacImportOptions importOptions = null, bool dropDatabaseOnCleanup = false) { string dbName = Path.GetFileNameWithoutExtension(bacpacPath); DacServices ds = new DacServices(instance.BuildConnectionString(dbName)); using (BacPackage bp = BacPackage.Load(bacpacPath, DacSchemaModelStorageType.Memory)) { importOptions = FillDefaultImportOptionsForTest(importOptions); ds.ImportBacpac(bp, dbName, importOptions); } var sqlDb = new SqlTestDB(instance, dbName, dropDatabaseOnCleanup); return sqlDb; }
public static SqlTestDB CreateFromBacpac(InstanceInfo instance, string bacpacPath, DacImportOptions importOptions = null, bool dropDatabaseOnCleanup = false) { string dbName = Path.GetFileNameWithoutExtension(bacpacPath); DacServices ds = new DacServices(instance.BuildConnectionString(dbName)); using (BacPackage bp = BacPackage.Load(bacpacPath, DacSchemaModelStorageType.Memory)) { importOptions = FillDefaultImportOptionsForTest(importOptions); ds.ImportBacpac(bp, dbName, importOptions); } var sqlDb = new SqlTestDB(instance, dbName, dropDatabaseOnCleanup); return(sqlDb); }
public static bool TryCreateFromBacpac(InstanceInfo instance, string bacpacPath, out SqlTestDB db, out string error, DacImportOptions importOptions = null, bool dropDatabaseOnCleanup = false) { error = null; string dbName = string.Empty; try { dbName = Path.GetFileNameWithoutExtension(bacpacPath); importOptions = FillDefaultImportOptionsForTest(importOptions); db = SqlTestDB.CreateFromBacpac(instance, bacpacPath, importOptions, dropDatabaseOnCleanup); return(true); } catch (Exception ex) { error = ExceptionText.GetText(ex); db = null; bool dbCreated = SafeDatabaseExists(instance, dbName); if (dbCreated) { db = new SqlTestDB(instance, dbName, dropDatabaseOnCleanup); } return(false); } }
public void TestDbLocationModifierForImport() { // Given database name, and path to save to string dbName = TestContext.TestName; string dataFolder = GetTestDir(); string filePrefix = "mydb"; string bacpacPath = Path.Combine(dataFolder, dbName + ".bacpac"); string mdfFilePath = Path.Combine(dataFolder, filePrefix + "_Primary.mdf"); string ldfFilePath = Path.Combine(dataFolder, filePrefix + "_Primary.ldf"); // Delete any existing artifacts from a previous run TestUtils.DropDbAndDeleteFiles(dbName, mdfFilePath, ldfFilePath); SqlTestDB importedDb = null; try { // Create a DB and export SqlTestDB db = _trash.Add(TestUtils.CreateTestDatabase(TestUtils.DefaultInstanceInfo, "MyOriginalDb")); db.Execute(CreateOneTable); db.ExportBacpac(bacpacPath); // When deploying using the location modifying contributor DacImportOptions options = new DacImportOptions(); options.ImportContributors = DbLocationModifier.ContributorId; options.ImportContributorArguments = Utils.BuildContributorArguments(new Dictionary<string, string>() { {DbLocationModifier.DbSaveLocationArg, dataFolder}, {DbLocationModifier.DbFilePrefixArg, filePrefix}, }); importedDb = SqlTestDB.CreateFromBacpac(TestUtils.DefaultInstanceInfo, bacpacPath, options, true); // Then expect the database to be saved under that path AssertDeploySucceeded(importedDb.BuildConnectionString(), importedDb.DatabaseName); Assert.IsTrue(File.Exists(mdfFilePath)); Assert.IsTrue(File.Exists(ldfFilePath)); // Note: for a real application, after creating the DB on the server they may want to // detach it and reattach using the database path. We are not doing this since it's // not relevant to this test } finally { if(importedDb != null) { importedDb.Dispose(); } TestUtils.DeleteIfExists(bacpacPath); TestUtils.DeleteIfExists(mdfFilePath); TestUtils.DeleteIfExists(ldfFilePath); } }