Tutorials: Migrations

Setting Development Environment

Migrations execution will be subject of differences depending on development environment (eg: using db connection settings). It will thus require environment variable ENVIRONMENT to be already set:

If above is not set, framework automatically assumes {VALUE} will be local!

Setting Cache Solution

API requires a implementation for keeping track of migrations that ran already as well as their outcome. Framework Skeleton API comes with:

is used by default in :

define("CACHE_TYPE", "sql");

If you want to switch to the other, you only need to edit and change line to:

define("CACHE_TYPE", "nosql");

Writing Migration Scripts

To generate a migration script, open console and run:

php migrations.php generate

This will create a VersionTIMESTAMP class (eg: Version20210205105634) in migrations folder, implementing and coming with two methods:

Example implementation:

class Version20210205105634 implements \Hlis\Migration\Script { /** * Commits changes */ public function up(): void { SQL("ALTER TABLE users ADD age TINYINT UNSIGNED NOT NULL DEFAULT 0"); } /** * Rolls back changes */ public function down(): void { SQL("ALTER TABLE users DROP COLUMN age"); } }

Executing Migrations

allows you to execute migrations using multiple strategies through console arguments:

Depending on solution chosen, migration progress will be saved to a permanent storage medium that makes sure: