Ejemplo n.º 1
0
        protected virtual void OnAfterScriptRun(ScriptRunEventArgs e)
        {
            EventHandler <ScriptRunEventArgs> handler = AfterScriptRun;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Ejemplo n.º 2
0
        private SchemaChange RunSingleScript(UpdateScript script)
        {
            var schemaScript = BuildSchemaFromFile(script);

            if (schemaScript == null)
            {
                schemaScript              = new SchemaChange();
                schemaScript.Status       = Models.SchemaChange.Status_Failed;
                schemaScript.ScriptErrors = string.Format("The script name {0} was not found. The script did not run.", script.Name);
                return(schemaScript);
            }

            //Fire the Before Event if we are not creating inital schema or database defaults.
            if (!script.Equals(INITIAL_DATABASE_DEFAULTS_FILENAME) && !script.Equals(INITIAL_DATABASE_SCHEMA_FILENAME))
            {
                ScriptRunEventArgs scriptEventArgs = new ScriptRunEventArgs()
                {
                    ScriptName    = script.Name,
                    ScriptVersion = script.Version
                };
                OnBeforeScriptRun(scriptEventArgs);
            }

            using (SqlConnection connection = new SqlConnection(ConnectionString.ConnectionString))
            {
                connection.Open();

                foreach (var item in schemaScript.ScriptLines)
                {
                    schemaScript.Comment = ExtractSingleComment(item, SCRIPT_COMMENT_PREFIX);
                    using (SqlTransaction transaction = connection.BeginTransaction())
                    {
                        try
                        {
                            SqlCommand sqlCommand = new SqlCommand(item, connection);
                            sqlCommand.Connection  = connection;
                            sqlCommand.Transaction = transaction;
                            sqlCommand.ExecuteNonQuery();
                            transaction.Commit();
                        }
                        catch (Exception ex)
                        {
                            schemaScript.ScriptErrors = string.Format("Failed Running script {0} with the comment {1}. SQL Syntax is {2}  Error: {3}", schemaScript.ScriptName, schemaScript.Comment, item, ex.Message);

                            //Get out becuase something broke!
                            schemaScript.Status = Models.SchemaChange.Status_Failed;
                            return(schemaScript);
                        }
                    }
                }

                schemaScript.Status = Models.SchemaChange.Status_Success;
                if (schemaScript.LogThisChange)
                {
                    AddSchemaChange(schemaScript);
                }

                //Fire the Before Event if we are not creating inital schema or database defaults.
                if (!script.Equals(INITIAL_DATABASE_DEFAULTS_FILENAME) && !script.Equals(INITIAL_DATABASE_SCHEMA_FILENAME))
                {
                    ScriptRunEventArgs scriptEventArgs = new ScriptRunEventArgs()
                    {
                        ScriptName    = script.Name,
                        ScriptVersion = script.Version
                    };
                    OnAfterScriptRun(scriptEventArgs);
                }

                return(schemaScript);
            }
        }