t4web / migrations
ZF2 数据库迁移管理模块。
2.0.3
2016-03-13 08:20 UTC
Requires
- php: >=5.5
- t4web/filesystem: 1.0.0
- zendframework/zend-console: ^2.5
- zendframework/zend-db: ^2.5
- zendframework/zend-modulemanager: ^2.5
- zendframework/zend-mvc: ^2.5
- zendframework/zend-servicemanager: ^2.5
- zendframework/zend-text: ^2.5
Requires (Dev)
- phpunit/phpunit: ^4.8
- squizlabs/php_codesniffer: ^2.3
README
迁移
为 Zend Framework 2 提供的简单迁移。
支持的驱动器
此模块支持以下数据库适配器驱动器。
- Pdo_Sqlite
- Pdo_Mysql
- Mysqli 只有当你通过
'buffer_results' => true
配置驱动器选项时
安装
使用 composer
php composer.phar require t4web/migrations
或将其添加到您的 composer.json 中
"require": { "t4web/migrations": "^2.0.0" }
然后,将 T4web\Migrations
添加到 application.config.php 中的 modules
数组中
配置
添加到您的配置中
'migrations' => array( 'dir' => dirname(__FILE__) . '/../../../migrations', 'namespace' => 'T4web\Migrations', 'adapter' => 'Zend\Db\Adapter\Adapter', 'show_log' => true ),
常规选项
用于配置此模块的最高级键是 migrations
。
迁移配置:迁移
migrations
下的每个键都是一个迁移配置。
子键: dir
存储迁移文件的目录的路径。默认为项目根目录中的 ./migrations
。
子键: namespace
迁移类将使用此类命名空间生成。默认为 T4web\Migrations
。
子键: show_log
(可选)
日志输出迁移的标志。默认为 true
。
子键: adapter
(可选)
将用于从服务管理器获取 Zend\Db\Adapter\Adapter
的服务别名。
用法
可用命令
migration version
- 显示最后应用的迁移(name
指定配置的迁移)migration list [--all]
- 列出可用的迁移(all
包括已应用的迁移)migration apply [<version>] [--force] [--down]
- 应用或回滚迁移migration generate
- 生成迁移类
迁移类默认存储在 /path/to/project/migrations/
目录中。
通用迁移类的名称为 Version_<YmdHis>
并实现 T4web\Migrations\Migration\MigrationInterface
。
迁移类示例
<?php namespace T4web\Migrations; use `T4web\Migrations\Migration\AbstractMigration; class Version_20130403165433 extends AbstractMigration { public static $description = "Migration description"; public function up() { /** @var Zend\Db\ResultSet\ResultSet $result */ //$result = $this->executeQuery(/*Sql instruction*/); } public function down() { //throw new \RuntimeException('No way to go down!'); //$this->executeQuery(/*Sql instruction*/); } }
在迁移类中访问 ServiceLocator
<?php namespace T4web\Migrations; use T4web\Migrations\Migration\AbstractMigration; class Version_20130403165433 extends AbstractMigration { public static $description = "Migration description"; public function up() { //$this->getServiceLocator()->get(/*Get service by alias*/); } public function down() { //$this->getServiceLocator()->get(/*Get service by alias*/); } }