Пример #1
0
 public DbUpgradeEventArgs(Database database, DbUpgradeEventType eventType, DbUpgradeInfo upgradeInfo, Exception exception = null, DbUpgradeScript failedScript = null)
 {
     Database     = database;
     EventType    = eventType;
     UpgradeInfo  = upgradeInfo;
     Exception    = exception;
     FailedScript = failedScript;
 }
Пример #2
0
        private void ApplyPostUpgradeMigrations(DbUpgradeInfo upgradeInfo)
        {
            if (upgradeInfo.PostUpgradeMigrations == null || upgradeInfo.PostUpgradeMigrations.Count == 0)
            {
                return;
            }
            var session = _app.OpenSystemSession();

            foreach (var m in upgradeInfo.PostUpgradeMigrations)
            {
                m.Action(session);
            }
            session.SaveChanges();
        }
Пример #3
0
        public virtual void BuildScripts(DbUpgradeInfo upgradeInfo)
        {
            var allChanges = new List <DbObjectChange>(upgradeInfo.NonTableChanges);

            allChanges.AddRange(upgradeInfo.TableChanges.SelectMany(tc => tc.Changes));
            foreach (var change in allChanges)
            {
                BuildScripts(change);
                upgradeInfo.AllScripts.AddRange(change.Scripts);
            }
            if (Test_RandomizeInitialSchemaUpdatesOrder)
            {
                RandomHelper.RandomizeListOrder(upgradeInfo.AllScripts);
            }
        }
Пример #4
0
        public static void DropSchemaObjects(DbSettings settings, List <string> schemas, ILog log)
        {
            var driver      = settings.Driver;
            var model       = LoadDbModel(settings, schemas, log);
            var upgradeInfo = new DbUpgradeInfo(null, model);

            foreach (var sch in model.Schemas)
            {
                if (sch.Schema != "dbo") //just for MS SQL, can never drop this
                {
                    upgradeInfo.AddChange(sch, null);
                }
            }
            foreach (var tbl in model.Tables)
            {
                upgradeInfo.AddChange(tbl, null);
                foreach (var refc in tbl.RefConstraints)
                {
                    upgradeInfo.AddChange(refc, null);
                }
                foreach (var key in tbl.Keys)
                {
                    if (key.KeyType.IsSet(KeyType.Index))
                    {
                        upgradeInfo.AddChange(key, null);
                    }
                }
            }
            //  foreach (var custType in model.CustomDbTypes)
            //    upgradeInfo.AddChange(custType, null);
            foreach (var seq in model.Sequences)
            {
                upgradeInfo.AddChange(seq, null);
            }

            var updater = driver.CreateDbModelUpdater(settings);

            updater.BuildScripts(upgradeInfo);
            upgradeInfo.AllScripts.Sort(DbUpgradeScript.CompareExecutionOrder);
            //apply
            var             conn = driver.CreateConnection(settings.SchemaManagementConnectionString);
            IDbCommand      currCmd;
            DbUpgradeScript currScript = null;

            try {
                conn.Open();
                foreach (var script in upgradeInfo.AllScripts)
                {
                    currScript          = script;
                    currCmd             = conn.CreateCommand();
                    currCmd.CommandText = script.Sql;
                    driver.ExecuteCommand(currCmd, DbExecutionType.NonQuery);
                }
            } catch (Exception ex) {
                var logStr = ex.ToLogString();
                System.Diagnostics.Debug.WriteLine(logStr);
                var allScripts = string.Join(Environment.NewLine, upgradeInfo.AllScripts);
                Debug.WriteLine("SCRIPTS: \r\n" + allScripts);
                if (currScript != null)
                {
                    Debug.WriteLine("Failed script: " + currScript);
                }
                throw;
            } finally {
                conn.Close();
            }
        }
Пример #5
0
        public static void DropSchemaObjects(DbSettings settings)
        {
            var driver = settings.ModelConfig.Driver;
            var loader = driver.CreateDbModelLoader(settings, null);

            var model       = loader.LoadModel();
            var upgradeInfo = new DbUpgradeInfo(null, model);

            foreach (var sch in model.Schemas)
            {
                if (sch.Schema != "dbo") //just for MS SQL, can never drop this
                {
                    upgradeInfo.AddChange(sch, null);
                }
            }
            foreach (var tbl in model.Tables)
            {
                upgradeInfo.AddChange(tbl, null);
                foreach (var refc in tbl.RefConstraints)
                {
                    upgradeInfo.AddChange(refc, null);
                }
                foreach (var key in tbl.Keys)
                {
                    if (key.KeyType.IsSet(KeyType.Index))
                    {
                        upgradeInfo.AddChange(key, null);
                    }
                }
            }
            //  foreach (var custType in model.CustomDbTypes)
            //    upgradeInfo.AddChange(custType, null);
            foreach (var cmd in model.Commands)
            {
                upgradeInfo.AddChange(cmd, null);
            }
            foreach (var seq in model.Sequences)
            {
                upgradeInfo.AddChange(seq, null);
            }

            var updater = driver.CreateDbModelUpdater(settings);

            updater.BuildScripts(upgradeInfo);
            upgradeInfo.AllScripts.Sort(DbUpgradeScript.CompareExecutionOrder);
            //apply
            var conn = driver.CreateConnection(settings.SchemaManagementConnectionString);

            try {
                conn.Open();
                foreach (var script in upgradeInfo.AllScripts)
                {
                    var cmd = conn.CreateCommand();
                    cmd.CommandText = script.Sql;
                    driver.ExecuteCommand(cmd, DbExecutionType.NonQuery);
                }
            } catch (Exception ex) {
                var logStr = ex.ToLogString();
                System.Diagnostics.Debug.WriteLine(logStr);
                throw;
            } finally {
                conn.Close();
            }
        }
Пример #6
0
 public override void BuildScripts(DbUpgradeInfo upgradeInfo)
 {
     base.BuildScripts(upgradeInfo);
 }
Пример #7
0
        public static bool CanLogModelChanges(this DbUpgradeInfo changeSet)
        {
            var hasChanges = changeSet.EntitiesChanging(DbUpgradeLogModule.EntityTypes);

            return(!hasChanges);
        }