public static void AttachDatabase(Adapters.WebServer.ConnectionString connectionString, SqlConnectionStringBuilder defaultConnectionString, string name, string sqlPrefix, bool attachSql, string databasesFolderPath, string instanceName, IPipelineController controller) { if (connectionString.IsMongoConnectionString) { connectionString.Value = GetMongoConnectionString(connectionString.Name, sqlPrefix); connectionString.SaveChanges(); return; } if (connectionString.IsSqlConnectionString) { AttachDatabase(name, sqlPrefix, attachSql, databasesFolderPath, connectionString, defaultConnectionString, controller); } }
private static string GetDatabasePath(string databasesFolderPath, Adapters.WebServer.ConnectionString connectionString, string databaseName, string extension) { var databasePath = DatabaseFilenameHook(Path.Combine(databasesFolderPath, connectionString.DefaultFileName), connectionString.Name.Replace("yafnet", "forum"), databasesFolderPath); databasePath = Path.Combine(Path.GetDirectoryName(databasePath), Path.GetFileNameWithoutExtension(databasePath) + extension); if (!FileSystem.Local.File.Exists(databasePath)) { if (connectionString.Name.EqualsIgnoreCase("reporting")) { databasePath = Path.Combine(Path.GetDirectoryName(databasePath), "Sitecore.Analytics" + extension); } else if (connectionString.Name.EqualsIgnoreCase("exm.dispatch")) { databasePath = Path.Combine(Path.GetDirectoryName(databasePath), "Sitecore.Exm" + extension); } else if (connectionString.Name.EqualsIgnoreCase("session")) { databasePath = Path.Combine(Path.GetDirectoryName(databasePath), "Sitecore.Sessions" + extension); } } if (!File.Exists(databasePath)) { return(databasePath); } if (Settings.CoreInstallRenameSqlFiles.Value) { // Make the database data file also matching databaseName var newPath = databasePath.Replace(Path.GetFileNameWithoutExtension(connectionString.DefaultFileName) + extension, string.Concat(databaseName, extension)); try { File.Move(databasePath, newPath); } catch { } // Assert again databasePath = newPath; FileSystem.Local.File.AssertExists(databasePath, databasePath + " file doesn't exist"); } return(databasePath); }
public static void AttachDatabase(string name, string sqlPrefix, bool attachSql, string databasesFolderPath, Adapters.WebServer.ConnectionString connectionString, SqlConnectionStringBuilder defaultConnectionString, IPipelineController controller) { SetConnectionStringNode(name, sqlPrefix, defaultConnectionString, connectionString); if (!attachSql) { return; } var databaseName = connectionString.GenerateDatabaseName(name, sqlPrefix); var databasePath = GetDatabasePath(databasesFolderPath, connectionString, databaseName, ".mdf"); if (!File.Exists(databasePath)) { databasePath = GetDatabasePath(databasesFolderPath, connectionString, databaseName, ".dacpac"); if (!File.Exists(databasePath)) { Log.Warn($"File cannot be found: {databasePath} (.dacpac or .mdf)"); return; } } if (SqlServerManager.Instance.DatabaseExists(databaseName, defaultConnectionString)) { databaseName = ResolveConflict(defaultConnectionString, connectionString, databasePath, databaseName, controller); } if (databaseName != null) { var extension = Path.GetExtension(databasePath); if (extension == ".dacpac") { new SqlAdapter(new SqlConnectionString(defaultConnectionString.ToString())).DeployDatabase(databaseName, new RealFileSystem().ParseFile(databasePath)); var tmpPath = SqlServerManager.Instance.GetDatabaseFileName(databaseName, defaultConnectionString); SqlServerManager.Instance.DetachDatabase(databaseName, defaultConnectionString); extension = ".mdf"; databasePath = Path.Combine(Path.GetDirectoryName(databasePath), Settings.CoreInstallRenameSqlFiles.Value ? databaseName + extension : Path.GetFileNameWithoutExtension(databasePath) + extension); File.Move(tmpPath, databasePath); } SqlServerManager.Instance.AttachDatabase(databaseName, databasePath, defaultConnectionString); } }
private static void SetConnectionStringNode([NotNull] string name, [NotNull] string sqlPrefix, [NotNull] SqlConnectionStringBuilder defaultConnectionString, [NotNull] Adapters.WebServer.ConnectionString connectionString) { Assert.ArgumentNotNull(name, nameof(name)); Assert.ArgumentNotNull(sqlPrefix, nameof(sqlPrefix)); Assert.ArgumentNotNull(defaultConnectionString, nameof(defaultConnectionString)); Assert.ArgumentNotNull(connectionString, nameof(connectionString)); SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(defaultConnectionString.ConnectionString) { InitialCatalog = connectionString.GenerateDatabaseName(name, sqlPrefix), IntegratedSecurity = false }; connectionString.Value = builder.ToString(); connectionString.SaveChanges(); }
private static string ResolveConflictByUnsedName([NotNull] SqlConnectionStringBuilder defaultConnectionString, [NotNull] Adapters.WebServer.ConnectionString connectionString, [NotNull] string databaseName) { Assert.ArgumentNotNull(defaultConnectionString, nameof(defaultConnectionString)); Assert.ArgumentNotNull(connectionString, nameof(connectionString)); Assert.ArgumentNotNull(databaseName, nameof(databaseName)); databaseName = GetUnusedDatabaseName(defaultConnectionString, databaseName); connectionString.RealName = databaseName; connectionString.SaveChanges(); return(databaseName); }
public static string ResolveConflict(SqlConnectionStringBuilder defaultConnectionString, Adapters.WebServer.ConnectionString connectionString, string databasePath, string databaseName, IPipelineController controller) { var 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 var delete = $"Delete the '{databaseName}' database"; const string AnotherName = "Use another database name"; const string Cancel = "Terminate current action"; string[] options = new[] { delete, AnotherName, Cancel }; var m2 = $"The database with '{databaseName}' name already exists"; var 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); }