Database Migrations¶
Occasionally certain features or bug fixes require changes to the database — for instance creating new or altering existing tables.
All migrations are managed at the project level. This means all migration scripts are created in the core project plugin under /customizations/plugin, even if the change is required by a custom sub-module or dependency.
Used in: Barberklingen.dk, Barberklingen.se, Barberklingen.nl, Kaffedrengen.dk
Structure¶
customizations/
└── plugin/
└── src/
└── Versions/
├── Migrations/
│ ├── Migration.php # Base Trait
│ ├── Migration_1_0_0.php
│ └── Migration_1_1_0.php
└── Migrations.php # Provider
How does it work?¶
Migrations are checked and executed on CLI requests only. Run them with:
Replace {{project}} with barberklingen or kaffedrengen depending on the project.
The command triggers Migrations::install_available_updates(), which iterates over all scripts in the Migrations folder. For each file found, it compares the current DB version stored in the database against the migration script version. If the script version is higher, it executes — otherwise it is skipped.
The script version is derived from the filename: Migration_1_0_0.php is parsed as version 1.0.0. Migration scripts must have unique version names and must always increment.
The current DB version is stored in the wp_options table under the {{project}}_db_version key (e.g. barberklingen_db_version).
Creating a migration script¶
-
Check the
Migrationsfolder for the latest version. If the latest isMigration_1_1_0.php, your new script must be at least1.1.1(or1.2.0, etc.). -
Create the new migration class:
# Location: /customizations/plugin/src/Versions/Migrations/Migration_1_2_0.php <?php class Migration_1_2_0 { // Use the Migration trait to inherit abstract methods and logic use Migration; // Contains the actual business logic for this migration public function install() { global $wpdb; $wpdb->query("CREATE TABLE IF NOT EXISTS ..."); // ... } } // Returning a new instance is required. // Returning the class only will not work and the migration runner will fail. return new Migration_1_2_0(); -
Register the new version in
/customizations/plugin/src/Versions/Migrations.phpby adding it to the array inget_migrations(): -
Run the migration script locally to verify it works. When deploying, Capistrano will run the migration automatically.
Cross-brand migrations
If a migration needs to run across all projects/brands, the script must be implemented separately in each project.