Search code examples
yii2database-migrationyii2-advanced-app

Yii2 migrations - Separated Migrations


I am creating a module with the following structure:

common
L modules
LL blog
LLL backend
LLL frontend
LLL common
LLL migrations

I found in yii2 documentation a section about "Separated Migrations"

In console/config/main.php I have set:

'migrate-blog' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationNamespaces' => ['app\common\modules\blog\migrations'],
            'migrationTable' => 'migration_blog',
            'migrationPath' => null,
        ]

Then I go to console and run following command:

php yii migrate/create app\\common\\modules\\blog\\migrations\\create_table_blog_post

It returns an error:

Error: Namespace 'app\common\modules\blog\migrations' not found in `migrationNamespaces`

am I missing any settings?


Solution

  • Did you add the following info to config of console.php

    'controllerMap' => [
    
        // Migrations for the specific project's module
        'migrate-module' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationNamespaces' => ['app\module\migrations'],
            'migrationTable' => 'migration_module',
            'migrationPath' => null,
        ],
    ],
    

    I have seen that you have the config in console/config/main.php then the check the yii file is having the following line.

    $config = require(__DIR__ . '/console/config/main.php');
    

    After this instead of running

    php yii migrate/create app\\common\\modules\\blog\\migrations\\create_table_blog_post
    

    Run the following command

    php yii/migrate-blog/create create_table_blog_post  
    

    I hope this helps.