Exemplo n.º 1
0
        /// <summary>
        /// Setup database.
        /// </summary>
        private void SetupMigration()
        {
            var migrations      = ApplicationContext.Current.Services.MigrationEntryService.GetAll(ApplicationConstants.ProductName);
            var latestMigration = migrations.OrderByDescending(x => x.Version).FirstOrDefault();

            var currentVersion = latestMigration != null ? latestMigration.Version : new SemVersion(0, 0, 0);

            var targetVersion = new SemVersion(0, 5, 1);

            if (targetVersion != currentVersion)
            {
                var migrationsRunner = new MigrationRunner(
                    ApplicationContext.Current.Services.MigrationEntryService,
                    ApplicationContext.Current.ProfilingLogger.Logger,
                    currentVersion,
                    targetVersion,
                    ApplicationConstants.ProductName);

                try
                {
                    migrationsRunner.Execute(UmbracoContext.Current.Application.DatabaseContext.Database);
                }
                catch (Exception e)
                {
                    LogHelper.Error <Startup>("Error running Statistics migration", e);
                }
            }
        }
        public void Executes_Only_For_Specified_Product_Name()
        {
            var changed1 = new Args {
                CountExecuted = 0
            };
            var testHandler1 = new TestMigrationHandler("Test1", changed1);

            testHandler1.OnApplicationStarting(Mock.Of <UmbracoApplicationBase>(), new ApplicationContext(CacheHelper.CreateDisabledCacheHelper(), new ProfilingLogger(Mock.Of <ILogger>(), Mock.Of <IProfiler>())));
            var changed2 = new Args {
                CountExecuted = 0
            };
            var testHandler2 = new TestMigrationHandler("Test2", changed2);

            testHandler2.OnApplicationStarting(Mock.Of <UmbracoApplicationBase>(), new ApplicationContext(CacheHelper.CreateDisabledCacheHelper(), new ProfilingLogger(Mock.Of <ILogger>(), Mock.Of <IProfiler>())));

            var conn = new Mock <IDbConnection>();

            conn.Setup(x => x.BeginTransaction(It.IsAny <IsolationLevel>())).Returns(Mock.Of <IDbTransaction>());
            var db = new Mock <Database>(conn.Object);

            var runner1 = new MigrationRunner(Mock.Of <IMigrationEntryService>(), Mock.Of <ILogger>(), new SemVersion(1), new SemVersion(2), "Test1",
                                              new IMigration[] { Mock.Of <IMigration>() });
            var result1 = runner1.Execute(db.Object, DatabaseProviders.SqlServerCE, false);

            Assert.AreEqual(1, changed1.CountExecuted);
            Assert.AreEqual(0, changed2.CountExecuted);

            var runner2 = new MigrationRunner(Mock.Of <IMigrationEntryService>(), Mock.Of <ILogger>(), new SemVersion(1), new SemVersion(2), "Test2",
                                              new IMigration[] { Mock.Of <IMigration>() });
            var result2 = runner2.Execute(db.Object, DatabaseProviders.SqlServerCE, false);

            Assert.AreEqual(1, changed1.CountExecuted);
            Assert.AreEqual(1, changed2.CountExecuted);
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sqlSyntax"></param>
        /// <param name="migrationEntryService"></param>
        /// <param name="logger"></param>
        public void RunMigrations(ISqlSyntaxProvider sqlSyntax, IMigrationEntryService migrationEntryService, ILogger logger)
        {
            var currentVersion  = new SemVersion(0);
            var migrations      = ApplicationContext.Current.Services.MigrationEntryService.GetAll(ProductName).OrderByDescending(x => x.CreateDate);
            var latestMigration = migrations.FirstOrDefault();

            if (latestMigration != null)
            {
                currentVersion = latestMigration.Version;
            }

            if (TargetVersion == currentVersion)
            {
                return;
            }

            IMigration[] scriptsForMigration =
            {
                (IMigration) new Versions.Migration100(sqlSyntax, logger)
            };

            MigrationRunner migrationsRunner = new MigrationRunner(migrationEntryService, logger, currentVersion, TargetVersion,
                                                                   ProductName, scriptsForMigration);

            try
            {
                migrationsRunner.Execute(ApplicationContext.Current.DatabaseContext.Database);
            }
            catch (Exception ex)
            {
                LogHelper.Error <Migration>($"Error running {ProductName} migration", ex);
            }
        }
Exemplo n.º 4
0
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            var targetDbVersion  = new SemVersion(2, 0, 0); // Update this whenever a migration change is made
            var currentDbVersion = new SemVersion(0, 0, 0);

            var migrations      = ApplicationContext.Current.Services.MigrationEntryService.GetAll(Constants.ApplicationAlias);
            var latestMigration = migrations.OrderByDescending(x => x.Version).FirstOrDefault();

            if (latestMigration != null)
            {
                currentDbVersion = latestMigration.Version;
            }

            if (targetDbVersion == currentDbVersion)
            {
                return;
            }

            var migrationsRunner = new MigrationRunner(
                ApplicationContext.Current.Services.MigrationEntryService,
                ApplicationContext.Current.ProfilingLogger.Logger,
                currentDbVersion,
                targetDbVersion,
                Constants.ApplicationAlias);

            try
            {
                migrationsRunner.Execute(UmbracoContext.Current.Application.DatabaseContext.Database);
            }
            catch (Exception e)
            {
                //LogHelper.Error<Installer>("Error running UI-O-Matic migrations", e);
            }
        }
