public static void AttachDatabase(ConnectionString connectionString, SqlConnectionStringBuilder defaultConnectionString, string name, string databasesFolderPath, string instanceName, IPipelineController controller)
        {
            if (connectionString.IsMongoConnectionString)
              {
            connectionString.Value = AttachDatabasesHelper.GetMongoConnectionString(connectionString.Name, instanceName);
            connectionString.SaveChanges();
            return;
              }

              if (connectionString.IsSqlConnectionString)
              {
            try
            {
              AttachDatabasesHelper.AttachDatabase(name, databasesFolderPath, connectionString, defaultConnectionString, controller);
            }
            catch (Exception ex)
            {
              if (connectionString.Name == "reporting.secondary")
              {
            throw;
              }

              Log.Warn(ex, "Attaching reporting.secondary database failed. Skipping...");
            }
              }
        }
    public static void AttachDatabase(string name, string databasesFolderPath, ConnectionString connectionString, SqlConnectionStringBuilder defaultConnectionString, IPipelineController controller)
    {
      SetConnectionStringNode(name, defaultConnectionString, connectionString);

      string databaseName = connectionString.GenerateDatabaseName(name);

      string databasePath =
        DatabaseFilenameHook(Path.Combine(databasesFolderPath, connectionString.DefaultFileName), 
          connectionString.Name.Replace("yafnet", "forum"), databasesFolderPath);

      if (!IsRemoteSqlServer())
      {
        if (!FileSystem.FileSystem.Local.File.Exists(databasePath))
        {
          var file = Path.GetFileName(databasePath);
          if (file.EqualsIgnoreCase("sitecore.reporting.mdf"))
          {
            databasePath = Path.Combine(Path.GetDirectoryName(databasePath), "Sitecore.Analytics.mdf");
          }
        }

        FileSystem.FileSystem.Local.File.AssertExists(databasePath, databasePath + " file doesn't exist");
      }

      if (SqlServerManager.Instance.DatabaseExists(databaseName, defaultConnectionString))
      {
        databaseName = ResolveConflict(defaultConnectionString, connectionString, databasePath, databaseName, controller);
      }

      if (databaseName != null)
      {
        SqlServerManager.Instance.AttachDatabase(databaseName, databasePath, defaultConnectionString);
      }
    }
        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;
        }