zored/zf-simple-migrations

数据库迁移管理模块。

0.2.1 2018-01-19 14:50 UTC

This package is not auto-updated.

Last update: 2024-09-29 03:36:29 UTC


README

为 Zend Framework 2 设计的简单迁移工具。该项目最初基于 ZendDbMigrations,但由于模块作者没有对问题和拉取请求做出回应,因此分支成为了一个独立的项目。

支持的驱动程序

以下 DB 适配器驱动程序由本模块支持。

  • 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'    
    ),
),