vgarvardt/zf-simple-migrations

此软件包已被弃用,不再维护。未建议替代包。

数据库迁移管理模块。

0.1.0 2014-09-10 10:46 UTC

This package is not auto-updated.

Last update: 2023-01-18 02:57:46 UTC


README

Zend Framework 2的简单迁移。项目最初基于ZendDbMigrations,但由于模块作者未对问题或拉取请求作出回应,因此分支变成了独立项目。

支持的驱动程序

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

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