Skip to content

[FR] Option to automatically trigger migrations from a module #6172

@BenParizek

Description

@BenParizek

I have a group of plugins that use shared modules and those modules define schema in one or more of the plugins where they are used. This presents a problem in that I do not know which plugin may be updated first and need to trigger the migrations in all of the plugins. This is necessary right now because migration folders are registered automatically by Craft and expect migration folders to have a one-to-one relationship with plugins.

Currently, for modules that manage shared schema, this requires at least 3 migrations, where the migrations in any plugins can just import and call safeUp on the shared migration in the module but leads to several extra steps and room for errors when managing releases.

migrations/m000000_000000_base_module_migration.php
migrations/m000000_000000_base_module_migration_plugin1.php
migrations/m000000_000000_base_module_migration_plugin2.php

The acute problem in my situation is that modules that define shared schema for us in multiple plugins have no way of triggering migrations without multiple steps or duplicate code, and more generally, modules have no way of automatically communicating to Craft to trigger schema updates like plugins do using the schemaVersion attribute.

Register Migration Roots Event

One potential approach to solving this could be something similar to EVENT_REGISTER_CP_TEMPLATE_ROOTS Event, where Craft provides an API where modules could register new migration folders to be checked when it is resolving migrations:

Event::on(View::class, View::EVENT_REGISTER_MIGRATION_ROOTS, function(RegisterMigrationRootsEvent $e) {
    $e->roots['sprout-base'] = $this->getBasePath().DIRECTORY_SEPARATOR.'migrations';
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementimprovements to existing featuresextensibility 🔌features related to plugin/module dev

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions