Skip to content
This repository has been archived by the owner on Mar 19, 2022. It is now read-only.

MigrationHelper helps to up/down migrations from PHP script

Notifications You must be signed in to change notification settings

lembdev/yii2-migration-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

MigrationHelper

MigrationHelper helps to up/down migrations from PHP script

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist lembadm/yii2-migration-helper "*"

or add

"lembadm/yii2-migration-helper": "*"

to the require section of your composer.json file.

Usage

To use this extension, simply add the following code in your application configuration:

return [
    //....
    'components' => [
        'migration' => [
            'class' => 'lembadm\migration\MigrationHelper',
            'migrationTable' => '<migrationTable>',
            'idleTimeout' => '<idleTimeout>',
            'timeout' => '<timeout>',
        ],
    ],
];

Apply new migrations

Synchronously

try {
    $process = Yii::$app->migration->up();
    echo $process->getOutput();
} catch (ProcessFailedException $e) {
    echo $e->getMessage();
}

Async

$process = Yii::$app->migration->upAsync();
while ($process->isRunning()) {
    // waiting for process to finish
}

or

$process = Yii::$app->migration->upAsync();
// ... do other things
$process->wait(function ($type, $buffer) {
    echo (Process::ERR === $type)
        ? 'ERR > '.$buffer
        : 'OUT > '.$buffer;
});

Apply module migrations

All same but need to specify path to module: $migrationPath Path to migrations (--migrationPath argument for yii migrate/up command).

$process = Yii::$app->migration->up('<pathToModule>');

Downgrades the application by reverting old migrations.

Synchronously

try {
    $process = Yii::$app->migration->down();
    echo $process->getOutput();
} catch (ProcessFailedException $e) {
    echo $e->getMessage();
}

Async

$process = Yii::$app->migration->downAsync();
while ($process->isRunning()) {
    // waiting for process to finish
}

or

$process = Yii::$app->migration->downAsync();
// ... do other things
$process->wait(function ($type, $buffer) {
    echo (Process::ERR === $type)
        ? 'ERR > '.$buffer
        : 'OUT > '.$buffer;
});

Apply module migrations

All same but need to specify path to module: $migrationPath Path to migrations (--migrationPath argument for yii migrate/up command).

$process = Yii::$app->migration->downAsync('<pathToModule>');

About

MigrationHelper helps to up/down migrations from PHP script

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages