3

I'm trying to add a new table to an existing plugin. I can't add it to the Install migration as I want existing installations to pull the change.

The problems I ran into:

  1. I can't run Craft CLI (php craft migrate/create) from the plugin folder (as opposed to the project folder)
  2. If I add a migration manually to the migrations folder of the plugin I can't get the root project to identify and run it.

What is the best practice for this kind of change?

1 Answer 1

3

can't run Craft CLI (php craft migrate/create) from the plugin folder (as opposed to the project folder)

From the project folder run:

php craft migrate/create my_migration_name --plugin=my-plugin-handle 

https://craftcms.com/docs/4.x/extend/migrations.html#creating-migrations

If I add a migration manually to the migrations folder of the plugin I can't get the root project to identify and run it.

I have managed to run it manually:

php craft migrate --plugin=my-plugin-handle 

For Craft to run the new migration on update you need to increase the schemaVersion of the plugin under the Plugin Class (Defaults to 1.0.0). e.g:

public string $schemaVersion = '1.0.1'; 

However, it seems that migration needs to be added to Install migration as well for fresh installations, which would require code duplication or extracting the migration into a service class or trait. Though I can't see this pattern followed in Craft Commerce for example (https://github.com/craftcms/commerce/tree/develop/src/migrations).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.