Skip to content

Improve the migration and seeder flow #40

Closed
Closed

Description

@gfazioli

@balazsnasz

For now, migrations/seeders are not tracked properly if they have ran or not.
Especially, if we call new migrations/seeders in the upgrader_post_install, we must know somehow which ones have already ran and which isn't.
For WordPress, traditional migrations doesn't make sense in my opinion.

Here is my concern: if you create new migrations and/or seeder, you will most likely add columns or new data for an existing table. Now, the runOnce selects a count() for checking if it has already ran or not. If you add new columns or new lines to an existing table, the new migrations/seeders won't run due to this.

I have two possible solutions in my mind.
Attach the plugin's current version to each migration/seeder files. This way you know which files are new (in the upgrader_post_install hook) and you can run them. For example, create-websites-table-1.4.7.php.
Store every migrations/seeders' state in a new {plugin-name}-migrations table. This is closer what Laravel does.
The first solution is easier to implement. However the second one is more flexible, but for WordPress it also seems kinda overkill.

Activity

  1. moved this to In Review in Roadmapon Dec 10, 2024
  2. added theissue type on Feb 11, 2026
  3. balazsnasz commented on Sep 30, 2026

    @balazsnasz
    Collaborator

    @gfazioli What do you think about this issue? I would lean towards implementing the second recommended solution, as it seems to be the most flexible approach for handling migrations and seeders.

    Store every migrations/seeders' state in a new {plugin-name}-migrations table. This is closer what Laravel does.

  4. gfazioli commented on Oct 1, 2026

    @gfazioli
    CollaboratorAuthor

    @gfazioli What do you think about this issue? I would lean towards implementing the second recommended solution, as it seems to be the most flexible approach for handling migrations and seeders.

    Store every migrations/seeders' state in a new {plugin-name}-migrations table. This is closer what Laravel does.

    @balazsnasz yep, let's do it, and your count() point is exactly where today's flow breaks: a runOnce seeder added in a later release never runs once the table has rows. I'd keep the core of your option 2 (every migration tracked by name and run once) with two changes: the state goes in a WP option instead of a table (we need one anyway for the version check below, and no extra table on every site), and seeders become plain migrations so runOnce goes away. Laravel doesn't track seeders either

    tbh the bigger surprise was the trigger. upgrader_post_install runs inside the update request, so the new files get included with the old framework classes still loaded, it does nothing for us when someone updates by uploading a new zip, and FTP/git/Composer deploys never reach it at all. So I'd replace it with a version check on init behind a lock, the way WooCommerce does it. All of this is breaking, so I'm thinking of a 3.0 (likely together with React 19) with a bones command to convert existing plugins, like migrate:to-v2 did. wdyt? especially on option vs table and seeders becoming migrations

  5. balazsnasz commented on Oct 3, 2026

    @balazsnasz
    Collaborator

    @gfazioli I agree with the seeders becoming migrations. If they're meant to be one-time, versioned data changes, having a single migration mechanism makes the lifecycle much simpler and avoids the need for a separate runOnce concept.

    I'm also fine with using a WP option for the migration state, especially since the version check needs one anyway.

    One thing I'm wondering about: if this is handled on init with a version check, how would the system know which version a given migration belongs to? Would the version be part of the migration name/file name, or would there be some other mapping between migrations and plugin versions?

  6. gfazioli commented on Oct 5, 2026

    @gfazioli
    CollaboratorAuthor

    @balazsnasz glad we're on the same page on seeders and the option 🙌 as for your question, there's no mapping: migrations don't belong to a version at all. The version check is only the cheap trigger. When the stored version differs from the plugin header, the runner lists the migration files, skips every name already saved in the option, runs the rest in file name order and then stores the new version

    imo that's what makes it nicer than a version in the file name: a fix backported to an older branch, or a migration added during development before any bump, still runs exactly once wherever it's missing. We can keep the plugin version next to each entry in the option, but only to show it in something like php bones migrate:status, never to decide what runs. The one catch is local dev: without a version bump the trigger doesn't fire, so there you'd run the pending ones by hand or just reactivate the plugin

  7. balazsnasz commented on Oct 5, 2026

    @balazsnasz
    Collaborator

    @gfazioli I agree with everything here except running migrations in file name order.

    Some migrations depend on previous migrations being applied first. As more migrations are added over time, there’s no guarantee that filename order reflects the order in which those migrations were created or their dependency order.

    I think we need some explicit ordering/dependency mechanism for this, otherwise a migration that was added later could potentially run before one it depends on.
    What do you think?

  8. gfazioli commented on Oct 5, 2026

    @gfazioli
    CollaboratorAuthor

    @balazsnasz fair point on dependencies, but the file name already carries the order: php bones migrate:create prefixes every file with a Y_m_d_His timestamp, so name order is creation order, and Laravel sorts them the same way. A migration that depends on another one is normally written after it, so its timestamp comes later too

    ngl there's one real gap: two branches merged in a different order than their timestamps, so a site could get the newer file first. The ledger doesn't skip the older one when it shows up, it still runs it, but I'd rather make that case visible than add a dependency graph: if a pending migration sorts before one that already ran, bones warns while developing and the runner logs it. Explicit dependencies feel like a lot of machinery for what the timestamp already covers, but happy to revisit if you've hit a real case

  9. balazsnasz commented on Oct 5, 2026

    @balazsnasz
    Collaborator

    @gfazioli I checked Laravel's migration source code, and it also seems to order migrations by filename: https://github.com/illuminate/database/blob/master/Migrations/Migrator.php#L124

    So I think a warning would still be useful, as you mentioned.

    Otherwise, I think the specification for this task looks good.

  10. gfazioli commented on Oct 6, 2026

    @gfazioli
    CollaboratorAuthor

    Summing up what we agreed, so it's all in one place before I start:

    • One mechanism. Seeders become plain migrations and runOnce goes away
    • Once per site. Each migration is tracked by file name in a WP option, no extra table
    • Order. File name order, which is creation order thanks to the Y_m_d_His prefix from migrate:create (same as Laravel). A pending migration that sorts before one already applied still runs, with a warning in bones and a log entry
    • Trigger. A version check on init (stored version vs plugin header) behind a lock, replacing upgrader_post_install, and activation calls the same runner. The plugin version is saved next to each entry, only for migrate:status
    • Failures. The runner stops at the first migration that fails, doesn't record it and tells the admins
    • Dev. php bones migrate and php bones migrate:status, or just reactivate the plugin
    • Release. It's breaking, so it lands in 3.0 together with a php bones migrate:to-v3 command that converts existing seeders

    btw I'll work on a v3 branch like we did for v2, and link the PR here

  11. gfazioli commented on Oct 6, 2026

    @gfazioli
    CollaboratorAuthor

    Shipped in v3.0.0: each migration runs once per site, recorded in an option, from a version check on init; seed data is a migration; and php bones migrate:to-v3 converts a 2.x plugin's seeders. Docs: Migrations and the upgrade guide. Thanks @balazsnasz for working the design through here, the out-of-order warning included.

  12. balazsnasz commented on Oct 7, 2026

    @balazsnasz
    Collaborator

    @gfazioli Migrated to v3 and tested it thoroughly. Everything looks good. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions