Ejemplo n.º 1
0
        public void CreateSchema(SqlScript[] scripts)
        {
            Validate();
            string databaseName;
            var server = GetServer(out databaseName);
            try
            {
                var db = server.Databases[databaseName];
                if (db == null)
                    throw new InvalidDatabaseOperationException(String.Format("Database '{0}' not found on the server.", databaseName));

                foreach (var script in scripts)
                    try
                    {
                        db.ExecuteNonQuery(script.SQL);
                    }
                    catch (Exception ex)
                    {
                        throw new ExecuteDatabaseScriptException(String.Format("Create database script '{0}' failed. See inner exception for more details.", script.Name), ex, script);
                    }
            }
            finally
            {
                server.ConnectionContext.Disconnect();
            }
        }
Ejemplo n.º 2
0
 public ExecuteDatabaseScriptException(string message, Exception innerException, SqlScript script)
     : base(message, innerException)
 {
     Script = script;
 }
Ejemplo n.º 3
0
 List<ScriptInfo> PrepareScripts(SqlScript[] scripts)
 {
     List<ScriptInfo> sortedScripts = new List<ScriptInfo>();
     foreach (var script in scripts)
         try
         {
             sortedScripts.Add(new ScriptInfo
             {
                 Version = ScriptVersionNumberGetter(script.Name),
                 Script = script
             });
         }
         catch (Exception ex)
         {
             throw new InvalidDatabaseOperationException(String.Format("Could not determine upgrade script '{0}' version.", script.Name), ex);
         }
     return sortedScripts;
 }
Ejemplo n.º 4
0
        bool DoUpgrade(String currentVersion, string targetVersion, List<ScriptInfo> sortedScripts, SqlScript[] afterUpgradeScripts)
        {
            bool upgradeStarted = false;
            string databaseName;
            var server = GetServer(out databaseName);
            var db = server.Databases[databaseName];
            if (db == null)
                throw new InvalidDatabaseOperationException(String.Format("Database '{0}' not found on the server.", databaseName));

            bool errors = false;

            try
            {
                PrepareForUpgrade(server, databaseName, currentVersion);
                upgradeStarted = true;
            }
            catch (Exception ex)
            {
                throw new InvalidDatabaseOperationException("Could not make database backup. Check inner exception for more details.", ex);
            }

            try
            {
                foreach (var script in sortedScripts)
                    if (String.Compare(script.Version, currentVersion) > 0)
                    {
                        bool scriptSuccess;
                        try
                        {
                            db.ExecuteNonQuery(script.Script.SQL);
                            scriptSuccess = true;
                        }
                        catch (Exception ex)
                        {
                            var msg = String.Format("Database script '{0}' failed. Check inner exception for more details.", script.Script.Name);
                            if (!IgnoreScriptFailures)
                                throw new ExecuteDatabaseScriptException(msg, ex, script.Script);

                            Logger.Exception(msg, ex);
                            scriptSuccess = false;
                            errors = true;
                        }

                        if (scriptSuccess)
                            try
                            {
                                db.ExecuteNonQuery(SetVersionSqlCommandText.Replace("{Version}", script.Version));
                            }
                            catch (Exception ex)
                            {
                                throw new InvalidDatabaseOperationException("Setting the new database version failed. Check set version command and inner exception for more details.", ex);
                            }
                    }

                if (afterUpgradeScripts != null)
                    foreach (var script in afterUpgradeScripts)
                        try
                        {
                            db.ExecuteNonQuery(script.SQL);
                        }
                        catch (Exception ex)
                        {
                            throw new ExecuteDatabaseScriptException(String.Format("Database script '{0}' failed. Check inner exception for more details.", script.Name), ex, script);
                        }

                if (errors)
                    Logger.WarningFormat("Database has been upgraded to version '{0}' with errors. Please check previous log messages and fix all script errors.", targetVersion);
                else
                    Logger.InfoFormat("Database has been successfully upgraded to version '{0}'.", targetVersion);

                FreeUpgradeResources();
                return true;
            }
            catch (Exception ex)
            {
                CloseConnection();
                try
                {
                    if (upgradeStarted)
                        try
                        {
                            Logger.Exception("Database upgrade error occured. Database will be restored.", ex);
                            RollbackUpgrade(server, databaseName);
                            Logger.Error("Database restored to original version.");
                            FreeUpgradeResources();
                        }
                        catch (Exception rbex)
                        {
                            Logger.Exception(String.Format("Database rollback failed! Please perform the rollback manually. Database backup file is stored at '{0}'.", backupFilePath), rbex.InnerException);
                        }
                }
                finally
                {
                    CloseConnection();
                }
                throw;
            }
        }
Ejemplo n.º 5
0
 public bool UpgradeSchema(SqlScript[] scripts)
 {
     return UpgradeSchema(new SqlSchemaUpgradeScripts
     {
         UpgradeScripts = scripts
     });
 }
Ejemplo n.º 6
0
 public ExecuteDatabaseScriptException(string message, Exception innerException, SqlScript script)
     : base(message, innerException)
 {
     Script = script;
 }