Exemplo n.º 5
0
        private static void DoMigration()
        {
            var currentVersion = new SemVersion(0);

            var migrations = ApplicationContext.Current.Services.MigrationEntryService.GetAll(MagicStrings.Name);
            var latest     = migrations.OrderByDescending(x => x.Version).FirstOrDefault();

            if (null != latest)
            {
                currentVersion = latest.Version;
            }

            var targetVersion = new SemVersion(0, 7);

            if (targetVersion == currentVersion)
            {
                return;
            }

            var migrationsRunner = new MigrationRunner(
                ApplicationContext.Current.Services.MigrationEntryService,
                ApplicationContext.Current.ProfilingLogger.Logger,
                currentVersion,
                targetVersion,
                MagicStrings.Name);

            try
            {
                migrationsRunner.Execute(UmbracoContext.Current.Application.DatabaseContext.Database);
            }
            catch (Exception ex)
            {
                LogHelper.Error <Migration>("Error running Plumber migration", ex);
            }
        }
        public void CreateTableOfTDto()
        {
            var logger = new DebugDiagnosticsLogger();

            var migrationRunner = new MigrationRunner(
                Mock.Of <IMigrationEntryService>(),
                logger,
                new SemVersion(0, 0, 0),
                new SemVersion(1, 0, 0),
                "Test",

                // explicit migrations
                new CreateTableOfTDtoMigration(SqlSyntax, logger)
                );

            var db = new UmbracoDatabase("Datasource=|DataDirectory|UmbracoPetaPocoTests.sdf;Flush Interval=1;", Constants.DatabaseProviders.SqlCe, Logger);

            var upgraded = migrationRunner.Execute(db, DatabaseProviders.SqlServerCE, true);

            Assert.IsTrue(upgraded);

            var helper = new DatabaseSchemaHelper(db, logger, SqlSyntax);
            var exists = helper.TableExist("umbracoNode");

            Assert.IsTrue(exists);
        }
Exemplo n.º 7
0
        private void InstalledPackage_BeforeDelete(InstalledPackage sender, EventArgs e)
        {
            if (sender.Data.Name == Constants.PackageName)
            {
                try
                {
                    var mes    = ApplicationContext.Current.Services.MigrationEntryService;
                    var logger = ApplicationContext.Current.ProfilingLogger.Logger;

                    var migrationsRunner = new MigrationRunner(
                        mes,
                        logger,
                        new SemVersion(0),
                        new SemVersion(Constants.VersionNo),
                        Constants.PackageName);

                    var db = UmbracoContext.Current.Application.DatabaseContext.Database;

                    //calls the down method on migration UpdateExamineConfigFiles however the db entry for migration is not removed
                    //need to do that manually
                    migrationsRunner.Execute(db, false);

                    RemoveMigrationFromDb(db);
                }
                catch (Exception ex)
                {
                    LogHelper.Error <MigrationEvents>("Error running DemoPackage migration", ex);
                }
            }
        }
