Geta

Meny NO

No more manual deployment steps

Last Thursday, I attended EPiServer Techforum hosted in Oslo. In addition to new features in Commerce 9, Petter Sørby from EPiServer Norway showed us a useful but somewhat hidden feature called MigrationStep.

Disclamer

The IMigration interface is public but part of an internal namepace in Commerce.
It is part of the EPiServer.Commerce.Internal.Migration.dll which was first included in EPiServer.Commerce.Core.8.11.1.


How often haven't you made a list of todos/tasks for a deployment? Maybe you have added functionality that require a property set on the start page. Manual steps like these are especially painful when having several environments like development, test, staging and production

If you have upgraded a commerce solution you may have seen the "EPiServer Commerce Migration" screen. This is where EPiServer will execute migration steps in context of the website.
And this is where you can add your own steps, using the IMigrationStep interface.

A simple demo migration step may look like this:

[ServiceConfiguration(typeof(IMigrationStep))]
    public class AddSystemSettingToStartPage : 
                 EPiServer.Commerce.Internal.Migration.Steps.IMigrationStep
    {
        public bool Execute(IProgressMessenger processMessenger)
        {
            //This is where you add you step
            processMessenger.AddProgressMessageText("Starting...", false, 10);

            var contentRepo = ServiceLocator.Current.GetInstance();
            var startPage = contentRepo.Get(ContentReference.StartPage);
            var writableClone = startPage.CreateWritableClone();

            writableClone.EnableNewFancyFeature = true;
            writableClone.IntegrationUrl = "http://someapi";

            contentRepo.Save(writableClone, SaveAction.Publish, AccessLevel.NoAccess);

            processMessenger.AddProgressMessageText("Settings saved, all good", false, 100);

            return true;
        }

        public string Name
        {
            get { return "Add required system setting"; }
        }

        public string Description
        {
            get { return "Add required system setting"; }
        }

        public int Order { get { return 100; } }
    }

If the site starts with any pending migration steps you will see the following screen. To execute the step, you should press the button which is a link (see red circle).
And yes, this interface was most likely designed by a developer :)

 

The code will only run once per database - and the site will not load until all steps have status "Done". Need to run it again? Just change the namespace.

I hope to see it as a public class not only for Commerce, but also available for EPiServer CMS.

Special credit goes to Petter Sørby for finding the "hidden treasures".

A more advanced example can be found in the QuickSilver Commerce starterkit on Github - look for class ImportSiteContent.cs.

kommentarer drevet av Disqus