How to atomatically apply migrations that comes fr

2019-08-15 03:20发布

问题:

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