Exemplo n.º 8
0
        private void ApplyMigrations(ApplicationContext applicationContext, string productName, SemVersion targetVersion)
        {
            var currentVersion = new SemVersion(0);

            var migrations = applicationContext.Services.MigrationEntryService.GetAll(productName);
            var latest     = migrations.OrderByDescending(x => x.Version).FirstOrDefault();

            if (latest != null)
            {
                currentVersion = latest.Version;
            }

            if (targetVersion == currentVersion)
            {
                return;
            }

            var migrationRunner = new MigrationRunner(
                applicationContext.Services.MigrationEntryService,
                applicationContext.ProfilingLogger.Logger,
                currentVersion,
                targetVersion,
                productName);

            try
            {
                migrationRunner.Execute(applicationContext.DatabaseContext.Database);
            }
            catch (Exception ex)
            {
                applicationContext.ProfilingLogger
                .Logger.Error <MigrationEventHandler>("Error running " + productName + " Migration", ex);
            }
        }
Exemplo n.º 9
0
        public override void Can_Upgrade_From_470_To_600()
        {
            var configuredVersion = new SemVersion(4, 11, 0);
            var targetVersion     = new SemVersion(6, 0, 0);
            var provider          = GetDatabaseProvider();
            var db = GetConfiguredDatabase();

            var fix = new PublishAfterUpgradeToVersionSixth();

            //Setup the MigrationRunner
            var migrationRunner = new MigrationRunner(
                Mock.Of <IMigrationEntryService>(),
                Mock.Of <ILogger>(), configuredVersion, targetVersion, GlobalSettings.UmbracoMigrationName);

            bool upgraded = migrationRunner.Execute(db, provider, true);

            Assert.That(upgraded, Is.True);

            bool hasTabTable = db.TableExist("cmsTab");
            bool hasPropertyTypeGroupTable = db.TableExist("cmsPropertyTypeGroup");
            bool hasAppTreeTable           = db.TableExist("umbracoAppTree");

            fix.Unsubscribe();

            Assert.That(hasTabTable, Is.False);
            Assert.That(hasPropertyTypeGroupTable, Is.True);
            Assert.That(hasAppTreeTable, Is.False);
        }
Exemplo n.º 10
0
        public void ApplyMigration(string productName, SemVersion targetVersion)
        {
            var currentVersion = new SemVersion(0);

            var migrations = _migrationService.GetAll(productName);
            var latest     = migrations.OrderByDescending(x => x.Version).FirstOrDefault();

            if (latest != null)
            {
                currentVersion = latest.Version;
            }

            if (targetVersion == currentVersion)
            {
                return;
            }


            var migrationRunner = new MigrationRunner(
                _migrationService,
                _logger,
                currentVersion,
                targetVersion,
                productName);

            try
            {
                migrationRunner.Execute(_databaseContext.Database);
            }
            catch (Exception ex)
            {
                _logger.Error <MigrationManager>(string.Format("Error running {0} version: {1}", productName, targetVersion), ex);
            }
        }
Exemplo n.º 11
0
        private void ApplyMigrations(ApplicationContext applicationContext, string productName, Version currentVersion, Version targetVersion)
        {
            // TODO: [LK] If Mortar supports a version of Umbraco later than v7.3.0,
            // then we can make use of the MigrationEntryService database table.

            if (currentVersion == null)
            {
                currentVersion = new Version(0, 0, 0);
            }

            if (targetVersion == currentVersion)
            {
                return;
            }

            var migrationRunner = new MigrationRunner(currentVersion, targetVersion, productName);

            try
            {
                migrationRunner.Execute(applicationContext.DatabaseContext.Database);
            }
            catch (System.Web.HttpException)
            {
                // because umbraco runs some other migrations after the migration runner
                // is executed we get HttpException
                // catch this error, but don't do anything
                // fixed in 7.4.2+ see : http://issues.umbraco.org/issue/U4-8077
            }
            catch (Exception ex)
            {
                LogHelper.Error <Boostrapper>("Error running migration.", ex);
            }
        }
Exemplo n.º 12
0
        public void Run()
        {
            var installedVersion = new SemVersion(0);
            var logger           = _applicationContext.ProfilingLogger.Logger;

            // get latest executed migration
            var service    = _applicationContext.Services.MigrationEntryService;
            var migrations = service.GetAll(MigrationProductName);
            var latest     = migrations.OrderByDescending(x => x.Version).FirstOrDefault();

            if (latest != null)
            {
                installedVersion = latest.Version;
            }

            // compare to this version
            logger.Debug <Migrations>("Versions: this=" + Constants.Version + ", installed=" + installedVersion);
            if (Constants.Version == installedVersion)
            {
                return;
            }

            var migrationsRunner = new MigrationRunner(service, logger, installedVersion, Constants.Version, MigrationProductName);

            try
            {
                var database = _applicationContext.DatabaseContext.Database;
                migrationsRunner.Execute(database);
            }
            catch (Exception e)
            {
                logger.Error <Migrations>("Failed to run migrations.", e);
            }
        }
