Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add support for running migration in batches #7755

Open
wants to merge 7 commits into
base: epic/campaigns
Choose a base branch
from

Conversation

alaca
Copy link
Member

@alaca alaca commented Feb 21, 2025

Description

This PR adds support to run migration in batches using Action Scheduler.

Affects

Visuals

Testing Instructions

Pre-review Checklist

  • Acceptance criteria satisfied and marked in related issue
  • Relevant @unreleased tags included in DocBlocks
  • Includes unit tests
  • Reviewed by the designer (if follows a design)
  • Self Review of code and UX completed

Copy link
Contributor

@JasonTheAdams JasonTheAdams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the start, @alaca! A couple thoughts:

  1. I really want to use interfaces, but we're going to run into issues because we really should be using inheritance to for sync and async migrations, but interfaces can't enforce static methods until PHP 7.4.
  2. So for now, let's:
    • make a BaseMigration abstract class with the static id() and timestamp() methods
    • Change BatchMigration to an abstract class that extends this
    • Extend the Migration class with this
    • Use BaseMigration in the migrations runner and registrar
  3. We still want batch migrations to hold up the queue, as all migrations need to run in order, regardless of what kind of migration they are

Love seeing this! So cool!

Comment on lines -21 to +27
abstract public function run();
public function run()
{
if ( ! is_subclass_of($this, BatchMigration::class)) {
throw new RuntimeException('run method is not defined.');
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid this if we can. This sort of thing can't be caught by static analysis, so the only way to know if there's an issue is at runtime.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we switch everything to inheritance we shouldn't need to do this.

Comment on lines +45 to +62
/**
* @unreleased
*/
public function enqueueAction(
int $timestamp,
string $hook,
array $args,
string $group,
bool $unique = false,
int $priority = 10
): int {
$enqueuedAction = $this->getActionByHookArgsGroup($hook, $args, $group, 'ids');
if (empty($enqueuedAction)) {
return as_schedule_single_action($timestamp, $hook, $args, $group, $unique, $priority);
}

return $enqueuedAction[0];
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide a description for this. It looks like it conditionally enqueues a single action so long as that action isn't already queued? We may want to call this safelyEnqueueAction or something like that.

Comment on lines -33 to +34
Hooks::addAction('give_upgrades', MigrationsRunner::class, 'run', 0);
//Hooks::addAction('give_upgrades', MigrationsRunner::class, 'run', 0);
// running batch actions via cron doesn't trigger give_upgrades and all registered actions fail
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this break the existing migrations that are using the old upgrade system?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants