//used in execution ordering public static int CompareExecutionOrder(DbUpgradeScript x, DbUpgradeScript y) { var result = x.ScriptType.CompareTo(y.ScriptType); if (result != 0) return result; result = x.Phase.CompareTo(y.Phase); if (result != 0) return result; result = x._creationOrder.CompareTo(y._creationOrder); return result; }
public DbUpgradeScript AddScript(DbScriptType scriptType, ApplyPhase phase, string sqlTemplate, params object[] args) { string sql = sqlTemplate; if(args != null && args.Length > 0) sql = string.Format(sqlTemplate, args); var script = new DbUpgradeScript(scriptType, sql, this, phase); Scripts.Add(script); return script; }
public DbUpgradeScript AddScript(DbScriptType scriptType, ApplyPhase phase, string sqlTemplate, params object[] args) { Util.CheckNotEmpty(sqlTemplate, "Fatal error: attempt to add empty upgrade SQL scrpt; scriptType: {0}", scriptType); string sql = sqlTemplate; if (args != null && args.Length > 0) { sql = string.Format(sqlTemplate, args); } var script = new DbUpgradeScript(scriptType, sql, this, phase); Scripts.Add(script); return(script); }
public void ApplyUpgrades() { OnUpgrading(); var scriptsStr = string.Join(Environment.NewLine, _upgradeInfo.AllScripts); _log.WriteMessage("Applying DB Upgrades, {0} scripts. -----------------------------", _upgradeInfo.AllScripts.Count); _log.WriteMessage(scriptsStr); _upgradeInfo.StartedOn = _app.TimeService.UtcNow; var driver = _database.DbModel.Driver; var conn = driver.CreateConnection(_database.Settings.SchemaManagementConnectionString); DbUpgradeScript currScript = null; IDbCommand cmd = null; var appliedScripts = new List <DbUpgradeScript>(); try { conn.Open(); foreach (var script in _upgradeInfo.AllScripts) { currScript = script; cmd = conn.CreateCommand(); cmd.CommandText = script.Sql; var start = _app.TimeService.ElapsedMilliseconds; driver.ExecuteCommand(cmd, DbExecutionType.NonQuery); script.Duration = (int)(_app.TimeService.ElapsedMilliseconds - start); appliedScripts.Add(script); //Important for SQLite cmd.Connection = null; cmd.Dispose(); cmd = null; } _upgradeInfo.EndedOn = _app.TimeService.UtcNow; OnUpgraded(_upgradeInfo.AllScripts); _log.WriteMessage("Database upgraded successfully. ---------------------------"); } catch (Exception ex) { OnUpgraded(appliedScripts, ex, currScript); var logStr = ex.ToLogString(); // System.Diagnostics.Debug.WriteLine(logStr); _log.LogError(logStr); throw; } finally { if (cmd != null) { cmd.Connection = null; cmd.Dispose(); } conn.Close(); } }//method
//used in execution ordering public static int CompareExecutionOrder(DbUpgradeScript x, DbUpgradeScript y) { var result = x.ScriptType.CompareTo(y.ScriptType); if (result != 0) { return(result); } result = x.Phase.CompareTo(y.Phase); if (result != 0) { return(result); } result = x._creationOrder.CompareTo(y._creationOrder); return(result); }
public static IDbUpgradeScript NewDbModelChangeScript(this IDbUpgradeBatch batch, DbUpgradeScript script, int index = 0, Exception exception = null) { var session = EntityHelper.GetSession(batch); var ent = session.NewEntity<IDbUpgradeScript>(); ent.Batch = batch; if (script.Migration != null) { ent.ObjectType = DbObjectType.Other; ent.FullObjectName = script.Migration.Name; } else if (script.ModelChange != null) { ent.ObjectType = script.ModelChange.ObjectType; ent.FullObjectName = script.ModelChange.DbObject.GlobalName; } else { ent.ObjectType = DbObjectType.Other; ent.FullObjectName = "(Unknown)"; } ent.ExecutionOrder = index; ent.Sql = script.Sql; ent.Duration = script.Duration; if(exception != null) ent.Errors = exception.ToLogString(); return ent; }
private void OnUpgraded(List <DbUpgradeScript> appliedScripts, DateTime startedOn, DateTime completedOn, Exception exception = null, DbUpgradeScript failedScript = null) { var args = new DbUpgradeEventArgs(_dataSource, DataSourceEventType.DbModelUpdated, appliedScripts, startedOn, completedOn, exception, failedScript); _dataAccess.Events.OnDbUpgraded(args); var logService = _app.GetService <IDbUpgradeLogService>(); if (logService != null) { var oldVersionInfo = _upgradeInfo.OldDbModel.VersionInfo; var oldVersion = oldVersionInfo == null ? DbVersionInfo.ZeroVersion : oldVersionInfo.Version; var batch = new DbUpgradeReport() { Version = _database.DbModel.VersionInfo.Version, OldDbVersion = oldVersion, MachineName = Environment.MachineName, UserName = Environment.UserName, Scripts = appliedScripts, StartedOn = startedOn, CompletedOn = completedOn, Exception = exception, FailedScript = failedScript }; logService.LogDbUpgrade(batch); } }
private void OnUpgraded(List <DbUpgradeScript> appliedScripts, Exception exception = null, DbUpgradeScript failedScript = null) { DbUpgradeEventType eventType = exception == null ? DbUpgradeEventType.DbModelUpgraded : DbUpgradeEventType.Error; var args = new DbUpgradeEventArgs(_database, eventType, _upgradeInfo, exception, failedScript); _app.DataSourceEvents.OnDbUpgraded(args); var logService = (IDbUpgradeLogService)_app.GetService(typeof(IDbUpgradeLogService)); if (logService != null) { var oldVersionInfo = _upgradeInfo.OldDbModel.VersionInfo; var oldVersion = oldVersionInfo == null ? DbVersionInfo.ZeroVersion : oldVersionInfo.Version; var report = new DbUpgradeReport() { Version = _database.DbModel.VersionInfo.Version, OldDbVersion = oldVersion, MachineName = "(Unknown)",// Environment.MachineName, UserName = "******", Scripts = appliedScripts, StartedOn = _upgradeInfo.StartedOn, CompletedOn = _upgradeInfo.EndedOn, Exception = exception, FailedScript = failedScript }; logService.LogDbUpgrade(report); } }
private void OnUpgraded(List<DbUpgradeScript> appliedScripts, DateTime startedOn, DateTime completedOn, Exception exception = null, DbUpgradeScript failedScript = null) { var args = new DbUpgradeEventArgs(_dataSource, DataSourceEventType.DbModelUpdated, appliedScripts, startedOn, completedOn, exception, failedScript); _dataAccess.Events.OnDbUpgraded(args); var logService = _app.GetService<IDbUpgradeLogService>(); if(logService != null) { var oldVersionInfo = _upgradeInfo.OldDbModel.VersionInfo; var oldVersion = oldVersionInfo == null ? DbVersionInfo.ZeroVersion : oldVersionInfo.Version; var batch = new DbUpgradeReport() { Version = _database.DbModel.VersionInfo.Version, OldDbVersion = oldVersion, MachineName = Environment.MachineName, UserName = Environment.UserName, Scripts = appliedScripts, StartedOn = startedOn, CompletedOn = completedOn, Exception = exception, FailedScript = failedScript }; logService.LogDbUpgrade(batch); } }
public DbUpgradeEventArgs(DataSource dataSource, DataSourceEventType eventType, List<DbUpgradeScript> scripts, DateTime startedOn, DateTime? completedOn, Exception exception = null, DbUpgradeScript failedScript = null) { DataSource = dataSource; Scripts = scripts; StartedOn = startedOn; CompletedOn = completedOn; Exception = exception; FailedScript = failedScript; }