Exemplo n.º 13
0
        public static void RunMigrations(string version, string productName)
        {
            var currentVersion = new SemVersion(0, 0, 0);

            var migrations      = ApplicationContext.Current.Services.MigrationEntryService.GetAll(productName);
            var latestMigration = migrations.OrderByDescending(x => x.Version).FirstOrDefault();

            if (latestMigration != null)
            {
                currentVersion = latestMigration.Version;
            }

            var targetVersion = SemVersion.Parse(version);

            if (targetVersion == currentVersion)
            {
                return;
            }

            var migrationsRunner = new MigrationRunner(
                ApplicationContext.Current.Services.MigrationEntryService,
                ApplicationContext.Current.ProfilingLogger.Logger,
                currentVersion,
                targetVersion,
                productName);

            try
            {
                migrationsRunner.Execute(ApplicationContext.Current.DatabaseContext.Database);
            }
            catch (Exception e)
            {
                LogHelper.Error <MigrationsRunner>($"Error running {productName} migration", e);
            }
        }
Exemplo n.º 14
0
        public virtual void Can_Upgrade_From_470_To_600()
        {
            var configuredVersion = new SemVersion(4, 7, 0);
            var targetVersion     = new SemVersion(6, 0, 0);
            var provider          = GetDatabaseProvider();
            var db = GetConfiguredDatabase();

            //Create db schema and data from old Total.sql file for Sql Ce
            string statements = GetDatabaseSpecificSqlScript();

            // replace block comments by whitespace
            statements = FindComments.Replace(statements, " ");
            // execute all non-empty statements
            foreach (string statement in statements.Split(";".ToCharArray()))
            {
                string rawStatement = statement.Replace("GO", "").Trim();
                if (rawStatement.Length > 0)
                {
                    db.Execute(new Sql(rawStatement));
                }
            }

            var logger = Mock.Of <ILogger>();
            var sql    = GetSyntaxProvider();

            //Setup the MigrationRunner
            var migrationRunner = new MigrationRunner(
                Mock.Of <IMigrationEntryService>(),
                logger,
                configuredVersion,
                targetVersion,
                Constants.System.UmbracoMigrationName,
                //pass in explicit migrations
                new Core.Persistence.Migrations.Upgrades.TargetVersionFourNineZero.RemoveUmbracoAppConstraints(sql, logger),
                new DeleteAppTables(sql, logger),
                new EnsureAppsTreesUpdated(sql, logger),
                new MoveMasterContentTypeData(sql, logger),
                new NewCmsContentType2ContentTypeTable(sql, logger),
                new RemoveMasterContentTypeColumn(sql, logger),
                new RenameCmsTabTable(sql, logger),
                new RenameTabIdColumn(sql, logger),
                new UpdateCmsContentTypeAllowedContentTypeTable(sql, logger),
                new UpdateCmsContentTypeTable(sql, logger),
                new UpdateCmsContentVersionTable(sql, logger),
                new UpdateCmsPropertyTypeGroupTable(sql, logger));

            bool upgraded = migrationRunner.Execute(db, provider, true);

            Assert.That(upgraded, Is.True);

            bool hasTabTable = db.TableExist("cmsTab");
            bool hasPropertyTypeGroupTable = db.TableExist("cmsPropertyTypeGroup");
            bool hasAppTreeTable           = db.TableExist("umbracoAppTree");

            Assert.That(hasTabTable, Is.False);
            Assert.That(hasPropertyTypeGroupTable, Is.True);
            Assert.That(hasAppTreeTable, Is.False);
        }
Exemplo n.º 15
0
 private void RunMigrations()
 {
     try
     {
         var runner   = new MigrationRunner(this.oldVersion, this.newVersion, "UpdateEventCalendarTables");
         var upgraded = runner.Execute(this._db, true);
         LogHelper.Info <installer>("Done doing migration for version " + this.newVersion.ToString());
     }
     catch (Exception ex) { LogHelper.Error <installer>("Failed to do the migration for a version", ex); }
 }
