PHP code example of rkr / php-db-migrations

1. Go to this page and download the library: Download rkr/php-db-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/ */

    

rkr / php-db-migrations example snippets




use Kir\DB\Migrations\DBAdapter;
use Kir\DB\Migrations\DBAdapters\PdoDBAdapter;

return [
    'statements' => [[
        'up' => static fn (DBAdapter $db) =>
            $db->exec("ALTER TABLE `my_table` ADD COLUMN `id` INT NOT NULL"),
        'down' => static fn (DBAdapter $db) =>
            $db->exec("ALTER TABLE `my_table` DROP COLUMN `id`")
    ]],
];



use Kir\DB\Migrations\DBAdapters\PdoDBAdapter;
use Kir\DB\Migrations\MigrationManager;
use PDO;
use Psr\Log\NullLogger;

$pdoInstance = new PDO('mysql:host=localhost;dbname=app', 'user', 'pass');
$dbAdapter = new PdoDBAdapter($pdoInstance, 'migrations', new NullLogger());
$manager = new MigrationManager($dbAdapter, __DIR__ . '/migrations', new NullLogger());
$manager->migrate();



use Doctrine\DBAL\DriverManager;
use Kir\DB\Migrations\DBAdapters\DoctrineDbalAdapter;
use Kir\DB\Migrations\MigrationManager;
use Psr\Log\NullLogger;

$connection = DriverManager::getConnection([
    'url' => 'pgsql://user:pass@localhost:5432/dbname',
]);
$adapter = new DoctrineDbalAdapter($connection);
$manager = new MigrationManager($adapter, __DIR__ . '/migrations', new NullLogger());
$manager->migrate();



use Kir\DB\Migrations\Commands\Add;
use Kir\DB\Migrations\Commands\Change;
use Kir\DB\Migrations\Commands\Drop;

return [
    'statements' => [
        Add::table('test')
            ->intColumn(name: 'a', isUnsigned: true, nullable: false, comment: 'Primary id')
            ->varcharColumn(name: 'b', length: 255, comment: 'Label')
            ->primaryKey('a')
            ->autoIncrement('a')
            ->engine('InnoDB'),

        Add::toTable('test')
            ->intColumn(name: 'c', after: 'b', nullable: true, comment: 'Extra column'),

        Drop::fromTable('test')
            ->column('c'),

        Change::table('test')
            ->column(name: 'b', comment: 'New comment'),
    ]
];



use Kir\DB\Migrations\Commands\Add;

return [
    'statements' => [
        Add::table('authors')
            ->intColumn(name: 'id', isUnsigned: true, nullable: false, comment: 'Primary id')
            ->varcharColumn(name: 'name', length: 255, comment: 'Display name')
            ->primaryKey('id')
            ->autoIncrement('id')
            ->engine('InnoDB'),

        Add::table('books')
            ->intColumn(name: 'id', isUnsigned: true, nullable: false, comment: 'Primary id')
            ->intColumn(name: 'author_id', isUnsigned: true, nullable: false, comment: 'Author reference')
            ->varcharColumn(name: 'title', length: 255, comment: 'Book title')
            ->primaryKey('id')
            ->autoIncrement('id')
            ->engine('InnoDB'),

        Add::toTable('books')
            ->index('idx_books_author', 'author_id')
            ->foreignKey(
                name: 'fk_books_author',
                localColumns: ['author_id'],
                foreignTable: 'authors',
                foreignColumns: ['id'],
                onDelete: 'CASCADE',
                onUpdate: 'CASCADE'
            ),
    ]
];