示例#1
0
        public void LogMigration(IExecutableSqlMigration migration, string migrationTableName)
        {
            using (var command = CreateCommand())
            {
                command.CommandText = $@"
INSERT INTO [{migrationTableName}] (
    [MigrationId],
    [Sql],
    [Description],
    [Time],
    [UserName],
    [UserDomainName],
    [MachineName]
) VALUES (
    @id,
    @sql,
    @description,
    @time,
    @userName,
    @userDomainName,
    @machineName
)
";

                command.Parameters.Add("id", SqlDbType.NVarChar, 200).Value        = migration.Id;
                command.Parameters.Add("sql", SqlDbType.NVarChar).Value            = migration.Sql;
                command.Parameters.Add("description", SqlDbType.NVarChar).Value    = migration.Description;
                command.Parameters.Add("time", SqlDbType.DateTime2).Value          = DateTime.Now;
                command.Parameters.Add("userName", SqlDbType.NVarChar).Value       = Environment.GetEnvironmentVariable("USERNAME") ?? "??";
                command.Parameters.Add("userDomainName", SqlDbType.NVarChar).Value = Environment.GetEnvironmentVariable("USERDOMAIN") ?? "??";
                command.Parameters.Add("machineName", SqlDbType.NVarChar).Value    = Environment.MachineName;

                command.ExecuteNonQuery();
            }
        }
        public void LogMigration(IExecutableSqlMigration migration, string migrationTableName)
        {
            using (var command = CreateCommand())
            {
                command.CommandText = $@"
INSERT INTO ""{migrationTableName}"" (
    ""MigrationId"",
    ""Sql"",
    ""Description"",
    ""Time"",
    ""UserName"",
    ""UserDomainName"",
    ""MachineName""
) VALUES (
    @id,
    @sql,
    @description,
    @time,
    @userName,
    @userDomainName,
    @machineName
)
";

                command.Parameters.Add("id", NpgsqlDbType.Text).Value             = migration.Id;
                command.Parameters.Add("sql", NpgsqlDbType.Text).Value            = migration.Sql;
                command.Parameters.Add("description", NpgsqlDbType.Text).Value    = migration.Description;
                command.Parameters.Add("time", NpgsqlDbType.TimestampTZ).Value    = DateTime.Now;
                command.Parameters.Add("userName", NpgsqlDbType.Text).Value       = Environment.UserName;
                command.Parameters.Add("userDomainName", NpgsqlDbType.Text).Value = Environment.UserDomainName;
                command.Parameters.Add("machineName", NpgsqlDbType.Text).Value    = Environment.MachineName;

                command.ExecuteNonQuery();
            }
        }
示例#3
0
        public void LogMigration(IExecutableSqlMigration migration, string migrationTableName)
        {
            using (var command = CreateCommand())
            {
                command.CommandText = $@"
INSERT INTO `{migrationTableName}` (
    `MigrationId`,
    `Sql`,
    `Description`,
    `Time`,
    `UserName`,
    `UserDomainName`,
    `MachineName`
) VALUES (
    @id,
    @sql,
    @description,
    @time,
    @userName,
    @userDomainName,
    @machineName
)
";
                command.Parameters.Add("id", MySqlDbType.Text).Value             = migration.Id;
                command.Parameters.Add("sql", MySqlDbType.Text).Value            = migration.Sql;
                command.Parameters.Add("description", MySqlDbType.Text).Value    = migration.Description;
                command.Parameters.Add("time", MySqlDbType.Timestamp).Value      = DateTime.Now;
                command.Parameters.Add("userName", MySqlDbType.Text).Value       = Environment.GetEnvironmentVariable("USERNAME") ?? "??";
                command.Parameters.Add("userDomainName", MySqlDbType.Text).Value = Environment.GetEnvironmentVariable("USERDOMAIN") ?? "??";
                command.Parameters.Add("machineName", MySqlDbType.Text).Value    = Environment.MachineName;

                command.ExecuteNonQuery();
            }
        }
示例#4
0
        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");
        }
示例#5
0
 void LogMigration(IExclusiveDbConnection connection, IExecutableSqlMigration migration)
 {
     connection.LogMigration(migration, _migrationTableName);
 }
示例#6
0
        static bool ShouldHaveBeenExecutedByNow(IEnumerable <IGrouping <string, MigrationId> > executedMigrationsByBranch, IExecutableSqlMigration executableSqlMigration)
        {
            var migrationId = MigrationId.FromString(executableSqlMigration.Id);

            var migrationsForThisParticularBranch = executedMigrationsByBranch
                                                    .FirstOrDefault(b => b.Key == migrationId.BranchSpecification)
                                                    ?.ToList()
                                                    ?? new List <MigrationId>();

            return(migrationsForThisParticularBranch.Any(id => id.CompareTo(migrationId) > 0));
        }
        public void LogMigration(IExecutableSqlMigration migration, string migrationTableName)
        {
            using (var command = CreateCommand())
            {
                command.CommandText = $@"
            INSERT INTO ""{migrationTableName}"" (
            ""MigrationId"",
            ""Sql"",
            ""Description"",
            ""Time"",
            ""UserName"",
            ""UserDomainName"",
            ""MachineName""
            ) VALUES (
            @id,
            @sql,
            @description,
            @time,
            @userName,
            @userDomainName,
            @machineName
            )
            ";

                command.Parameters.Add("id", NpgsqlDbType.Text).Value = migration.Id;
                command.Parameters.Add("sql", NpgsqlDbType.Text).Value = migration.Sql;
                command.Parameters.Add("description", NpgsqlDbType.Text).Value = migration.Description;
                command.Parameters.Add("time", NpgsqlDbType.TimestampTZ).Value = DateTime.Now;
                command.Parameters.Add("userName", NpgsqlDbType.Text).Value = Environment.UserName;
                command.Parameters.Add("userDomainName", NpgsqlDbType.Text).Value = Environment.UserDomainName;
                command.Parameters.Add("machineName", NpgsqlDbType.Text).Value = Environment.MachineName;

                command.ExecuteNonQuery();
            }
        }
        public void LogMigration(IExecutableSqlMigration migration, string migrationTableName)
        {
            using (var command = CreateCommand())
            {
                command.CommandText = $@"
            INSERT INTO [{migrationTableName}] (
            [MigrationId],
            [Sql],
            [Description],
            [Time],
            [UserName],
            [UserDomainName],
            [MachineName]
            ) VALUES (
            @id,
            @sql,
            @description,
            @time,
            @userName,
            @userDomainName,
            @machineName
            )
            ";

                command.Parameters.Add("id", SqlDbType.NVarChar, 200).Value = migration.Id;
                command.Parameters.Add("sql", SqlDbType.NVarChar).Value = migration.Sql;
                command.Parameters.Add("description", SqlDbType.NVarChar).Value = migration.Description;
                command.Parameters.Add("time", SqlDbType.DateTime2).Value = DateTime.Now;
                command.Parameters.Add("userName", SqlDbType.NVarChar).Value = Environment.UserName;
                command.Parameters.Add("userDomainName", SqlDbType.NVarChar).Value = Environment.UserDomainName;
                command.Parameters.Add("machineName", SqlDbType.NVarChar).Value = Environment.MachineName;

                command.ExecuteNonQuery();
            }
        }