Answers.Body: <blockquote>
  <p>can't run Craft CLI (php craft migrate/create) from the plugin folder (as opposed to the project folder)</p>
  </blockquote>
  <p>From the project folder run:</p>
  <pre><code>php craft migrate/create my_migration_name --plugin=my-plugin-handle
  </code></pre>
  <p><a href="https://craftcms.com/docs/4.x/extend/migrations.html#creating-migrations" rel="nofollow noreferrer">https://craftcms.com/docs/4.x/extend/migrations.html#creating-migrations</a></p>
  <blockquote>
  <p>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.</p>
  </blockquote>
  <p>I have managed to run it manually:</p>
  <pre><code>php craft migrate --plugin=my-plugin-handle
  </code></pre>
  <p>For Craft to run the new migration on update you need to increase the <code>schemaVersion</code> of the plugin under the <code>Plugin</code> Class (Defaults to 1.0.0). e.g:</p>
  <pre><code>public string $schemaVersion = '1.0.1';
  </code></pre>
  <p>However, it seems that migration needs to be added to <code>Install</code> 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 (<a href="https://github.com/craftcms/commerce/tree/develop/src/migrations" rel="nofollow noreferrer">https://github.com/craftcms/commerce/tree/develop/src/migrations</a>).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-03T10:34:29.247Z
Answers.LastActivityDate: 2022-07-03T11:23:22.650Z
Answers.LastEditDate: 2022-07-03T11:23:22.650Z
Answers.UserDisplayName: Matanya
Body: <p>I'm trying to add a new table to an existing plugin.
  I can't add it to the <code>Install</code> migration as I want existing installations to pull the change.</p>
  <p>The problems I ran into:</p>
  <ol>
  <li>I can't run Craft CLI (<code>php craft migrate/create</code>) from the <strong>plugin</strong> folder (as opposed to the project folder)</li>
  <li>If I add a migration manually to the <code>migrations</code> folder of the <strong>plugin</strong> I can't get the root project to identify and run it.</li>
  </ol>
  <p>What is the best practice for this kind of change?</p>
  
Tags: <plugin-development><migration>
Title: How to add and run migration from plugin root folder?
ThreadText: # How to add and run migration from plugin root folder?
  
  (Asked by: username_0 on 2022-07-02)
  
  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?
  
  **Tags:** plugin-development, migration
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2022-07-03. Score: 2 | Accepted answer
  
  > 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>).
  
  
  ---
  
  