nkovacs / migrate-command
这是Yii数据库迁移工具的增强版本,增加了模块支持以及许多实用的功能。
Requires
- php: >=5.1.0
- yiisoft/yii: 1.1.*
This package is auto-updated.
Last update: 2024-08-25 03:21:59 UTC
README
此扩展是Yii数据库迁移工具的增强版本,增加了模块支持以及更多实用的功能。如果您想添加任何内容,或者发现了任何错误,请通过GitHub或通过电子邮件联系我。
功能
- 模块支持(迁移分布在每个模块的单独文件夹中),因此您可以...
- ...启用和禁用模块
- ...通过迁移添加新模块
- ...通过运行其迁移向下删除模块
- ...选择您要在每次运行中运行迁移的模块
- ...声明模块依赖(即将推出)
- ...根据模块使用不同的迁移模板(即将推出)
资源
需求
- Yii 1.1.6或更高版本(MigrateCommand从该版本开始引入)。如果您复制MigrateCommand和CDbMigration,您应该能够使用任何yii版本使用此扩展。
安装
- 在
protected/extensions
下提取发布文件。 - 将以下内容添加到yiic命令的配置文件中
'commandMap' => array( 'migrate' => array( // alias of the path where you extracted the zip file 'class' => 'application.extensions.yiiext.commands.migrate.EMigrateCommand', // this is the path where you want your core application migrations to be created 'migrationPath' => 'application.db.migrations', // the name of the table created in your database to save versioning information 'migrationTable' => 'tbl_migration', // the application migrations are in a pseudo-module called "core" by default 'applicationModuleName' => 'core', // define all available modules (if you do not set this, modules will be set from yii app config) 'modulePaths' => array( 'admin' => 'application.modules.admin.db.migrations', 'user' => 'application.modules.user.db.migrations', 'yourModule' => 'application.any.other.path.possible', // ... ), // you can customize the modules migrations subdirectory which is used when you are using yii module config 'migrationSubPath' => 'migrations', // here you can configure which modules should be active, you can disable a module by adding its name to this array 'disabledModules' => array( 'admin', 'anOtherModule', // ... ), // the name of the application component that should be used to connect to the database 'connectionID'=>'db', // alias of the template file used to create new migrations 'templateFile'=>'application.db.migration_template', ), ),
请注意:如果您之前已经使用了MigrateCommand,请确保将模块列添加到您的迁移表中
ALTER TABLE `tbl_migration` ADD COLUMN `module` varchar(32) DEFAULT NULL; UPDATE `tbl_migration` SET module='core';
使用方法
###通用用法
您可以通过运行yiic migrate help
来查看所有参数和简短的示例,说明如何使用它们。
基础知识在Yii definitive guide中解释。如果您以前没有使用过数据库迁移,请先阅读这些内容。扩展迁移命令的使用与原生命令没有太大区别。唯一的区别是create命令,您必须另外指定模块名称
yiic migrate create modulename create_user_table
这将创建一个名为'create_user_table'的新迁移,位于'modulename'模块中。原生用法
yiic migrate create create_user_table
在应用程序(核心)中创建一个名为'create_user_table'的新迁移。
###--module 参数
在其他所有命令(up
、down
、history
、new
、to
和mark
)中,您可以使用参数--module=<modulenames>
,其中<modulenames>
可以是模块名称的逗号分隔列表或单个模块名称。此参数将限制当前命令仅影响指定的模块。一些示例
yiic migrate new --module=core
这将显示core模块的所有新迁移
yiic migrate up 5 --module=core,user
将迁移下一个模块 core 和 user 中的 5 个新迁移。如果其他模块中有新的迁移,则将被忽略。
yiic migrate history --module=core,user
将显示过去已应用的模块 core 和 user 的迁移。如果您没有指定模块,则命令行为类似于原生命令,并为所有模块执行迁移。
###添加模块
只需在您的配置中启用它,然后运行 yiic migrate up --module=yourModule
。
###移除模块
运行 yiic migrate to m000000_000000 --module=yourModule
。为了使此命令生效,您必须确保所有迁移都正确实现了 down() 方法。