Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 Migrate Command Programmatically

I want to migrate up/down the database using the code instead of the console window. I have looked into the framework. I have tried the following code:

$runner = new yii\console\Application([
                                          'id'       => 'auto-migrate',
                                          'basePath' => dirname(__DIR__)
                                      ]);        

    $runner->runAction('migrate');
    ob_start();
    return htmlentities(ob_get_clean(), null, Yii::$app->charset);

It gives the Internal Server Error. And doesn't even migrates the files to the database. But if the directory doesn't exists, it creates the directory. It is behaving the way it should but if the migration file exist in that same directory it gives Internal Server Error.

like image 616
Abhimanyu Saharan Avatar asked May 07 '26 11:05

Abhimanyu Saharan


2 Answers

\Yii::$app->runAction('migrate', ['migrationPath' => '@yii/rbac/migrations/']);
like image 74
visionp Avatar answered May 09 '26 23:05

visionp


I would probably do that over php function exec, but if you really want to do it by creating new application instance then you should be able to do it like this.

Case 1 - not very good idea

//Define, if you want to capture output
defined('STDIN') or define('STDIN', fopen('php://input', 'r'));
defined('STDOUT') or define('STDOUT', fopen('php://output', 'w'));
//Create new console app, if you do not run it from root namespace you should use '\yii\console\Application'
$runner = new \yii\console\Application([
    'id'       => 'auto-migrate',
    'controllerNamespace' => 'console\controllers', //for migrate command it should not matter but for other cases you must specify otherwise applocation cannot find your controllers
    'basePath' => dirname(__DIR__ . '/../../console/config'), //This must point to your console config directory, when i run this in frontend sitecontroller i must add '/../../console/config',
    'components' => [
                'db' => [//If you want to call migrate you probably need some database component
                    'class' => 'yii\db\Connection',
                    'dsn' => 'mysql:host=localhost;dbname=mydb',
                    'username' => 'root',
                    'password' => '',
                    'charset' => 'utf8',
                ],
            ],
]);        
ob_start();
try
{
    $runner->runAction('migrate/up');
}
catch(\Exception $ex)
{
    echo $ex->getMessage();
}
return htmlentities(ob_get_clean(), null, Yii::$app->charset);

And result in my case:

Yii Migration Tool (based on Yii v2.0.1) No new migration found. Your system is up-to-date.

Case 2

    ob_start();
    try
    {
        $output = Array();
        exec(__DIR__ . '/../../yii migrate/up', $output);
        echo implode("\n", $output);
    }
    catch(\Exception $ex)
    {
        echo $ex->getMessage();
    }
    return htmlentities(ob_get_clean(), null, Yii::$app->charset);

And result in my case:

Yii Migration Tool (based on Yii v2.0.1) No new migration found. Your system is up-to-date.
like image 36
Aivar Avatar answered May 10 '26 01:05

Aivar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!