Exemplo n.º 16
0
        public bool Execute(SemVersion from, SemVersion to)
        {
            var runner = new MigrationRunner(
                migrationEntryService,
                logger,
                from,
                to,
                Constants.System.UmbracoMigrationName);

            return(runner.Execute(database, isUpgrade: true));
        }
Exemplo n.º 17
0
        private void RunMigrations(IApp app, ApplicationContext applicationContext)
        {
            if (!app.Migrations.Any())
            {
                return;
            }

            var currentVersion = new SemVersion(0);
            var productName    = nameof(Shield) + app.Id;

            var migrations      = ApplicationContext.Current.Services.MigrationEntryService.GetAll(productName);
            var latestMigration = migrations.OrderByDescending(x => x.Version).FirstOrDefault();

            if (latestMigration != null)
            {
                currentVersion = latestMigration.Version;
            }

            var latestVersion = currentVersion;

            foreach (var migration in app.Migrations)
            {
                var semver = new SemVersion(new Version(migration.Key));
                if (latestVersion < semver)
                {
                    latestVersion = semver;
                }
            }

            if (latestVersion == currentVersion)
            {
                return;
            }

            var logger = applicationContext.ProfilingLogger.Logger;

            var migrationsRunner = new MigrationRunner(
                applicationContext.Services.MigrationEntryService,
                logger,
                currentVersion,
                latestVersion,
                productName,
                app.Migrations.Values.ToArray());

            try
            {
                migrationsRunner.Execute(ApplicationContext.Current.DatabaseContext.Database);
            }
            catch (Exception ex)
            {
                logger.Error(GetType(), $"Error running {productName} migration", ex);
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// Determines if the database schema needs to be upgraded with a version update
        /// </summary>
        internal static bool VerifyDatabaseSchema(Database database)
        {
            if (!MerchelloConfiguration.Current.AutoUpdateDbSchema)
            {
                return(true);
            }

            // Check if merchello has been upgraded
            if (MerchelloConfiguration.ConfigurationStatus == MerchelloVersion.Current.ToString())
            {
                return(true);
            }

            LogHelper.Info <CoreBootManager>("Beginning Merchello DB Schema Upgrade");

            // 1.0.1 was Merchello's first public release
            var configVersion = String.IsNullOrEmpty(MerchelloConfiguration.ConfigurationStatus)
                ? "1.0.1"
                : MerchelloConfiguration.ConfigurationStatus;

            var currentVersion = new Version(configVersion);
            var targetVersion  = MerchelloVersion.Current;

            var isUpgrage = IsUpgrade(currentVersion, targetVersion);

            var migration = new MigrationRunner(currentVersion, MerchelloVersion.Current, MerchelloConfiguration.MerchelloMigrationName);

            if (migration.Execute(database, isUpgrage))
            {
                try
                {
                    // update the web.config merchelloConfigurationVersion
                    var config = WebConfigurationManager.OpenWebConfiguration("~");
                    config.AppSettings.Settings["merchelloConfigurationStatus"].Value = MerchelloVersion.Current.ToString();

                    config.Save(ConfigurationSaveMode.Modified);
                }
                catch (Exception ex)
                {
                    LogHelper.Error <CoreBootManager>("Failed to update 'merchelloConfigurationStatus' AppSetting", ex);
                    return(false);
                }
            }

            LogHelper.Info <CoreBootManager>("Finished Merchello DB Schema Upgrade");

            return(true);
        }
            public void WhenRunningMigrationFromSettings()
            {
                _versionValidator = new Mock <IVersionValidator>();
                _versionValidator
                .Setup(v => v.Validate(It.IsAny <ISimpleMigrator>(), It.IsAny <Settings>()))
                .Returns(VersionValidation.TargetVersionIsSameAsCurrent);

                _applyProcess = new Mock <IApplyProcess>();
                _noOpProcess  = new Mock <INoOpProcess>();

                _migrator = new Mock <ISimpleMigrator>();
                _settings = new Settings("connection", 100, Mode.Apply, "assembly");

                _versionOutputHelper = new Mock <IVersionOutputHelper>();

                var runner = new MigrationRunner(_migrator.Object, _versionValidator.Object, _noOpProcess.Object, _applyProcess.Object, _versionOutputHelper.Object);

                _result = runner.Execute(_settings);
            }
            public void WhenRunningMigrationFromSettings()
            {
                _versionValidator = new Mock <IVersionValidator>();
                _versionValidator
                .Setup(v => v.Validate(It.IsAny <ISimpleMigrator>(), It.IsAny <Settings>()))
                .Returns(VersionValidation.Valid);

                _noOpProcess = new Mock <INoOpProcess>();

                _settings = new Settings("connection", 1, Mode.NoOp, "test.dll");

                _migrator = new Mock <ISimpleMigrator>();

                _versionOutputHelper = new Mock <IVersionOutputHelper>();

                var runner = new MigrationRunner(_migrator.Object, _versionValidator.Object, _noOpProcess.Object, null, _versionOutputHelper.Object);

                _result = runner.Execute(_settings);
            }
Exemplo n.º 21
0
        // Handle migrations to version 2.0
        private static void HandleV20Migrations()
        {
            const string productName    = "Notely";
            var          currentVersion = new SemVersion(0, 0, 0);

            // Get all migrations
            var migrations = ApplicationContext.Current.Services.MigrationEntryService.GetAll(productName);

            // Get the latest migration
            var latestMigration = migrations.OrderByDescending(x => x.Version).FirstOrDefault();

            if (latestMigration != null)
            {
                currentVersion = latestMigration.Version;
            }

            var targetVersion = new SemVersion(2, 0);

            if (targetVersion == currentVersion)
            {
                return;
            }

            var migrationsRunner = new MigrationRunner(
                ApplicationContext.Current.Services.MigrationEntryService,
                ApplicationContext.Current.ProfilingLogger.Logger,
                currentVersion,
                targetVersion,
                productName
                );

            try
            {
                migrationsRunner.Execute(UmbracoContext.Current.Application.DatabaseContext.Database);
            }
            catch (Exception e)
            {
                LogHelper.Error <MigrationRunner>("Error running Notely migrations", e);
            }
        }
Exemplo n.º 22
0
        private static void HandleMigrations(ApplicationContext context)
        {
            const string productName = Constants.PluginName;

            try
            {
                var currentVersion = new SemVersion(0, 0, 0);

                // get all migrations already executed
                var migrations = context.Services.MigrationEntryService.GetAll(productName);

                // get the latest migration for "UDF" executed
                var latestMigration = migrations.OrderByDescending(x => x.Version).FirstOrDefault();

                if (latestMigration != null)
                {
                    currentVersion = latestMigration.Version;
                }

                var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version;
                var targetVersion   = new SemVersion(assemblyVersion);
                if (targetVersion == currentVersion)
                {
                    return;
                }

                var migrationsRunner = new MigrationRunner(
                    context.Services.MigrationEntryService,
                    context.ProfilingLogger.Logger,
                    currentVersion,
                    targetVersion,
                    productName);

                migrationsRunner.Execute(context.DatabaseContext.Database);
            }
            catch (Exception e)
            {
                LogHelper.Error <Runner>("Error running " + productName + " migrations", e);
            }
        }
Exemplo n.º 23
0
        private SemVersion ApplyMigration(ApplicationContext applicationContext, string productName, string targetVersion)
        {
            var currentVersion = new SemVersion(0);

            var migrations = applicationContext.Services.MigrationEntryService.GetAll(productName);
            var latest     = migrations.OrderByDescending(x => x.Version)
                             .FirstOrDefault();

            if (latest != null)
            {
                currentVersion = latest.Version;
            }

            using (applicationContext.ProfilingLogger.DebugDuration <MigrationHelper>("Running Migrations: " + productName, "Migrations for " + productName + " complete"))
            {
                if (targetVersion == currentVersion)
                {
                    return(currentVersion);
                }

                var migrationRunner = new MigrationRunner(
                    applicationContext.Services.MigrationEntryService,
                    applicationContext.ProfilingLogger.Logger,
                    currentVersion,
                    targetVersion,
                    productName);

                try
                {
                    migrationRunner.Execute(applicationContext.DatabaseContext.Database);
                }
                catch (Exception ex)
                {
                    applicationContext.ProfilingLogger.Logger.Error <MigrationHelper>("Error running migration for " + productName, ex);
                }

                return(currentVersion);
            }
        }
Exemplo n.º 24
0
        private static void HandleStatisticsMigration()
        {
            // Declare an initial version (will be used as fallback fro new install)
            SemVersion currentVersion = new SemVersion(0);

            // Get all migrations for "Skybrud.Redirects" already executed
            IEnumerable <IMigrationEntry> migrations = ApplicationContext.Current.Services.MigrationEntryService.GetAll(Package.Alias);

            // Get the latest migration for "Skybrud.Redirects" executed
            IMigrationEntry latestMigration = migrations.OrderByDescending(x => x.Version).FirstOrDefault();

            if (latestMigration != null)
            {
                currentVersion = latestMigration.Version;
            }

            // Get the target version (and return if already up-to-date)
            SemVersion targetVersion = Package.SemVersion;

            if (targetVersion == currentVersion)
            {
                return;
            }

            // Initialize a new migration runner for our package
            MigrationRunner migrationsRunner = new MigrationRunner(
                ApplicationContext.Current.Services.MigrationEntryService,
                ApplicationContext.Current.ProfilingLogger.Logger,
                currentVersion,
                targetVersion,
                Package.Alias
                );

            try {
                migrationsRunner.Execute(UmbracoContext.Current.Application.DatabaseContext.Database);
            } catch (Exception e) {
                LogHelper.Error <MigrationEvents>("Error running " + Package.Alias + " migration", e);
            }
        }
    private static void HandleStatisticsMigration()
    {
        const string productName    = "YourTableName";
        var          currentVersion = new SemVersion(0, 0, 0);

        // get all migrations for "YourTableName" already executed
        var migrations = ApplicationContext.Current.Services.MigrationEntryService.GetAll(productName);

        // get the latest migration for "YourTableName" executed
        var latestMigration = migrations.OrderByDescending(x => x.Version).FirstOrDefault();

        if (latestMigration != null)
        {
            currentVersion = latestMigration.Version;
        }

        var targetVersion = new SemVersion(1, 0, 1);

        if (targetVersion == currentVersion)
        {
            return;
        }

        var migrationsRunner = new MigrationRunner(
            ApplicationContext.Current.Services.MigrationEntryService,
            ApplicationContext.Current.ProfilingLogger.Logger,
            currentVersion,
            targetVersion,
            productName);

        try
        {
            migrationsRunner.Execute(UmbracoContext.Current.Application.DatabaseContext.Database);
        }
        catch (Exception e)
        {
            LogHelper.Error <MyApplication>("Error running YourTableName migration", e);
        }
    }
        /// <summary>
        /// Standard Migrations Code, with the addition of an optional param
        /// to allow for calling the installation code directly for error handling/debugging purposes.
        /// </summary>
        /// <param name="CurrentVersion">Version to use to override current version as recorded in Migrations Table.</param>
        internal static void HandlePageCounterMigration(SemVersion CurrentVersion)
        {
            //Update this to trigger new migrations to run
            var targetVersion = new SemVersion(1, 0, 0);

            LogHelper.Debug <MigrationEvents>($"HandlePageCounterMigration running for {Constants.ProductName} version {targetVersion}...");

            //Initialize variable (should always be 0,0,0)
            // var currentVersion = new SemVersion(0, 0, 0);
            var currentVersion = CurrentVersion;

            LogHelper.Debug <MigrationEvents>($"HandlePageCounterMigration: {Constants.ProductName} current version is {currentVersion}...");

            if (targetVersion == currentVersion)
            {
                LogHelper.Debug <MigrationEvents>($"HandlePageCounterMigration: {Constants.ProductName} is up-to-date. No need to update.");
                return;
            }

            LogHelper.Info <MigrationEvents>($"HandlePageCounterMigration: {Constants.ProductName} current version is {currentVersion} and target version is {targetVersion}. Updating...");

            var migrationsRunner = new MigrationRunner(
                ApplicationContext.Current.Services.MigrationEntryService,
                ApplicationContext.Current.ProfilingLogger.Logger,
                currentVersion,
                targetVersion,
                Constants.ProductName);

            try
            {
                migrationsRunner.Execute(UmbracoContext.Current.Application.DatabaseContext.Database);
                LogHelper.Info <MigrationEvents>($"HandlePageCounterMigration: {Constants.ProductName} migrations run. ");
            }
            catch (Exception e)
            {
                LogHelper.Error <MigrationEvents>($"Error running {Constants.ProductName} migration", e);
            }
        }
Exemplo n.º 27
0
        private static void HandleTwoFactorMigration(ApplicationContext applicationContext)
        {
            var currentVersion = new SemVersion(0, 0, 0);

            // get all migrations for "TwoFactor" already executed
            var migrations = applicationContext.Services.MigrationEntryService.GetAll(Constants.ProductName);

            // get the latest migration for "TwoFactor" executed
            var latestMigration = migrations.OrderByDescending(x => x.Version).FirstOrDefault();

            if (latestMigration != null)
            {
                currentVersion = latestMigration.Version;
            }

            var targetVersion = new SemVersion(1, 0, 0);

            if (targetVersion == currentVersion)
            {
                return;
            }

            var migrationsRunner = new MigrationRunner(
                applicationContext.Services.MigrationEntryService,
                applicationContext.ProfilingLogger.Logger,
                currentVersion,
                targetVersion,
                Constants.ProductName);

            try
            {
                migrationsRunner.Execute(applicationContext.DatabaseContext.Database);
            }
            catch (Exception e)
            {
                LogHelper.Error <MigrationEvents>("Error running TwoFactor migration", e);
            }
        }
        public void Issue8361Test()
        {
            var logger = new DebugDiagnosticsLogger();

            //Setup the MigrationRunner
            var migrationRunner = new MigrationRunner(
                Mock.Of <IMigrationEntryService>(),
                logger,
                new SemVersion(7, 4, 0),
                new SemVersion(7, 5, 0),
                Constants.System.UmbracoMigrationName,

                //pass in explicit migrations
                new DeleteRedirectUrlTable(SqlSyntax, logger),
                new AddRedirectUrlTable(SqlSyntax, logger)
                );

            var db = new UmbracoDatabase("Datasource=|DataDirectory|UmbracoPetaPocoTests.sdf;Flush Interval=1;", Constants.DatabaseProviders.SqlCe, Logger);

            var upgraded = migrationRunner.Execute(db, DatabaseProviders.SqlServerCE, true);

            Assert.IsTrue(upgraded);
        }
        public void HandleMigration(string migrationName, Version targetVersion)
        {
            var currentVersion   = new SemVersion(0, 0, 0);
            var targetSemVersion = new SemVersion(targetVersion);

            var migrations = _migrationEntryService.GetAll(migrationName);

            var latestMigration = migrations
                                  .OrderByDescending(x => x.Version)
                                  .FirstOrDefault();

            if (latestMigration != null)
            {
                currentVersion = latestMigration.Version;
            }

            if (targetSemVersion == currentVersion)
            {
                return;
            }

            var migrationsRunner = new MigrationRunner(
                _migrationEntryService,
                _logger,
                currentVersion,
                targetSemVersion,
                migrationName);

            try
            {
                migrationsRunner.Execute(_databaseContext.Database);
            }
            catch (Exception e)
            {
                LogHelper.Error <MigrationEvents>("Error running " + migrationName + " migration", e);
            }
        }
Exemplo n.º 30
0
        /// <summary>
        /// Handles checking and running migrations
        /// </summary>
        /// <param name="db">Database context</param>
        /// <param name="migrationService">Migration service</param>
        private void HandleMigrations(UmbracoDatabase db, IMigrationEntryService migrationService)
        {
            var latestMigrationVersion = new SemVersion(0, 0, 0);

            // get all migrations for "Redirects" already executed
            var migrations = migrationService.GetAll(REDIRECTS_TABLE_NAME);

            // get the latest migration for "Redirects" executed
            var latestMigration = migrations.OrderByDescending(x => x.Version).FirstOrDefault();

            if (latestMigration != null)
            {
                latestMigrationVersion = latestMigration.Version;
            }

            if (this._targetVersion == latestMigrationVersion)
            {
                return;
            }

            var migrationsRunner = new MigrationRunner(
                migrationService,
                ApplicationContext.Current.ProfilingLogger.Logger,
                latestMigrationVersion,
                this._targetVersion,
                REDIRECTS_TABLE_NAME);

            try
            {
                migrationsRunner.Execute(db);
            }
            catch (HttpException e) {}
            catch (Exception e)
            {
                LogHelper.Error <MigrationRunner>("Error running Redirects migration", e);
            }
        }