Migrations

Migrations ensure that changes in the database between updates or during the installation are correctly applied.

The migration system from the core reads all files in the _migrations folders that exist in the modules folder in order. You need to specify the order with incremental prefixes.

ā”œā”€ā”€ _migrations/
ā”‚   ā”œā”€ā”€ 1_create_test_table.php
ā”‚   ā”œā”€ā”€ 2_update_table_with_new_fields.php

This way we can assure that changes can be based on other changes.

The migration file:

<?php

namespace Migrations;

use Controllers\Panel;
use Migration\MigrationInterface;

class create_test_table extends MigrationInterface {

    public function up() {
        $result = Panel::getDatabase()->custom_query(<<<SQL
            CREATE TABLE `test` (
                `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=COMPACT;
        SQL);
        return $result;
    }

    public function down() {
        $result = Panel::getDatabase()->custom_query(<<<SQL
            DROP TABLE `test`;
        SQL);
        return $result;
    }

    public function getName() {
        return "create_test_table";
    }
}

As you can see, we extend the MigrationsInterface, which will have the up and down methods. The class name and migration name are the same.

Helpers

In order to work with the Migrations, we have some helper functions we can call:

MigrationHandler::getInstance()->ensureMigrations(
    ['create_test_table', 'update_table_with_new_fields']
);

checkMigrationStatus / getAppliedMigrations

get a list of all the applied migrations.

getNewMigrations

get a list of migrations that have not been applied yet.

applyMigrations

apply all migrations that have not been applied yet.

loadMigrations

anotherway to get a list of migrations.

ensureMigrations

takes an array and return either true of false if all the migrations have already been applied or not.

support

Do you require help?

Wether you have encountered a Bug, ran into a problem setting something up or require generall assistance using some of the features, we want to help you with that.

On our Discord-Server you can ask for help of any kind, suggest new ideas for our products or just hangout and chat!

Open Discord