I'm looking into migrating a couple of
# package-development
m
I'm looking into migrating a couple of Property Editors in my package to v14. In v13 I had two different property editors with slightly different data, similar to this:
Copy code
Person Picker
[{
 GroupName : string,
 PersonId : string
}]
Copy code
Company Picker
[{
 GroupName : string,
 CompanyId : string
}]
I'm thinking that maybe I should merge them into one schema in v14, something like this:
Copy code
[{
 GroupName : string,
 EntityId : string
}]
I guess I would need to create something that migrates the data and transform the stored JSON. Is there anyone who have made similar migrations when moving to v14 or that have seen any reading/examples around this?
j
uSync Migrate allows you to create your own custom migrators to transform data when you import it: https://github.com/Jumoo/uSyncMigrations?tab=readme-ov-file#complex-data-
m
@Jemayn Thanks, but not sure that I want my package to depend on uSync
s
You can do it in migration code, so when the site runs the first time after adding the version with the migration, it can finde all data types using your editors, and update accordingly. I had some trouble finding the right article in the docs, but heres an example of running migrations https://docs.umbraco.com/umbraco-cms/v/13.latest-lts/extending/database#using-a-notification-handler
j
w
Yep agree with Jesper & Soren that you could use a package migration to reshape the data as you need.
m
Also docs seem to suggest the notification handler route is the newer alternative.. https://docs.umbraco.com/umbraco-cms/extending/database#using-a-notification-handler using the MigrationPlanExector?
Copy code
If building a new solution, you can adopt a new pattern. With this pattern you create and run a similar migration but trigger it in response to a notification handler.
And a third option.. is just inherit from
MigrationBase
with a
MigratonPlan
if you don't need the
PacakgeMigrationBase
overhead of schema imports? load of example in the core here.. https://github.com/umbraco/Umbraco-CMS/tree/contrib/src/Umbraco.Infrastructure/Migrations/Upgrade might be a json content update one?
m
Thanks! I'm using migrations for stuff like db-tables etc. so it make sense to put the code in a migratipon. I was looking to see if there is anything specific around moving data from one schema to another since v14 kind of merged many different property editors into using the same schema i figured that there might be inspiration out there on how to approach this.
m
Thanks @Mike Chambers ! 😄 I'll have a closer look at that file, seems like the "source of truth" for the thing I'm looking for.
m
Though the one thing I keep coming up against.. is that do these migrations, and then have to use the
INotificationHandler<MigrationPlansExecutedNotification>
to resave (dataype/doctype) as
migrationplans
will supress notifications.. Otherwise if you have uSync/umb deploy for instance to import on startup.. it will immediately overwrite/revert your changes as it didn't know the migration changed anything 😦
m
Ohh, thanks @Mike Chambers did not think about the uSync/Umbraco Deploy use case here. That's a great point for package devs, easy to handle in a individual project but probably more tricky for a package that migrates data. How are you making sure that the notification only saves the stuff "it needs" to?
m
Copy code
csharp
 foreach (ExecutedMigrationPlan executedMigrationPlan in executedMigrationPlans)
        {
            foreach (MigrationPlan.Transition transition in executedMigrationPlan.CompletedTransitions)
            {
                if (transition.MigrationType == typeof(ImportPackageXmlMigration))
                {
                    return true;
                }
            }
        }
lifted from https://github.com/umbraco/The-Starter-Kit/blob/v14/dev/src/Umbraco.SampleSite/Migrations/PostMigrationNotificationHandler.cs after @Ronald Barendse pointed me in the right direction so can't take any credit ;-).. thread here https://discord.com/channels/869656431308189746/1245696446301208616/1246046623507812413
w
@Mike Chambers so why is the foreach loop above needed? Thought migrations deals with this so not sure what this code is trying to do/solve
m
migrations supress notifications by design, so if you alter a doctype, and have usync import at startup for settings.. usync doesn't get it's notification from the migration that the doctype has been changed, so usync config file is pre migration and then runs after the migration, reverting the migration change back to previous.
that for loop isn;t in the migration plan.. it;s in the agregated after all migrations have completed notification handler. (that allows usync to get it's doctype changed via resaving the doctype on the contenttype service)
w
Ah OK interesting to know
Perhaps something could or should be considered in core so us package dev's don't need to think about the nuances of this?!
m
It's by design though so that multiple synchronous package installs don't implode each other.. 😉 but is nuanced...
5 Views