Seed data
Seed steps are numbered. Each runs once per database, in order, as the master admin. The database
stores the last step it ran (at cms/modules/{name}.json in its storage), so a step added in an
update runs alone at the next start. A new step is also the place to change existing data.
The events module's step 2 came in an update. It throws while the host's page is missing, so it runs again at the next start:
public static void ListUnder(CmsModuleContext c, Guid? parentPageId) {
if (parentPageId is not Guid parent) return;
if (!c.Store.TryGet<IPage>(parent, out _))
throw new InvalidOperationException("The page the events go under is not there yet.");
foreach (var e in c.Store.Query<IEventPage>().Execute())
if (!e.Parent.IsSet()) c.Store.SetRelation<IEventPage>(e.Id, x => x.Parent, parent);
}- A step that throws stops the steps after it. The error is logged, and the step runs again at the next start.
- Make every step safe to run twice. Give seeded nodes fixed ids and check for them first, as
VenuesModuledoes. Later steps and other modules can then find the nodes too. - Never renumber a released step. A database that has run step 2 never runs a step 2 again.
- Write synchronously (
Insert,Update,CreateAndInsert). Only the store's synchronous writes run its transaction plugins.
CmsModules.SeedVersionOf(name, store) returns the last step a database ran.