Skip to content

Migration Manager

hooman helmi edited this page Oct 19, 2025 · 5 revisions

The migration manager includes three types of migrations, each designed for a different purpose.

1. Background Process Migrations

Background Process Migrations are designed for large-scale data migrations that must be triggered manually by a user with the necessary permissions. These migrations typically operate on substantial data sets and are not executed automatically to avoid performance issues or unintended changes.

2. Schema Migrations

Schema Migrations handle structural changes to the database. They are executed during plugin update and ensure that the database schema remains compatible with the current plugin version. These migrations apply version-controlled changes such as adding or modifying tables, columns, or indexes.

3. Queue Migrations

Queue Migrations apply small, ordered database updates through a queue(FIFO) system. Each update step is identified by a unique job index and runs sequentially. The queue only runs when triggered by a user with the appropriate permission, ensuring that updates are controlled and predictable.

How to add a migration step

Step 1: Add a job index and method to the $migrationSteps array

protected $migrationSteps = [
    'v510_update_settings' => 'updateSettings510',
];

Step 2: Define the corresponding public method

public function updateSettings510() {
    update_option( 'sample_option', 'new-value' );
}

Full example

use WP_Statistics\Service\Database\Migrations\Queue\QueueMigration;

class QueueMigration extends BaseMigrationOperation {
    protected array $migrationSteps = [
        'v510_update_settings' => 'updateSettings510',
    ];

    public function updateSettings510() {
        update_option( 'sample_option', 'new-value' );
    }
}

Execution flow

  1. The queue starts from the first incomplete job step.
  2. Each step runs inside a try/catch block to handle errors gracefully.
  3. Completed steps are recorded in the wp_options table to prevent re-execution.
  4. Once all steps are complete, the queue remains idle unless new steps are added in future updates.

State tracking (wp_statistics_queue_background_process)

  • completed_steps – Array of completed job index keys.
  • completed – Boolean flag indicating whether all steps have finished.
  • On fresh installations, all steps are marked as complete to prevent unnecessary execution.

User approval requirement

Migration type Requires user trigger/approval?
Background Process Migrations Yes
Schema Migrations No
Queue Migrations Yes

Clone this wiki locally