'Run initial RBAC migrations as part of a regular app migration

I am building a product that is based on the Yii2 advanced template.

As part of this product and its future deployments, I am trying to automatically create the tables related to Authorization in a regular Yii2 migration.

E.g, when the end user installs the product and runs the regular Yii migration commands he should have a fully functional user management AND authorization active.

For authorization to work, the Yii2 RBAC documentation page states that 4 tables are needed (auth_*). The documentation states that they are created by running the following migration:

yii migrate --migrationPath=@yii/rbac/migrations

I'd like to offset this extra hassle from the end user by running this specific migration code for him inside a regular migration that will be stored in common/migrations.

Any easy solution for this?



Solution 1:[1]

Try to add in console/config/main.php:

'controllerMap' => [
        'migrate' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationPath' => [
                '@console/migrations',               
                '@yii/rbac/migrations',
            ]
        ]
    ],

Solution 2:[2]

Another approach (not using *.sh file) is to copy the rbac_init migration to your migrations folder:

cp vendor/yiisoft/yii2/rbac/migrations/m???????_rbac_init.php console/migrations/

Now, when you run php yii migrate it will be included the rbac_init migration.

Solution 3:[3]

I know this is pretty old question, but here is easy solution, create migration file and have this code in that file

<?php
require(Yii::getAlias('@yii/rbac/migrations/m140506_102106_rbac_init.php'));

/**
 * Class m220225_133725_init_rbac
 */
class m220225_133725_init_rbac extends m140506_102106_rbac_init
{

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gamover
Solution 2 sdlins
Solution 3