Appearance
<div style="display: none;" hidden="true" aria-hidden="true">Are you an LLM? You can read better optimized documentation at /guides/Migrations.md for this page in Markdown format</div>
Migrations
Changelog
v1.0.3 - initial for adveshop 4
Migration package is ecommmercen/v4-migrations
It is a wrapper for Phinx with dependencies applicable for adveshop4.
Phinx migrations support raw sql which we use for convenience (no need to learn another query builder - and more compatible with current flow).
You can read more info in it's documentation
All phinx commands are registered in package and available to client. configuration is at /phinx.php
Usage
To execute migrations run php migrator.php migrate
To create a new migration run php migrator.php create MigrationTask. A new file will be created in database/migrations with a class with up and down methods.
Example of up function
public function up(): void {
$this->execute('ALTER TABLE `table` ADD COLUMN `new_column` VARCHAR(255) NOT NULL');
}If the migration is to be used as a patch the created migration class should extend \Patches\Patcher and have protected property protected string $class = \Patches\PatchClass::class
To create a new seeder run php migrator.php seed:create SeedNewColumn. A new file will be created in database/migrations with a class with run method.
Example from InitialSeeder code (show usage of read / write)
$currencies = $this->table('currencies');
$currencies->insert($this->dbDefaultValues['default_currency'])->saveData();
$result = $this->query('SELECT id FROM `currencies` WHERE `code` = "' . $this->dbDefaultValues['default_currency']['code'] . '"');
$data = $result->fetch(\PDO::FETCH_OBJ);
//single row save
$this->table('registry')
->insert(['reggroup' => 'ESHOP', 'regkey' => 'DEFAULT_CURRENCY', 'regval' => $data->id, 'lang' => ''])
->saveData();
//batch row save
$this->table('registry')
->insert([
['reggroup' => 'ESHOP', 'regkey' => 'TRANS_COST_LIMIT', 'regval' => $this->dbDefaultValues['trans_cost_limit'], 'lang' => ''],
['reggroup' => 'ESHOP', 'regkey' => 'MIN_ORDER_AMOUNT', 'regval' => $this->dbDefaultValues['min_order_amount'], 'lang' => ''],
])
->saveData();To run the seeders run php migrator.php seed:run
Version collisions (client forks)
A migration's version is the leading digits of its filename (20260923120000 in 20260923120000_add_expires_at_to_shop_cart.php). Versions must be unique across database/migrations/, for two reasons:
- Phinx keeps one log table (
APP_DB_MIGRATION_TABLE) and identifies each applied migration by its version. - When two files share a version, Phinx aborts
migrateandstatuswithDuplicate migration - "<file>" has the same version as "<version>". Nothing runs, so the deploy stepinit_migrate.phpexits non-zero and the database is never provisioned.
Upstream and each client fork create migrations independently, often with round-hour timestamps, so an upstream merge into a fork can bring in a file whose version the fork already uses.
How to resolve a collision
- Upstream keeps its version. Upstream's file is already released to every other fork and database.
- The client fork renumbers its own migration to the next free version that keeps its order among the fork's migrations. Rename the file, and rename any
data/file named after it in the same change (this only applies where such a file exists; the fork's migration refers to it by name). - If the fork's old version already ran on a database, update that database's log row before its next migrate:
sql
UPDATE <APP_DB_MIGRATION_TABLE> SET version = <new> WHERE version = <old> AND migration_name = '<Class>';The log row matters because Phinx decides what has run by version alone. Without the update, Phinx sees upstream's version as already applied and silently skips upstream's migration, then runs the renumbered fork migration again under its new version.
Example
Upstream added 20260923120000_add_expires_at_to_shop_cart.php (#789). The v4-fastpass-admin fork already had 20260923120000_create_programme_tables.php. After merging upstream, the fork:
- keeps upstream's
20260923120000_add_expires_at_to_shop_cart.phpas is; - renames its own file to the next free version after
20260923120000that is still before its next migration, for example20260923120001_create_programme_tables.php(and anydata/20260923120000_*file with it); - on every database where the fork's migration already ran, runs
UPDATE <APP_DB_MIGRATION_TABLE> SET version = 20260923120001 WHERE version = 20260923120000 AND migration_name = 'CreateProgrammeTables';before the nextphp migrator.php migrate.
Catching it early
bash .docker/scripts/ci/check-migration-versions.sh lists every version shared by more than one migration file and exits non-zero. Run it locally after an upstream merge. CI runs the same script on every pull request (Bitbucket "Migration Version Gate", GitHub migration-version-gate). It only checks version collisions, not two migrations that share a class name under different versions.
Clients
Existing clients
Set in .env value for APP_DB_MIGRATION_TABLE Change in database/migrations/20240920143439_initial_schema.php and database/seeders/InitialSeed.php the return value from true to false in shouldExecute method Execute php migrator.php migrate to initialize migration table
New Clients
Set in .env value for APP_DB_MIGRATION_TABLE
if you do not want to open the Wizard with the settings open the migration method SetInitialWizardCompleted::shouldExecute to return true
If client needs new bridge tables migrations change in database/migrations/20240923141428_new_bridge_tables.php the return value from false to true in shouldExecute method.
Execute php migrator.php migrate to initialize migration table and run migrations.
Execute php migrator.php seed:run to initialize migration table and run migrations (This command will execute all seeders, for new clients that does not require fake data in their database,
you should run php migrator.php seed:run --seed=InitialSeed to run only initial seed)