adamiecradek / zf-simple-migrations
数据库迁移管理模块。
Requires
- php: >=5.4
- zendframework/zend-mvc-console: ^1.1.9
Requires (Dev)
- phpunit/phpunit: 4.6.*
This package is not auto-updated.
Last update: 2022-11-26 08:51:29 UTC
README
为Zend Framework 2提供的简单迁移方案。该项目最初基于ZendDbMigrations,但由于模块作者未对问题和pull请求做出回应,因此分支成为一个独立的项目。
支持的驱动程序
以下数据库适配器驱动程序由本模块支持。
- Pdo_Sqlite
- Pdo_Mysql
- Mysqli 仅当您使用
'buffer_results' => true
配置驱动程序选项时
安装
使用Composer
php composer.phar require vgarvardt/zf-simple-migrations:dev-master php composer.phar update
将ZfSimpleMigrations
添加到application.config.php中的modules
数组中
用法
可用的命令
migration version [<name>]
- 显示最后应用的迁移(name
指定配置的迁移)migration list [<name>] [--all]
- 列出可用的迁移(all
包括应用的迁移)migration apply [<name>] [<version>] [--force] [--down] [--fake]
- 应用或回滚迁移migration generate [<name>]
- 生成迁移骨架类
默认情况下,迁移类存储在/path/to/project/migrations/
目录中。
通用的迁移类名为Version<YmdHis>
,并实现ZfSimpleMigrations\Library\MigrationInterface
。
迁移类示例
<?php namespace ZfSimpleMigrations\Migrations; use ZfSimpleMigrations\Library\AbstractMigration; use Zend\Db\Metadata\MetadataInterface; class Version20130403165433 extends AbstractMigration { public static $description = "Migration description"; public function up(MetadataInterface $schema) { //$this->addSql(/*Sql instruction*/); } public function down(MetadataInterface $schema) { //$this->addSql(/*Sql instruction*/); } }
多语句SQL
虽然此模块支持执行多个SQL语句,但它无法检测除第一个语句之外的其他语句是否包含错误。强烈建议您每次只向addSql
提供单个SQL语句。即,而不是
$this->addSql('SELECT NOW(); SELECT NOW(); SELECT NOW();');
您应该使用
$this->addSql('SELECT NOW();');
$this->addSql('SELECT NOW();');
$this->addSql('SELECT NOW();');
在迁移类中访问ServiceLocator
通过在您的迁移类中实现Zend\ServiceManager\ServiceLocatorAwareInterface
,您可以访问应用程序中使用的ServiceLocator。
<?php namespace ZfSimpleMigrations\Migrations; use ZfSimpleMigrations\Library\AbstractMigration; use Zend\Db\Metadata\MetadataInterface; use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\ServiceManager\ServiceLocatorAwareTrait; class Version20130403165433 extends AbstractMigration implements ServiceLocatorAwareInterface { use ServiceLocatorAwareTrait; public static $description = "Migration description"; public function up(MetadataInterface $schema) { //$this->getServiceLocator()->get(/*Get service by alias*/); //$this->addSql(/*Sql instruction*/); } public function down(MetadataInterface $schema) { //$this->getServiceLocator()->get(/*Get service by alias*/); //$this->addSql(/*Sql instruction*/); } }
在迁移类中访问Zend Db Adapter
通过在您的迁移类中实现Zend\Db\Adapter\AdapterAwareInterface
,您可以访问为迁移配置的Db Adapter。
<?php namespace ZfSimpleMigrations\Migrations; use Zend\Db\Adapter\AdapterAwareInterface; use Zend\Db\Adapter\AdapterAwareTrait; use Zend\Db\Sql\Ddl\Column\Integer; use Zend\Db\Sql\Ddl\Column\Varchar; use Zend\Db\Sql\Ddl\Constraint\PrimaryKey; use Zend\Db\Sql\Ddl\CreateTable; use Zend\Db\Sql\Ddl\DropTable; use ZfSimpleMigrations\Library\AbstractMigration; use Zend\Db\Metadata\MetadataInterface; class Version20150524162247 extends AbstractMigration implements AdapterAwareInterface { use AdapterAwareTrait; public static $description = "Migration description"; public function up(MetadataInterface $schema) { $table = new CreateTable('my_table'); $table->addColumn(new Integer('id', false)); $table->addConstraint(new PrimaryKey('id')); $table->addColumn(new Varchar('my_column', 64)); $this->addSql($table->getSqlString($this->adapter->getPlatform())); } public function down(MetadataInterface $schema) { $drop = new DropTable('my_table'); $this->addSql($drop->getSqlString($this->adapter->getPlatform())); } }
配置
用户配置
用于配置此模块的顶级键是migrations
。
迁移配置:迁移名称
在migrations
下的每个键都是迁移配置,其值是一个包含以下一个或多个键的数组。
子键:dir
存储迁移文件的目录路径。默认为项目根目录下的 ./migrations
。
子键:namespace
迁移类将生成的类命名空间。默认为 ZfSimpleMigrations\Migrations
。
子键:show_log
(可选)
记录迁移输出的标志。默认为 true
。
子键:adapter
(可选)
用于从服务管理器获取 Zend\Db\Adapter\Adapter
的服务别名。
用户配置示例
'migrations' => array( 'default' => array( 'dir' => dirname(__FILE__) . '/../../../../migrations-app', 'namespace' => 'App\Migrations', ), 'albums' => array( 'dir' => dirname(__FILE__) . '/../../../../migrations-albums', 'namespace' => 'Albums\Migrations', 'adapter' => 'AlbumDb' ), ),