Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 496
Dzhuneyt Avatar asked Mar 10 '15 20:03

Dzhuneyt


People also ask

Why run database migrations as part of an application deployment?

In my previous post I discussed the need to run database migrations as part of an application deployment, so that the database migrations are applied before the new application code starts running. This allows zero-downtime deployments, and ensures that the new application code doesn't have to work against old versions of the database.

Can I perform rolling updates of my application with database migrations?

With the proposed solution (and careful database schema design!) you can perform rolling updates of your application that includes database migrations. The problem is very common—the new version of your app uses a slightly different database schema to the previous version.

Why doesn't the database creation code run after the migration?

If you created the initial migration when the database already exists, the database creation code is generated but it doesn't have to run because the database already matches the data model.

How do I re-run an app migration?

After you have run a migration plan in your assistant (like selecting a cloud instance and entities to migrate), re-run the app migration using the following URL: rest/migration/latest/app-migration/rerun/<planId> . This operation can be repeated multiple times as required.


2 Answers

I have created a migrate.sh file where I put my migration commands that I need to run. This allows me to migrate from multiple places in the same time. It is quite simple, take a look here: https://github.com/Mihai-P/yii2-app-advanced/blob/master/migrate.sh

Instead of running ./yii migrate/up i just run sh migrate.sh that will update everything from any place.

The actual point of this is: you do not have to stick to exactly what Yii gave you. That is just a template for you to build on. Fork it, modify it, make it your own.

like image 53
Mihai P. Avatar answered Oct 13 '22 12:10

Mihai P.


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

'controllerMap' => [
        'migrate' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationPath' => [
                '@console/migrations',               
                '@yii/rbac/migrations',
            ]
        ]
    ],
like image 3
Tahiaji Avatar answered Oct 13 '22 12:10

Tahiaji