PHP code example of thathoff / kirby-migrations

1. Go to this page and download the library: Download thathoff/kirby-migrations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thathoff / kirby-migrations example snippets


use Kirby\Cms\App as Kirby;

Kirby::plugin('example/test', [
    'migrations' => true,
]);

use Kirby\Cms\App as Kirby;

Kirby::plugin('example/test', [
    'migrations' => __DIR__ . '/classes/migrations',
]);



namespace Thathoff\KirbyMigrations;

use Kirby\Database\Db;

class Migration20250524144726InitDb extends Migration
{
    public function up(): void
    {
        $result = Db::execute("CREATE TABLE `my_table` (
            id int NOT NULL,
            test varchar(255) NOT NULL,
            PRIMARY KEY (id)
        )");

        if (!$result) throw Db::lastError();
    }

    public function down(): void
    {
        $result = Db::execute("DROP TABLE `my_table`");

        if (!$result) throw Db::lastError();
    }
}



namespace Thathoff\KirbyMigrations;

use Kirby\Database\Db;

class Migration20250524150432AddNameToMyTable extends Migration
{
    public function up(): void
    {
        $result = Db::execute("ALTER TABLE `my_table` ADD COLUMN `name` varchar(255) NOT NULL");

        if (!$result) throw Db::lastError();
    }

    public function down(): void
    {
        $result = Db::execute("ALTER TABLE `my_table` DROP COLUMN `name`");

        if (!$result) throw Db::lastError();
    }
}



namespace Thathoff\KirbyMigrations;

class Migration20250524150757CreatePage extends Migration
{
    public function up(): void
    {
        $content = [
            'title' => 'An interesting title',
            'text'  => 'Some great content here…'
        ];

        // make sure we are allowed to create the page
        $this->kirby->impersonate('kirby');

        $this->kirby->site()->createChild([
            'content'  => $content,
            'slug'     => 'created-by-migration',
            'template' => 'default',
            'isDraft'  => false,
        ]);
    }

    public function down(): void
    {
        // make sure we are allowed to delete the page
        $this->kirby->impersonate('kirby');

        $this->kirby->site()->find('created-by-migration')?->delete();
    }
}