Inheritance: MigrationContext, IRuntimeContext
Example #1
0
        internal void Execute(IDbConnection connection, IDbTransaction transaction, MigrationDirection direction, IDbCommandExecutor commandExecutor)
        {
            Debug.Assert(connection.State == ConnectionState.Open);

            var context = new RuntimeContext(connection, transaction, commandExecutor, _providerMetadata);
            Database database = GetDatabaseContainingMigrationChanges(direction, context);
            var translator = new CommandsToSqlTranslator(_provider);
            foreach (string commandText in translator.TranslateToSql(database, context))
            {
                //MigSharp uses ADO, not SMO to run SQL. ADO does not support the 'GO' statement in SQL as it is not TSQL.
                //We split SQL on 'GO' and run as individual statements
                var separatedByGoStatements = new List<string>(goStatementRegex.Split(commandText));
                separatedByGoStatements.RemoveAll(r => String.IsNullOrEmpty(r.Trim()));

                foreach (var statement in separatedByGoStatements)
                {
                    IDbCommand command = connection.CreateCommand();
                    command.CommandTimeout = 0; // do not timeout; the client is responsible for not causing lock-outs
                    command.Transaction = transaction;
                    command.CommandText = statement;
                    try
                    {
                        commandExecutor.ExecuteNonQuery(command);
                    }
                    catch (DbException x)
                    {
                        Log.Error("An error occurred: {0}{1}while trying to execute:{1}{2}", x.Message, Environment.NewLine, command.CommandText);
                        throw;
                    }
                }
            }
        }
        internal void Execute(IDbConnection connection, IDbTransaction transaction, MigrationDirection direction, IDbCommandExecutor commandExecutor)
        {
            Debug.Assert(connection.State == ConnectionState.Open);

            var context = new RuntimeContext(connection, transaction, commandExecutor, _providerMetadata);
            Database database = GetDatabaseContainingMigrationChanges(direction, context);
            var translator = new CommandsToSqlTranslator(_provider);
            foreach (string commandText in translator.TranslateToSql(database, context))
            {
                IDbCommand command = connection.CreateCommand();
                command.CommandTimeout = 0; // do not timeout; the client is responsible for not causing lock-outs
                command.Transaction = transaction;
                command.CommandText = commandText;
                try
                {
                    commandExecutor.ExecuteNonQuery(command);
                }
                catch (DbException x)
                {
                    Log.Error("An error occurred: {0}{1}while trying to execute:{1}{2}{1}{3}", x.Message, Environment.NewLine, command.CommandText,_migration.GetType().ToString());
                    throw new Exception(String.Format("An error occurred: {0}{1}while trying to execute:{1}{2}{1}Migration: {3}", x.Message, Environment.NewLine, command.CommandText, _migration.GetType()));
                }
            }
        }