public static void Process(IEnumerable<Database> innerDatabases, string rootPath, SqlConnectionStringBuilder connectionString, string instanceName, IPipelineController controller, List<string> done) { string localDataSource = SqlServerManager.Instance.NormalizeServerName(connectionString.DataSource); foreach (Database database in innerDatabases) { var cstr = database.ConnectionString.ToString(); if (!SqlServerManager.Instance.IsSqlConnectionString(cstr)) { continue; } // if the database is attached to remote SQL Server string dataSource = SqlServerManager.Instance.NormalizeServerName(database.ConnectionString.DataSource); if (!dataSource.EqualsIgnoreCase(localDataSource)) { // user doesn't confirm deleting if (controller == null || !controller.Confirm("The '{0}' database seems to be located on the '{1}' remote SQL server instead of the '{2}' local one specified in the Settings dialog. \n\nShould it be deleted as well?".FormatWith(database.RealName, dataSource, localDataSource))) { continue; } } string fileName = database.FileName; if (!string.IsNullOrEmpty(fileName)) { // database is located out of the rootPath folder if (!fileName.ToLower().Contains(rootPath)) { // user doesn't confirm deleting if (controller == null || !controller.Confirm("The '{0}' database with '{1}' file path is located out of the {2} instance's root path '{3}'. \n\nShould it be deleted as well?".FormatWith(database.RealName, fileName, instanceName, rootPath))) { continue; } } } if (SqlServerManager.Instance.DatabaseExists(database.RealName, connectionString)) { database.Delete(); } } SqlServerManager.Instance.DetectDatabases(rootPath, connectionString, name => { if (!done.Contains(name)) { SqlServerManager.Instance.DeleteDatabase(name, connectionString); done.Add(name); } }); }
public static void Process(IEnumerable <Database> innerDatabases, string rootPath, SqlConnectionStringBuilder connectionString, string instanceName, IPipelineController controller, List <string> done) { var localDataSource = SqlServerManager.Instance.NormalizeServerName(connectionString.DataSource); foreach (Database database in innerDatabases) { var cstr = database.ConnectionString.ToString(); if (!SqlServerManager.Instance.IsSqlConnectionString(cstr)) { continue; } // if the database is attached to remote SQL Server var dataSource = SqlServerManager.Instance.NormalizeServerName(database.ConnectionString.DataSource); if (!dataSource.EqualsIgnoreCase(localDataSource)) { // user doesn't confirm deleting if (controller == null || !controller.Confirm("The '{0}' database seems to be located on the '{1}' remote SQL server instead of the '{2}' local one specified in the Settings dialog. \n\nShould it be deleted as well?".FormatWith(database.RealName, dataSource, localDataSource))) { continue; } } var fileName = database.FileName; if (!string.IsNullOrEmpty(fileName)) { // database is located out of the rootPath folder if (!fileName.ToLower().Contains(rootPath)) { // user doesn't confirm deleting if (controller == null || !controller.Confirm("The '{0}' database with '{1}' file path is located out of the {2} instance's root path '{3}'. \n\nShould it be deleted as well?".FormatWith(database.RealName, fileName, instanceName, rootPath))) { continue; } } } if (SqlServerManager.Instance.DatabaseExists(database.RealName, connectionString)) { database.Delete(); } } SqlServerManager.Instance.DetectDatabases(rootPath, connectionString, name => { if (!done.Contains(name)) { SqlServerManager.Instance.DeleteDatabase(name, connectionString); done.Add(name); } }); }
private static void AddDatabase([NotNull] Instance instance, [NotNull] IEnumerable <XmlElement> databases, [NotNull] Product module, SqlConnectionStringBuilder connectionString, IPipelineController controller) { Assert.ArgumentNotNull(instance, nameof(instance)); Assert.ArgumentNotNull(databases, nameof(databases)); Assert.ArgumentNotNull(module, nameof(module)); Assert.ArgumentNotNull(connectionString, nameof(connectionString)); foreach (XmlElement database in databases) { var name = database.GetAttribute("name"); var role = database.GetAttribute("role").EmptyToNull() ?? name; var fileName = database.GetAttribute("fileName"); var sourceFileName = database.GetAttribute("sourceFileName"); var databasesFolder = GetDatabasesFolder(instance, connectionString, controller); var locationInPackage = database.GetAttribute("location"); Assert.IsNotNull(databasesFolder, nameof(databasesFolder)); FileSystem.FileSystem.Local.Directory.AssertExists(databasesFolder); var sqlPrefix = AttachDatabasesHelper.GetSqlPrefix(instance); bool skipAttach = false; var realDBname = SqlServerManager.Instance.GenerateDatabaseRealName(instance.Name, sqlPrefix, role); var physicalPath = Path.Combine(databasesFolder, fileName); var newDatabaseConnectionString = new SqlConnectionStringBuilder(connectionString.ToString()); newDatabaseConnectionString.InitialCatalog = realDBname; if (SqlServerManager.Instance.DatabaseExists(realDBname, newDatabaseConnectionString)) { var databasePath = SqlServerManager.Instance.GetDatabaseFileName(realDBname, newDatabaseConnectionString); const string theDatabaseExists = "The database with the same name ('{0}') already exists in the current instance of SQL Server ('{1}')"; if (string.IsNullOrEmpty(databasePath)) { var message = string.Format(theDatabaseExists + ", but doesn't point to any file(s) and looks like corrupted.", realDBname, newDatabaseConnectionString.DataSource); if (!controller.Confirm(message + " Would you like to delete it? If not then this installation will be interrupted.")) { throw new InvalidOperationException(message); } SqlServerManager.Instance.DeleteDatabase(realDBname, newDatabaseConnectionString); } else if (!databasePath.EqualsIgnoreCase(physicalPath)) { throw new InvalidOperationException(string.Format(theDatabaseExists + ", but points to files by another location ('{2}') than was expected ('{3}')", realDBname, newDatabaseConnectionString.DataSource, databasePath, physicalPath)); } skipAttach = true; } if (!skipAttach) { ExtractDatabase(module, fileName, sourceFileName, databasesFolder, locationInPackage); SqlServerManager.Instance.AttachDatabase(realDBname, physicalPath, newDatabaseConnectionString); } instance.Configuration.ConnectionStrings.Add(name, newDatabaseConnectionString); } }
private static string GetDatabasesFolder(Instance instance, SqlConnectionStringBuilder connectionString, IPipelineController controller) { string databasesFolder; Database mainDatabase = GetMainDatabase(instance, connectionString); if (mainDatabase == null) { while (true) { databasesFolder = controller.Ask( "Can't find any local database of the " + instance + " instance to detect the Databases folder. Please specify it manually:", instance.RootPath.TrimEnd('\\') + "\\Databases"); if (string.IsNullOrEmpty(databasesFolder)) { if (controller.Confirm("You didn't input anything - would you like to terminate this installation?")) { throw new Exception("Aborted."); } continue; } if (!FileSystem.FileSystem.Local.Directory.Exists(databasesFolder)) { if (controller.Confirm("The " + databasesFolder + " doesn't exist. Would you like to create the folder?")) { FileSystem.FileSystem.Local.Directory.CreateDirectory(databasesFolder); break; } } else { break; } } } else { databasesFolder = Path.GetDirectoryName(mainDatabase.FileName).EmptyToNull(); } return(databasesFolder); }
public static string ResolveConflict(SqlConnectionStringBuilder defaultConnectionString, ConnectionString connectionString, string databasePath, string databaseName, IPipelineController controller) { string existingDatabasePath = SqlServerManager.Instance.GetDatabaseFileName(databaseName, defaultConnectionString); if (string.IsNullOrEmpty(existingDatabasePath)) { var m = "The database with the same '{0}' name is already exists in the SQL Server metabase but points to non-existing file. ".FormatWith(databaseName); if (controller.Confirm(m + "Would you like to delete it?")) { SqlServerManager.Instance.DeleteDatabase(databaseName, defaultConnectionString); return(databaseName); } throw new Exception(m); } if (existingDatabasePath.EqualsIgnoreCase(databasePath)) { return(null); } // todo: replce this with shiny message box string delete = "Delete the '{0}' database".FormatWith(databaseName); const string AnotherName = "Use another database name"; const string Cancel = "Terminate current action"; string[] options = new[] { delete, AnotherName, Cancel }; string m2 = "The database with '{0}' name already exists".FormatWith(databaseName); string result = controller.Select(m2, options); switch (result) { case Cancel: throw new Exception(m2); case AnotherName: databaseName = ResolveConflictByUnsedName(defaultConnectionString, connectionString, databaseName); break; default: SqlServerManager.Instance.DeleteDatabase(databaseName, defaultConnectionString); break; } return(databaseName); }
private static string GetDatabasesFolder(Instance instance, SqlConnectionStringBuilder connectionString, IPipelineController controller) { string databasesFolder; Database mainDatabase = GetMainDatabase(instance, connectionString); if (mainDatabase == null) { while (true) { databasesFolder = controller.Ask( "Can't find any local database of the " + instance + " instance to detect the Databases folder. Please specify it manually:", instance.RootPath.TrimEnd('\\') + "\\Databases"); if (string.IsNullOrEmpty(databasesFolder)) { if (controller.Confirm("You didn't input anything - would you like to terminate this installation?")) { throw new Exception("Aborted."); } continue; } if (!FileSystem.FileSystem.Local.Directory.Exists(databasesFolder)) { if (controller.Confirm("The " + databasesFolder + " doesn't exist. Would you like to create the folder?")) { FileSystem.FileSystem.Local.Directory.CreateDirectory(databasesFolder); break; } } else { break; } } } else { databasesFolder = Path.GetDirectoryName(mainDatabase.FileName).EmptyToNull(); } return databasesFolder; }
private static void AddDatabase([NotNull] Instance instance, [NotNull] IEnumerable<XmlElement> databases, [NotNull] Product module, SqlConnectionStringBuilder connectionString, IPipelineController controller) { Assert.ArgumentNotNull(instance, "instance"); Assert.ArgumentNotNull(databases, "databases"); Assert.ArgumentNotNull(module, "module"); Assert.ArgumentNotNull(connectionString, "connectionString"); foreach (XmlElement database in databases) { string name = database.GetAttribute("name"); string role = database.GetAttribute("role").EmptyToNull() ?? name; string fileName = database.GetAttribute("fileName"); string sourceFileName = database.GetAttribute("sourceFileName"); string databasesFolder = GetDatabasesFolder(instance, connectionString, controller); var locationInPackage = database.GetAttribute("location"); Assert.IsNotNull(databasesFolder, "databasesFolder"); FileSystem.FileSystem.Local.Directory.AssertExists(databasesFolder); bool skipAttach = false; string realDBname = SqlServerManager.Instance.GenerateDatabaseRealName(instance.Name, role); string physicalPath = Path.Combine(databasesFolder, fileName); var newDatabaseConnectionString = new SqlConnectionStringBuilder(connectionString.ToString()); newDatabaseConnectionString.InitialCatalog = realDBname; if (SqlServerManager.Instance.DatabaseExists(realDBname, newDatabaseConnectionString)) { string databasePath = SqlServerManager.Instance.GetDatabaseFileName(realDBname, newDatabaseConnectionString); const string theDatabaseExists = "The database with the same name ('{0}') already exists in the current instance of SQL Server ('{1}')"; if (string.IsNullOrEmpty(databasePath)) { string message = string.Format(theDatabaseExists + ", but doesn't point to any file(s) and looks like corrupted.", realDBname, newDatabaseConnectionString.DataSource); if (!controller.Confirm(message + " Would you like to delete it? If not then this installation will be interrupted.")) { throw new InvalidOperationException(message); } SqlServerManager.Instance.DeleteDatabase(realDBname, newDatabaseConnectionString); } else if (!databasePath.EqualsIgnoreCase(physicalPath)) { throw new InvalidOperationException(string.Format(theDatabaseExists + ", but points to files by another location ('{2}') than was expected ('{3}')", realDBname, newDatabaseConnectionString.DataSource, databasePath, physicalPath)); } skipAttach = true; } if (!skipAttach) { if ("data".Equals(locationInPackage, StringComparison.InvariantCulture)) { var sourceMdfFile = Path.Combine(instance.DataFolderPath, fileName); var sourceLdfFile = Path.ChangeExtension(sourceMdfFile, "ldf"); if (!FileSystem.FileSystem.Local.File.Exists(sourceMdfFile) || !FileSystem.FileSystem.Local.File.Exists(sourceLdfFile)) { throw new InvalidOperationException("mdf or ldf database files not found in Data folder"); } var targetLdfFile = Path.ChangeExtension(physicalPath, "ldf"); FileSystem.FileSystem.Local.File.Move(sourceMdfFile, physicalPath); FileSystem.FileSystem.Local.File.Move(sourceLdfFile, targetLdfFile); } else { ExtractDatabase(module, fileName, sourceFileName, databasesFolder, locationInPackage); } SqlServerManager.Instance.AttachDatabase(realDBname, physicalPath, newDatabaseConnectionString); } instance.Configuration.ConnectionStrings.Add(name, newDatabaseConnectionString); } }
public static string ResolveConflict(SqlConnectionStringBuilder defaultConnectionString, ConnectionString connectionString, string databasePath, string databaseName, IPipelineController controller) { string existingDatabasePath = SqlServerManager.Instance.GetDatabaseFileName(databaseName, defaultConnectionString); if (string.IsNullOrEmpty(existingDatabasePath)) { var m = "The database with the same '{0}' name is already exists in the SQL Server metabase but points to non-existing file. ".FormatWith(databaseName); if (controller.Confirm(m + "Would you like to delete it?")) { SqlServerManager.Instance.DeleteDatabase(databaseName, defaultConnectionString); return databaseName; } throw new Exception(m); } if (existingDatabasePath.EqualsIgnoreCase(databasePath)) { return null; } // todo: replce this with shiny message box string delete = "Delete the '{0}' database".FormatWith(databaseName); const string AnotherName = "Use another database name"; const string Cancel = "Terminate current action"; string[] options = new[] { delete, AnotherName, Cancel }; string m2 = "The database with '{0}' name already exists".FormatWith(databaseName); string result = controller.Select(m2, options); switch (result) { case Cancel: throw new Exception(m2); case AnotherName: databaseName = ResolveConflictByUnsedName(defaultConnectionString, connectionString, databaseName); break; default: SqlServerManager.Instance.DeleteDatabase(databaseName, defaultConnectionString); break; } return databaseName; }