Esempio n. 1
0
        public void AddMigrations(DbMigrationSet migrations)
        {
            var migrScripts = migrations.GetActiveSqlMigrations().Select(m => new DbUpgradeScript(m));

            AllScripts.AddRange(migrScripts);
            PostUpgradeMigrations = migrations.GetActiveActionMigrations().ToList();
        }
Esempio n. 2
0
        /// <summary>
        /// Performs Db model update actions, depending on instance type (dev, staging, production).
        /// </summary>
        public DbUpgradeInfo BuildUpgradeInfo()
        {
            //Even if we do not do db model upgrades, we still may have migration actions (post upgrate), which we will run after completing connection to db
            var driver = _database.DbModel.Driver;

            _upgradeInfo = new DbUpgradeInfo(_database.Settings, _database.DbModel);
            var oldDbVersion = LoadDbVersionInfo();

            if (!CheckCanUpgrade(oldDbVersion))
            {
                return(_upgradeInfo);
            }
            var loader = driver.CreateDbModelLoader(_database.Settings, _log);

            if (driver.Supports(DbFeatures.Schemas))
            {
                var schemas = _database.DbModel.Schemas.Select(s => s.Schema).ToList();
                loader.SetSchemasSubset(schemas);
            }
            _upgradeInfo.OldDbModel             = loader.LoadModel();
            _upgradeInfo.OldDbModel.VersionInfo = oldDbVersion;
            //assign prior versions
            //Compare two models and get changes
            var modelComparer = new DbModelComparer();

            modelComparer.BuildDbModelChanges(_upgradeInfo, loader as IDbObjectComparer, _log);
            //build scripts
            var updater = driver.CreateDbModelUpdater(_database.Settings);

            updater.BuildScripts(_upgradeInfo);
            //Add migrations
            var migrSet = new DbMigrationSet(_app, _database, _upgradeInfo.OldDbModel);

            foreach (var module in this._database.DbModel.EntityApp.Modules)
            {
                migrSet.CurrentModule = module;
                module.RegisterMigrations(migrSet);
            }
            migrSet.CurrentModule = null;
            _upgradeInfo.AddMigrations(migrSet);
            //Update final status
            _upgradeInfo.VersionsChanged = oldDbVersion != null && _database.DbModel.VersionInfo.VersionChanged(oldDbVersion);
            if (_upgradeInfo.AllScripts.Count > 0 || _upgradeInfo.VersionsChanged)
            {
                _upgradeInfo.Status = UpgradeStatus.ChangesDetected;
            }
            else
            {
                _upgradeInfo.Status = UpgradeStatus.NoChanges;
            }
            //Sort, Clear up
            _upgradeInfo.AllScripts.Sort(DbUpgradeScript.CompareExecutionOrder);
            _database.DbModel.ResetPeerRefs(); //drop refs to old model
            return(_upgradeInfo);
        }