How to atomatically apply migrations that comes from Yii2 extensions

放肆的年华 提交于 2019-12-24 16:05:19

问题


I have installed an extension for Yii2 dektrium/yii2-user using composer using it's "require" section. This extension contains migrations for database. Is it possible to apply migrations from this extension using console syntax not like this:

php yii migrate --migrationPath=@dektrium/yii2-user/migrations

but run all migrations automatically by using a simple command like:

php yii migrate

Is it possible to tell composer where the concrete extension contains it's migrations?


回答1:


If you want to make this process automated, you can use scripts property of composer. For more information you can see https://getcomposer.org/doc/articles/scripts.md. In your case you can do your goal with something like this on composer.json:

{
 // Some codes are here
    "scripts": {
        "post-update-cmd": [
            "php yii migrate --migrationPath=@dektrium/yii2-user/migrations"
        ],
        "post-install-cmd": [
            "php yii migrate --migrationPath=@dektrium/yii2-user/migrations"
        ]
    },
 // Some codes are here
}

I prefer to save all commands that must be run after install -or update- on a file (for example file named commands) in the root of project, like this:

#!/usr/bin/env bash

./yii migrate/up --migrationPath=@vendor/dektrium/yii2-user/migrations
./yii migrate/up
./yii migrate/up --migrationPath=@app/modules/rules/migrations
./yii migrate/up --migrationPath=@app/modules/formsaz/migrations
./yii migrate/up --migrationPath=@app/modules/todo/migrations
./yii formsaz/rules/init
./yii husky/rules/init

and on composer.json file put its name:

{
 // Some codes are here
    "scripts": {
        "post-update-cmd": [
            "sh commands"
        ],
        "post-install-cmd": [
            "sh commands"
        ]
    },
 // Some codes are here
}

So each time after composer install or composer update, all commands will be run (and it's useful on teamwork).




回答2:


I found only one good solution - Install yii2 extension https://github.com/dmstr/yii2-migrate-command

Now i can easily use command "php yii migrate" and don't worry that someone from my team doesn't apply required migrations.

Thanks others for help! If u find more appropriate solutions, please share =)




回答3:


Yii2: Allow Migrate From Multiple Path



来源:https://stackoverflow.com/questions/35779980/how-to-atomatically-apply-migrations-that-comes-from-yii2-extensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!