Exemple #1
0
        void EnsureMigrationTableExists(IExclusiveDbConnection connection)
        {
            var tableNames = connection.GetTableNames();

            if (!tableNames.Contains(_migrationTableName))
            {
                _writer.Verbose($"Database does not contain migration log table '{_migrationTableName}' - will try to create it now");

                try
                {
                    CreateMigrationTable(_migrationTableName, connection);
                }
                catch (Exception)
                {
                    // if table creation failed, we might have run into a race with another process...
                    // ....therefore, just check if the table NOW exists:
                    try
                    {
                        _writer.Verbose("Could not create the table - checking if someone else created it");

                        if (connection.GetTableNames().Contains(_migrationTableName))
                        {
                            _writer.Verbose($"The migration log table '{_migrationTableName}' was now found - we're good");
                            return;
                        }
                    }
                    catch { }

                    // if it didn't exist, throw the original exception
                    _writer.Verbose($"Could not find the migration log table '{_migrationTableName}' - re-throwing the exception caught when initally trying to create it");
                    throw;
                }
            }
        }
Exemple #2
0
        void EnsureMigrationTableExists(IExclusiveDbConnection connection)
        {
            var tableNames = connection.GetTableNames();

            if (!tableNames.Contains(_migrationTableName))
            {
                _writer.Verbose($"Database does not contain migration log table '{_migrationTableName}' - will create it now");

                CreateMigrationTable(_migrationTableName, connection);
            }
        }
Exemple #3
0
        void CreateMigrationTable(string migrationTableName, IExclusiveDbConnection connection)
        {
            try
            {
                connection.CreateMigrationTable(migrationTableName);

                _writer.Info($"Created migration table '{migrationTableName}'");
            }
            catch (Exception exception)
            {
                throw new MigrationException($"Could not create migration table '{migrationTableName}'", exception);
            }
        }
Exemple #4
0
        IExecutableSqlMigration GetNextMigration(IExclusiveDbConnection connection, List <IExecutableSqlMigration> migrations)
        {
            var executedMigrationIds = connection.GetExecutedMigrationIds(_migrationTableName).ToList();

            var remainingMigrations = migrations
                                      .Where(m => !executedMigrationIds.Contains(m.Id))
                                      .ToList();

            VerifyMigrationBandit(executedMigrationIds, remainingMigrations);

            var nextMigration = remainingMigrations
                                .OrderBy(m => m.SequenceNumber).ThenBy(m => m.BranchSpecification)
                                .FirstOrDefault();

            return(nextMigration);
        }
Exemple #5
0
        IExecutableSqlMigration GetNextMigration(IExclusiveDbConnection connection, List <IExecutableSqlMigration> migrations)
        {
            var executedMigrationIds = connection.GetExecutedMigrationIds(_migrationTableName);

            var remainingMigrations = migrations
                                      .Where(m => !executedMigrationIds.Contains(m.Id))
                                      .ToList();

            VerifyMigrationBandit(executedMigrationIds, remainingMigrations);

            remainingMigrations.Sort((m1, m2) => CompareMigrationId(m1.Id, m2.Id));

            var nextMigration = remainingMigrations.FirstOrDefault();

            return(nextMigration);
        }
        void ExecuteMigration(IExecutableSqlMigration migration, IExclusiveDbConnection logConnection, IExclusiveDbConnection migrationConnection)
        {
            var id  = migration.Id;
            var sql = migration.Sql;

            _writer.Verbose($"Inserting log row for migration {id}");

            LogMigration(logConnection, migration);

            const RegexOptions options = RegexOptions.Multiline
                                         | RegexOptions.IgnorePatternWhitespace
                                         | RegexOptions.IgnoreCase;

            const string searchPattern = @"^\s*GO\s* ($ | \-\- .*$)";

            var sqlStatements = Regex.Split(sql, searchPattern, options)
                                .Where(x => !string.IsNullOrWhiteSpace(x))
                                .Select(x => x.Trim(' ', '\r', '\n'))
                                .ToList();

            _writer.Verbose($"Migration {id} contains {sqlStatements.Count} individual SQL statements");

            foreach (var sqlStatement in sqlStatements)
            {
                _writer.Verbose($"Executing statement: {sqlStatement}");

                try
                {
                    var hints          = HintParser.ParseHints(migration.Hints);
                    var commandTimeout = hints.GetHint("sql-command-timeout")?.GetValueAsTimeSpan();

                    migrationConnection.ExecuteStatement(sqlStatement, commandTimeout);
                }
                catch (Exception exception)
                {
                    throw new MigrationException($"Error executing SQL statement: '{sqlStatement}'", exception);
                }
            }

            _writer.Info($"Migration {id} executed");
        }
        IExecutableSqlMigration GetNextMigration(IExclusiveDbConnection connection,
                                                 List <IExecutableSqlMigration> migrations, IWriter writer)
        {
            var executedMigrationIds = connection.GetExecutedMigrationIds(_migrationTableName).ToList();

            writer.Verbose($"Database reports the following migrations have been executed: {string.Join(", ", executedMigrationIds)}");

            var remainingMigrations = migrations
                                      .Where(m => !executedMigrationIds.Contains(m.Id))
                                      .ToList();

            VerifyMigrationBandit(executedMigrationIds, remainingMigrations);

            var remainingMigrationsList = remainingMigrations
                                          .OrderBy(m => m.SequenceNumber).ThenBy(m => m.BranchSpecification)
                                          .ToList();

            writer.Verbose($"The following migrations remain in order: {string.Join(", ", remainingMigrationsList.Select(m => m.Id))}");

            var nextMigration = remainingMigrationsList.FirstOrDefault();

            return(nextMigration);
        }
Exemple #8
0
 void LogMigration(IExclusiveDbConnection connection, IExecutableSqlMigration migration)
 {
     connection.LogMigration(migration, _migrationTableName);
 }