vgarvardt/zf-simple-migrations 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vgarvardt/zf-simple-migrations

最新稳定版本:0.1.0

Composer 安装命令:

composer require vgarvardt/zf-simple-migrations

包简介

Module for database migrations management.

README 文档

README

Simple Migrations for Zend Framework 2. Project originally based on ZendDbMigrations but module author did not response for issues and pull-requests so fork became independent project.

Supported Drivers

The following DB adapter drivers are supported by this module.

  • Pdo_Sqlite
  • Pdo_Mysql
  • Pdo_Pgsql
  • Mysqli only if you configure the driver options with 'buffer_results' => true

Installation

Using composer

php composer.phar require vgarvardt/zf-simple-migrations:dev-master
php composer.phar update

add ZfSimpleMigrations to the modules array in application.config.php

Usage

Available commands

  • migration version [<name>] - show last applied migration (name specifies a configured migration)
  • migration list [<name>] [--all] - list available migrations (all includes applied migrations)
  • migration apply [<name>] [<version>] [--force] [--down] [--fake] - apply or rollback migration
  • migration generate [<name>] - generate migration skeleton class

Migration classes are stored in /path/to/project/migrations/ dir by default.

Generic migration class has name Version<YmdHis> and implement ZfSimpleMigrations\Library\MigrationInterface.

Migration class example

<?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*/);
    }
}

Multi-statement sql

While this module supports execution of multiple SQL statements it does not have way to detect if any other statement than the first contained an error. It is highly recommended you only provide single SQL statements to addSql at a time. I.e instead of

$this->addSql('SELECT NOW(); SELECT NOW(); SELECT NOW();');

You should use

$this->addSql('SELECT NOW();');
$this->addSql('SELECT NOW();');
$this->addSql('SELECT NOW();');

Accessing ServiceLocator In Migration Class

By implementing the Zend\ServiceManager\ServiceLocatorAwareInterface in your migration class you get access to the ServiceLocator used in the application.

<?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*/);
    }
}

Accessing Zend Db Adapter In Migration Class

By implementing the Zend\Db\Adapter\AdapterAwareInterface in your migration class you get access to the Db Adapter configured for the migration.

<?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()));
    }
}

Configuration

User Configuration

The top-level key used to configure this module is migrations.

Migration Configurations: Migrations Name

Each key under migrations is a migrations configuration, and the value is an array with one or more of the following keys.

Sub-key: dir

The path to the directory where migration files are stored. Defaults to ./migrations in the project root dir.

Sub-key: namespace

The class namespace that migration classes will be generated with. Defaults to ZfSimpleMigrations\Migrations.

Sub-key: show_log (optional)

Flag to log output of the migration. Defaults to true.

Sub-key: adapter (optional)

The service alias that will be used to fetch a Zend\Db\Adapter\Adapter from the service manager.

User configuration example

'migrations' => array(
    'default' => array(
            'dir' => dirname(__FILE__) . '/../../../../migrations-app',
            'namespace' => 'App\Migrations',    
    ),
    'albums' => array(
            'dir' => dirname(__FILE__) . '/../../../../migrations-albums',
            'namespace' => 'Albums\Migrations',
            'adapter' => 'AlbumDb'    
    ),
),

vgarvardt/zf-simple-migrations 适用场景与选型建议

vgarvardt/zf-simple-migrations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.34k 次下载、GitHub Stars 达 29, 最近一次更新时间为 2013 年 04 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「migrations」 「db」 「zf2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 vgarvardt/zf-simple-migrations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 vgarvardt/zf-simple-migrations 我们能提供哪些服务?
定制开发 / 二次开发

基于 vgarvardt/zf-simple-migrations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 20.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 29
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 29
  • Watchers: 9
  • Forks: 23
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2013-04-03