/// <summary> /// Initializes a new instance of the <see cref="SingleTransactionCommand"/> class. /// </summary> /// <param name="connectionString">The connection string.</param> /// <param name="executionMode">The execution mode. </param> /// public SingleTransactionCommand(string connectionString, ExecutionMode executionMode) { _connectionString = connectionString; _parachuteContext = new ParachuteContext(_connectionString); _transactionScope = new TransactionScope(); _allTransactionsSuccess = false; }
/// <summary> /// Executes the specified SQL scripts in a single transaction for each script. /// </summary> /// <param name="sqlScripts">The SQL scripts.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> public bool Execute(IEnumerable<string> sqlScripts) { var success = false; using (var parachuteContext = new ParachuteContext(_connectionString)) { using (var transaction = new TransactionScope()) { foreach (var block in sqlScripts.Where(block => block.Length > 0)) { try { parachuteContext.Database.ExecuteSqlCommand(block); success = true; } catch (Exception ex) { TraceHelper.Error(ex.Message); throw; } transaction.Complete(); } } } return success; }
/// <summary> /// Executes the specified SQL scripts in a single transaction for each script. /// </summary> /// <param name="sqlScripts">The SQL scripts.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> public bool Execute(IEnumerable <string> sqlScripts) { var success = false; using (var parachuteContext = new ParachuteContext(_connectionString)) { using (var transaction = new TransactionScope()) { foreach (var block in sqlScripts.Where(block => block.Length > 0)) { try { parachuteContext.Database.ExecuteSqlCommand(block); success = true; } catch (Exception ex) { TraceHelper.Error(ex.Message); throw; } transaction.Complete(); } } } return(success); }
/// <summary> /// Gets the current schema version. /// </summary> /// <returns>The Current Schema Version of the Database</returns> public SchemaVersion GetCurrentSchemaVersion() { using (var pmdc = new ParachuteContext(_connectionString)) { var result = pmdc.ParachuteSchemaChangeLogs .OrderByDescending(l => l.MajorReleaseNumber) .ThenByDescending(l => l.MinorReleaseNumber) .ThenByDescending(l => l.PointReleaseNumber) .FirstOrDefault(); return result == null ? SchemaVersion.MinValue : new SchemaVersion(result.MajorReleaseNumber, result.MinorReleaseNumber, result.PointReleaseNumber); } }
/// <summary> /// Executes the script file. /// </summary> /// <param name="sqlScripts">The SQL scripts.</param> /// <param name="fileName">Name of the file.</param> /// <param name="hash">The hash.</param> /// <param name="version">The version.</param> public void ExecuteScriptFile(IEnumerable<string> sqlScripts, string fileName, string hash, SchemaVersion version) { var success = _parachuteCommand.Execute(sqlScripts); if (!success) return; using (var dc = new ParachuteContext(_connectionString)) { var entry = new ParachuteAppliedScriptsLog { SchemaVersion = version.ToString(), ScriptName = fileName, DateApplied = DateTime.Now, Hash = hash }; dc.ParachuteAppliedScriptsLogs.Add(entry); try { dc.SaveChanges(); } catch (InvalidOperationException invalidOperationEx) { TraceHelper.Error(invalidOperationEx.Message); throw; } catch (Exception ex) { TraceHelper.Error(ex.Message); throw; } } }
/// <summary> /// Executes the schema file. /// Executes a number of sql blocks as scripts and subsequently logs the Schema File in the SchemaChangeLog /// All blocks are executed as a single transaction. /// </summary> /// <param name="sqlScripts">The SQL scripts.</param> /// <param name="fileName">Name of the file.</param> /// <param name="version">The version.</param> public void ExecuteSchemaFile(IEnumerable<string> sqlScripts, string fileName, SchemaVersion version) { var success = _parachuteCommand.Execute(sqlScripts); if (!success) return; using (var dc = new ParachuteContext(_connectionString)) { var entry = new ParachuteSchemaChangeLog { MajorReleaseNumber = version.MajorVersion, MinorReleaseNumber = version.MinorVersion, PointReleaseNumber = version.PointRelease, ScriptName = fileName }; dc.ParachuteSchemaChangeLogs.Add(entry); try { dc.SaveChanges(); } catch (InvalidOperationException invalidOperationEx) { TraceHelper.Error(invalidOperationEx.Message); throw; } catch (Exception ex) { TraceHelper.Error(ex.Message); throw; } } }