public int ExecuteUpCommand(dynamic up) { var sql = _sql_formatter.GenerateSQLUp(up); using (var connection = GetOpenConnection()) { var transaction = connection.BeginTransaction(System.Data.IsolationLevel.Serializable); var cmd = connection.CreateCommand(); cmd.Transaction = transaction; try { foreach (var s in sql) { if (_configuration.Verbose) { Console.WriteLine("Executing SQL: " + s); } cmd.CommandText = s; cmd.ExecuteNonQuery(); } transaction.Commit(); } catch { transaction.Rollback(); throw; } finally { connection.Close(); } } return(sql.Length); }
private void CreateSql(SortedDictionary <int, string> migrations, SortedDictionary <int, string> migration_contents, Predicate <KeyValuePair <int, string> > allow_migration_func) { var sql_commands = new Dictionary <string, string>(); int min = migrations.Count; int max = 1; foreach (var migration in migrations) { if (allow_migration_func != null) { if (!allow_migration_func(migration)) { continue; } } if (migration.Key < min) { min = migration.Key; } if (migration.Key > max) { max = migration.Key; } var this_migration_contents = migration_contents[migration.Key]; dynamic migration_object = Helpers.DeserializeMigration(this_migration_contents); var commands = _sql_formatter.GenerateSQLUp(migration_object); if (commands.Length == 1) { sql_commands.Add(_sql_formatter.sql_file_name(migration.Value), commands[0]); } else { for (int i = 0; i < commands.Length; i++) { sql_commands.Add(_sql_formatter.sql_file_name(migration.Value) + "." + (i + 1).ToString("000"), commands[i]); } } } WriteFile(sql_commands, min, max); }
public virtual int ExecuteUpCommand(dynamic up) { var sql = _sql_formatter.GenerateSQLUp(up); // test for file bool has_filesmo = false; try { bool fsmo = up.up.filesmo != null; has_filesmo = fsmo; } catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException) { // intentionally let thru, no smo object } if (has_filesmo) { ExecuteSmo(_settings.ConnectionString(), sql[0]); return(1); } else { using (var connection = GetOpenConnection()) { var transaction = connection.BeginTransaction(System.Data.IsolationLevel.Serializable); var cmd = connection.CreateCommand(); cmd.Transaction = transaction; try { foreach (var s in sql) { if (s.Contains("DROP TABLE")) { if (_configuration.Verbose) { Console.WriteLine("DROPPING CONSTRAINTS BEFORE Executing SQL: " + s); } string table_name = s.Replace("DROP TABLE ", "").Trim(); foreach (var drop_string in DropTableConstraints(table_name, cmd)) { cmd.CommandText = drop_string; cmd.ExecuteNonQuery(); } } if (_configuration.Verbose) { Console.WriteLine("Executing SQL: " + s); } cmd.CommandText = s; cmd.ExecuteNonQuery(); } transaction.Commit(); } catch { transaction.Rollback(); throw; } finally { connection.Close(); } } return(sql.Length); } }