public void ExecuteMigrationScripts(IDbPlatform dbPlatform, IMigrationVersionInfoManager migrationVersionInfoManager, string connectionString, MigrationScriptCollection migrationScripts, long currentVersion, long? targetVersion, MigrationMode migrationMode) { this.migrationScripts = migrationScripts; this.currentVersion = currentVersion; this.targetVersion = targetVersion; this.migrationMode = migrationMode; }
public void ExecuteMigrationScripts(IDbPlatform dbPlatform, IMigrationVersionInfoManager migrationVersionInfoManager, string connectionString, MigrationScriptCollection migrationScripts, long currentVersion, long? targetVersion, MigrationMode migrationMode) { using(IDbConnection dbConnection = dbPlatform.ProviderFactory.CreateConnection()) { dbConnection.ConnectionString = connectionString; dbConnection.Open(); IList<MigrationScript> effectiveMigrationScripts = new List<MigrationScript>(GetMigrationScripts(migrationScripts, currentVersion, targetVersion, migrationMode)); foreach(MigrationScript migrationScript in effectiveMigrationScripts) { using(IDbTransaction ddlTransaction = BeginDdlTransaction(dbPlatform, dbConnection)) { using(IDbCommand dbCommand = dbConnection.CreateCommand()) { InvokeMigrating(new MigrationScriptExecutionEventArgs(migrationMode, migrationScript.MigrationVersion)); foreach(string ddlScript in migrationScript.DdlScripts) { dbCommand.CommandText = ddlScript; dbCommand.CommandType = CommandType.Text; Trace.WriteLine(ddlScript); // // Workaround for Jet if(!(ddlTransaction is NullDbTransaction)) dbCommand.Transaction = ddlTransaction; try { dbCommandExecutionStrategy.Execute(dbPlatform, dbCommand); } // try // TODO: Replace with DbPlatformException or something catch(Exception e) { throw new MigrationException( string.Format(migrationMode == MigrationMode.Upgrade ? Properties.Resources.ErrorWhileUpgradingToVersion: Properties.Resources.ErrorWhileDowngradingToVersion, migrationScript.MigrationVersion, e.ToString()), ddlScript, e); } // catch } // foreach } // using // // If we're downgrading all way down, do not register it, since SchemaInfo // will be deleted too. if(!(migrationMode == MigrationMode.Downgrade && targetVersion == 0 && effectiveMigrationScripts.IndexOf(migrationScript) == effectiveMigrationScripts.Count - 1)) migrationVersionInfoManager.RegisterMigrationVersion(ddlTransaction, migrationMode, migrationScript.MigrationVersion); ddlTransaction.Commit(); InvokeMigrated(new MigrationScriptExecutionEventArgs(migrationMode, migrationScript.MigrationVersion)); } // using } // foreach } // foreach }
private IEnumerable<MigrationScript> GetMigrationScripts(MigrationScriptCollection migrationScripts, long currentVersion, long? targetVersion, MigrationMode migrationMode) { if(migrationMode == MigrationMode.Upgrade) { foreach (MigrationScript ms in migrationScripts) { yield return ms; } // if } // if else { foreach(MigrationScript ms in Algorithms.Reverse(migrationScripts)) { yield return ms; } // foreach } // else }