Package Migration Plans - Do they support async at all?

Hello,
I am working on a package migration which runs when the package is installed.

With a lot of the newer bits and pieces of Umbraco supporting Async methods in the services. I was wondering if PackageMigrations support async at all?

As writing .Result in my code is giving me the heebie jeebies

// Booo :( the Migrate from PackageMigrationBase does not support Async it seems ?!
var adminGroup = _userGroupService.GetAsync(Umbraco.Cms.Core.Constants.Security.AdminGroupKey).Result;

Squirm

Example Code

This is what I have and was hoping to not use .Result in the codebase.

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Infrastructure.Migrations;
using Umbraco.Cms.Infrastructure.Packaging;

namespace ContentLock.Migrations.v1
{
    public class AddUserPermissionToAdmins : PackageMigrationBase
    {
        private readonly IUserGroupService _userGroupService;
        private readonly ILogger<AddUserPermissionToAdmins> _logger;

        public AddUserPermissionToAdmins(IPackagingService packagingService,
            IMediaService mediaService,
            MediaFileManager mediaFileManager,
            MediaUrlGeneratorCollection mediaUrlGenerators,
            IShortStringHelper shortStringHelper,
            IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
            IMigrationContext context,
            IOptions<PackageMigrationSettings> packageMigrationsSettings,
            IUserGroupService userGroupService,
            IUserService userService,
            ILogger<AddUserPermissionToAdmins> logger)
            : base(
                  packagingService,
                  mediaService,
                  mediaFileManager,
                  mediaUrlGenerators,
                  shortStringHelper,
                  contentTypeBaseServiceProvider,
                  context,
                  packageMigrationsSettings)
        {
            _userGroupService = userGroupService;
            _logger = logger;
        }

        protected override void Migrate()
        {
            // Booo :( the Migrate from PackageMigrationBase does not support Async
            var adminGroup = _userGroupService.GetAsync(Umbraco.Cms.Core.Constants.Security.AdminGroupKey).Result;

            if (adminGroup == null)
            {
                _logger.LogWarning("ContentLock is unable to find the default Umbraco Admin User Group. Exiting");
                return;
            }

            // Permissions ?!
            var permissions = adminGroup.Permissions;

            // Add new permission (Same as the permission verb in clientside code)
            permissions.Add("ContentLock.Enabled");

            // Update the user group
            var attempt = _userGroupService.UpdateAsync(adminGroup, Umbraco.Cms.Core.Constants.Security.SuperUserKey).Result;

            _logger.LogTrace("Updated default Umbraco Admin User Group with the 'ContentLock.Enabled' permission with this attempt status {status}", attempt.Status);

            if (!attempt.Success)
            {
                _logger.LogWarning("ContentLock was unable to update the default Umbraco Admin User Group with permission 'ContentLock.Enabled'");
                _logger.LogError(attempt.Exception, "Error updating the User Group permission");
            }
        }
    }
}

I don’t think they are,

I was just looking at some ‘old-school’ migration stuff using MigrationPlans, and that isn’t async either , I think the whole migration pipeline is synchronous. :frowning_face:

Current mood :point_down:


OK for now I will pretend I didn’t see the .Result in that bit of the code and just move on.

Are you aware of any GitHub issues related to this at all ?

I’ve seen talks about “async all the things” at HQ but it’s a huge undertaking, definitely not there yet. GitHub Discussions is good for feature requests!

OK I clearly don’t ever remember creating this, but it seems I have already done so…

Will close this now & suggest anyone reading/following along go there instead & vote it up or chime in please :point_up:

2 Likes

Ah, this is the related work I was remembering:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.