/// <summary> /// Executes the given <see cref="SchemaUpgradeCommandSet"/> on the given <see cref="SqlConnection"/>. /// </summary> /// <param name="commandSet">The <see cref="SchemaUpgradeCommandSet"/> containing the SQL commands to execute.</param> /// <param name="connection">The <see cref="SqlConnection"/> to execute the command set on.</param> /// <param name="commandTimeout">The timeout to set for each command, or null to use the default value.</param> public static void ExecuteCommandSet(SchemaUpgradeCommandSet commandSet, SqlConnection connection, int? commandTimeout) { SqlTransaction transaction = null; if (commandSet.RunInTransaction) { transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted, "Tasty.Build.SchemaUpgradeService"); } try { foreach (string command in commandSet.Commands) { using (SqlCommand sqlCommand = connection.CreateCommand()) { sqlCommand.Transaction = transaction; sqlCommand.CommandType = CommandType.Text; sqlCommand.CommandText = command; if (commandTimeout != null) { sqlCommand.CommandTimeout = commandTimeout.Value; } sqlCommand.ExecuteNonQuery(); } } if (transaction != null) { transaction.Commit(); } } catch { if (transaction != null) { transaction.Rollback(); } throw; } }
/// <summary> /// Performs the upgraded process. /// </summary> public void Upgrade() { Version current = this.UpgradeDelegate.GetCurrentVersion(); Version target = this.UpgradeDelegate.GetTargetVersion(); if (current < target) { using (SqlConnection connection = new SqlConnection(this.connectionString)) { connection.Open(); IEnumerable<Version> path = this.UpgradeDelegate.GetUpgradePath(current, target); foreach (Version version in path) { SchemaUpgradeCommandSetResult commands = this.UpgradeDelegate.GetCommandSet(version); SchemaUpgradeCommandSet commandSet = new SchemaUpgradeCommandSet(commands.Sql, version, commands.RunInTransaction); ExecuteCommandSet(commandSet, connection, this.CommandTimeout); this.UpgradeDelegate.MarkAsUpgraded(version); } } } }
/// <summary> /// Executes the given <see cref="SchemaUpgradeCommandSet"/> on the given <see cref="SqlConnection"/>. /// </summary> /// <param name="commandSet">The <see cref="SchemaUpgradeCommandSet"/> containing the SQL commands to execute.</param> /// <param name="connection">The <see cref="SqlConnection"/> to execute the command set on.</param> public static void ExecuteCommandSet(SchemaUpgradeCommandSet commandSet, SqlConnection connection) { ExecuteCommandSet(commandSet, connection, null); }