private static DBAppliedMigration[] FindMigrationsNotInInstance(DBAppliedMigration[] migrations1, DBAppliedMigration[] migrations2)
        {
            List<DBAppliedMigration> result = new List<DBAppliedMigration>();

            foreach (DBAppliedMigration appliedMig2 in migrations2)
            {
                bool found = false;
                foreach (DBAppliedMigration appliedMig1 in migrations1)
                {
                    if (appliedMig1.MigrationId == appliedMig2.MigrationId)
                    {
                        found = true;
                    }
                }

                if (!found)
                {
                    result.Add(appliedMig2);
                }
            }

            return result.ToArray();
        }
 public DBAppliedMigration[] GetAppliedMigrations(Instance instance)
 {
     List<DBAppliedMigration> result = new List<DBAppliedMigration>();
     using (SqlConnection connection = new SqlConnection(connectionStringProvider.GetConnectionString(instance)))
     {
         connection.Open();
         using (SqlCommand cmd = new SqlCommand(@"SELECT [Id]
                                                       ,[Executed_at]
                                                       ,[Executed_by]
                                                       ,[MigrateScript]
                                                       ,[RollbackScript]
                                                       ,[Migration_id]
                                                   FROM [DBAppliedMigrations]
                                                   ORDER BY [Executed_at] DESC", connection))
         {
             using (SqlDataReader reader = cmd.ExecuteReader())
             {
                 while (reader.Read())
                 {
                     DBAppliedMigration appliedMig = new DBAppliedMigration();
                     appliedMig.Id = reader.GetInt32(0);
                     appliedMig.At = reader.GetDateTime(1);
                     appliedMig.By = reader.GetString(2);
                     appliedMig.MigrateScript = reader.GetString(3);
                     appliedMig.RollbackScript = reader.GetString(4);
                     appliedMig.MigrationId = reader.GetInt32(5);
                     result.Add(appliedMig);
                 }
             }
         }
     }
     return result.ToArray();
 }
        private void SyncApplyMigration(Migration migration, Instance instance, Execution execution)
        {
            try
            {
                execution.AppendLog("Generating rollback scripts...");
                execution.UpdateAndFlush();
                string rollbackScript = scriptsGenerator.GenerateRollbackScript(migration, instance);
                execution.AppendLog(string.Concat(Environment.NewLine, rollbackScript, Environment.NewLine));
                execution.UpdateAndFlush();

                execution.AppendLog("Generating migrate scripts...");
                execution.UpdateAndFlush();
                string migrateScript = scriptsGenerator.GenerateMigrateScript(migration, instance);
                execution.AppendLog(string.Concat(Environment.NewLine, migrateScript, Environment.NewLine));
                execution.UpdateAndFlush();

                execution.AppendLog("Migrating...");
                execution.UpdateAndFlush();

                using (SqlConnection conn = new SqlConnection(connectionStringProvider.GetConnectionString(instance)))
                {
                    conn.Open();

                    conn.InfoMessage += new SqlInfoMessageEventHandler(delegate(object sender, SqlInfoMessageEventArgs e)
                    {
                        execution.AppendLog(e.Message);
                        execution.UpdateAndFlush();
                    });

                    using (System.Transactions.TransactionScope transaction = new System.Transactions.TransactionScope())
                    {
                        List<string> sqlStatements = GetSqlStatements(migrateScript);
                        foreach (string statement in sqlStatements)
                        {
                            using (SqlCommand cmd = new SqlCommand(statement, conn))
                            {
                                cmd.ExecuteNonQuery();
                            }
                        }

                        execution.AppendLog("Scripts completed. Writing migration audit...");
                        execution.UpdateAndFlush();

                        DBAppliedMigration appliedMigration = new DBAppliedMigration(execution);
                        appliedMigration.MigrateScript = migrateScript;
                        appliedMigration.RollbackScript = rollbackScript;
                        migrationTablesManager.WriteSuccessfullMigration(appliedMigration, conn);

                        transaction.Complete();
                    }
                }

                execution.AppendLog("Migration ended with success!");
                execution.ExecutionState = ExecutionState.Completed;
                execution.UpdateAndFlush();
            }
            catch (Exception ex)
            {
                execution.AppendLogException(ex);
                execution.UpdateAndFlush();
            }
        }
 public void WriteSuccessfullMigration(DBAppliedMigration appliedMigration, System.Data.IDbConnection openConnection)
 {
     using (SqlCommand cmd = new SqlCommand(@"INSERT INTO [DBAppliedMigrations]
                                                    ([Executed_at]
                                                    ,[Executed_by]
                                                    ,[MigrateScript]
                                                    ,[RollbackScript]
                                                    ,[Migration_id])
                                              VALUES
                                                    (@executedAt
                                                    ,@executedBy
                                                    ,@migrateScript
                                                    ,@rollbackScript
                                                    ,@migrationId)", (SqlConnection)openConnection))
     {
         cmd.Parameters.Add(new SqlParameter("executedAt", appliedMigration.At));
         cmd.Parameters.Add(new SqlParameter("executedBy", appliedMigration.By));
         cmd.Parameters.Add(new SqlParameter("migrateScript", appliedMigration.MigrateScript));
         cmd.Parameters.Add(new SqlParameter("rollbackScript", appliedMigration.RollbackScript ?? string.Empty));
         cmd.Parameters.Add(new SqlParameter("migrationId", appliedMigration.MigrationId));
         cmd.ExecuteNonQuery();
     }
 }