如何自动应用来自 Yii2 扩展的迁移 [英] How to atomatically apply migrations that comes from Yii2 extensions
问题描述
我已经使用 Composer 为 Yii2 dektrium/yii2-user 安装了一个扩展,使用它的要求"部分.此扩展包含数据库迁移.是否可以使用不像这样的控制台语法从这个扩展应用迁移:
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?
推荐答案
如果你想让这个过程自动化,你可以使用 composer
的 scripts
属性.有关更多信息,您可以查看 https://getcomposer.org/doc/articles/scripts.md一>.在你的情况下,你可以在 composer.json
上用这样的东西来实现你的目标:
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
}
我更喜欢将在安装或更新后必须运行的所有命令保存在项目根目录中的文件(例如名为 commands
的文件)上,如下所示:
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
并在 composer.json
文件中输入其名称:
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
}
所以每次在 composer install
或 composer update
之后,都会运行所有命令(这对团队合作很有用).
So each time after composer install
or composer update
, all commands will be run (and it's useful on teamwork).
这篇关于如何自动应用来自 Yii2 扩展的迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!