Beispiel #1
0
        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("Attaching reporting.secondary database failed. Skipping...", typeof(AttachDatabasesHelper), ex);
                }
            }
        }
        public static string GetSqlPrefix(Instance instance)
        {
            var connectionStrings = instance.Configuration.ConnectionStrings.Where(x => x.IsSqlConnectionString).ToArray();

            Assert.IsTrue(connectionStrings.Length >= 2, "2 or more sql connection strings are required");

            return(AttachDatabasesHelper.GetSqlPrefix(connectionStrings[0].Value, connectionStrings[1].Value));
        }
Beispiel #3
0
        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);
            }
        }
Beispiel #4
0
        public static void MoveDatabases(string databasesFolderPath)
        {
            if (AttachDatabasesHelper.IsRemoteSqlServer())
            {
                foreach (var filePath in FileSystem.FileSystem.Local.Directory.GetFiles(databasesFolderPath))
                {
                    var remoteDatabaseFilePath = AttachDatabasesHelper.GetRemoteDatabaseFilePath(filePath);
                    FileSystem.FileSystem.Local.Directory.Ensure(Path.GetDirectoryName(remoteDatabaseFilePath));
                    FileSystem.FileSystem.Local.File.Move(filePath, remoteDatabaseFilePath);
                }

                FileSystem.FileSystem.Local.Directory.DeleteIfExists(databasesFolderPath);
            }
        }