PHP code example of mb4it / bitrix-migration

1. Go to this page and download the library: Download mb4it/bitrix-migration 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/ */

    

mb4it / bitrix-migration example snippets




declare(strict_types=1);

use MB\Bitrix\Database\Migrations\Migration;

return new class extends Migration {
    private const TABLE = 'my_orders';

    public function up(): void
    {
        $this->statement(
            'CREATE TABLE IF NOT EXISTS ' . self::TABLE . ' ('
            . 'ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, '
            . 'TITLE VARCHAR(255) NOT NULL'
            . ')'
        );
    }

    public function down(): void
    {
        $this->statement('DROP TABLE IF EXISTS ' . self::TABLE);
    }
};

use Bitrix\Main\Application as Bitrix;
use MB\Bitrix\Database\Migrations\Migrator;
use MB\Bitrix\Database\Migrations\PathRegistry;
use MB\Bitrix\Database\Migrations\Repository\DatabaseMigrationRepository;

// с mb4it/bitrix-support:
$migrator = app(Migrator::class);      // или app('migrator')

// standalone (без support) — прямая сборка:
$docRoot  = rtrim(Bitrix::getDocumentRoot(), '/\\');
$migrator = new Migrator(
    new DatabaseMigrationRepository(),
    new PathRegistry($docRoot . '/local/migrations'),
);

$applied  = $migrator->run();                       // применить pending (проект)
$reverted = $migrator->rollback(steps: 1);          // откатить последний батч
$status   = $migrator->status();                    // [{migration, ran, batch}, ...]

// для модуля — передать путь и scope:
$applied  = $migrator->run(['/abs/path/my.module/migrations'], 'my.module');

$this->app->singleton(PathRegistry::class, fn () => new PathRegistry(
    $docRoot . '/local/migrations',
    $docRoot . '/local/modules/my.module/migrations',
));