Answers.Body: <p>You can have plugin A listen for the <code>EVENT_AFTER_INSTALL_PLUGIN</code> event.</p>
  <p>It could then check if <code>$plugin-&gt;handle</code> is for plugin B and if so, run any additional install migrations or logic.</p>
  <p>You could do something similar for the <code>EVENT_AFTER_UNINSTALL_PLUGIN</code> event to clean things up in the case where one gets uninstalled.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-17T09:11:39.470Z
Answers.Text: Thanks Brad, I've got the event listening and handle check now, but not sure on how to run migrations independently? If I try use the `$plugin->getMigrator()->migrateUp("name_of_migration")` I get an error as that migration has already been run (or at least added to the migration table).
Answers.UserDisplayName: Timmy O'Mahony
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-17T09:40:05.890Z
Answers.Text: Ah, I'm able to just run the migration and its `safeUp` method just as a regular class method (see original question update). Is this a sensible approach to running a migration outside of the migrator? Thanks for your help
Answers.UserDisplayName: Timmy O'Mahony
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-17T21:17:39.893Z
Answers.Text: Yup, seems reasonable to me.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-16T21:42:25.540Z
Answers.LastActivityDate: 2020-11-16T21:42:25.540Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm creating a plugin (A) that has optional support for another 3rd party plugin (B). When my plugin A is installed, I create a number of tables. If plugin B is also installed my migrations create further tables specifically for plugin B.</p>
  <p>The problem is if someone installs plugin A, then only installs plugin B afterward, my custom tables are never created.</p>
  <p>How can I create tables/run migrations only when a plugin (B) is installed?</p>
  <p><strong>EDIT</strong></p>
  <p>I've been able to get this working with the following:</p>
  <pre><code>Event::on(
    Plugins::class,
    Plugins::EVENT_AFTER_INSTALL_PLUGIN,
    function (Event $event) {
      if ($event-&gt;plugin-&gt;handle == &quot;pluginB&quot;) {
        $migration = new migrations\m201031_120401_add_pluginB_support();
        $migration-&gt;safeUp();
      }
    });
  </code></pre>
  
Tags: <plugin-development><migration>
Title: Running plugin migrations when other plugin is installed?
ThreadText: # Running plugin migrations when other plugin is installed?
  
  (Asked by: username_0 on 2020-11-16)
  
  I'm creating a plugin (A) that has optional support for another 3rd party plugin (B). When my plugin A is installed, I create a number of tables. If plugin B is also installed my migrations create further tables specifically for plugin B.
  
  The problem is if someone installs plugin A, then only installs plugin B afterward, my custom tables are never created.
  
  How can I create tables/run migrations only when a plugin (B) is installed?
  
  **EDIT**
  
  I've been able to get this working with the following:
  
  ```
  Event::on(
    Plugins::class,
    Plugins::EVENT_AFTER_INSTALL_PLUGIN,
    function (Event $event) {
      if ($event->plugin->handle == "pluginB") {
        $migration = new migrations\m201031_120401_add_pluginB_support();
        $migration->safeUp();
      }
    });
  
  ```
  
  **Tags:** plugin-development, migration
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-11-16. Score: 1 | Accepted answer
  
  You can have plugin A listen for the `EVENT_AFTER_INSTALL_PLUGIN` event.
  
  It could then check if `$plugin->handle` is for plugin B and if so, run any additional install migrations or logic.
  
  You could do something similar for the `EVENT_AFTER_UNINSTALL_PLUGIN` event to clean things up in the case where one gets uninstalled.
  
  > **Comments:**
  > **username_0**: Thanks username_1, I've got the event listening and handle check now, but not sure on how to run migrations independently? If I try use the `$plugin->getMigrator()->migrateUp("name_of_migration")` I get an error as that migration has already been run (or at least added to the migration table).  
  > **username_0**: Ah, I'm able to just run the migration and its `safeUp` method just as a regular class method (see original question update). Is this a sensible approach to running a migration outside of the migrator? Thanks for your help  
  > **username_1**: Yup, seems reasonable to me.  
  
  ---
  
  