GeniXCMS

Migration Class

categoryAPI edit_calendar31 Mar 2026

Migration Builder Class


The Migration class is the foundation of GeniXCMS's database versioning system. It allows developers to define schema changes (tables, columns, indexes) in PHP code, ensuring consistent database states across all development and production environments.

⚙️ How it Works

Each migration is a standalone PHP file containing a class that implements two critical directions:

  • up(): Defines the changes to be applied (e.g., CREATE TABLE).
  • down(): Defines how to reverse those changes (e.g., DROP TABLE).

🏗️ Creating a Migration

Migrations are stored in inc/migrations/ and must follow a strict naming and class structure to be discovered by the engine.

📂 File Naming Convention

The filename must follow the pattern: YYYYMMDD-batchid-Description.php

  • Example: 20240327-01-CreatePermissionsTable.php

📂 Class Naming Convention

The class name must match the filename, replacing dashes with underscores and prefixing with Migration_.

  • Example: class Migration_20240327_01_CreatePermissionsTable extends Migration

🚀 Execution via CLI

The genix console provides built-in commands to manage your migration stack.

Command Description
php genix migrate Scans for pending files and executes their up() methods in chronological order.
php genix migrate:rollback Identifies the last execution batch and runs their down() methods to undo changes.

📝 Example Implementation

<?php

class Migration_20240327_10_AddProductTable extends Migration
{
    /**
     * Apply schema changes.
     */
    public function up()
    {
        $sql = "CREATE TABLE `products` (
            `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
            `name` varchar(255) NOT NULL,
            `price` decimal(10,2) NOT NULL
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

        Db::query($sql);
    }

    /**
     * Revert schema changes.
     */
    public function down()
    {
        Db::query("DROP TABLE IF EXISTS `products` ");
    }
}

⚡ Method Reference

  • Migration::init(): System internal. Ensures the migrations tracking table exists.
  • Migration::run(): Dispatches the migration execution logic.
  • Migration::rollback(): Dispatches the rollback logic.
priority_high
ImportantData Safety: Always double-check your down() method. A well-written rollback is just as important as the migration itself to prevent manual database cleanup during failed deployments.

See Also

  • CLI Console — Full list of genix commands.
  • Db Class — Understanding how to execute raw SQL within migrations.