andrey-mokhov/anelegan-db

为Zend框架提供的迁移

1.0.2 2016-04-24 16:55 UTC

This package is auto-updated.

Last update: 2024-09-24 17:09:27 UTC


README

此包允许您为基于ZF2的解决方案组织数据库迁移。

对不起我的英语不好。俄语文档可以在以下链接找到:这里

使用Composer安装

通过命令安装安装包

composer require andrey-mokhov/anelegan-db

安装包后,在 config/application.config.php 中添加模块

<?php
return [
    // This should be an array of module namespaces used in the application.
    'modules' => [
        'Anelegan\Db', // <- added migration module
        // ... and yours modules
    ],
    // ... other settings
];

迁移开发

迁移模块支持通过实现接口 Anelegan\Db\Migration\MigrationInterface 的迁移。

自迁移开发

您的类必须实现接口 Anelegan\Db\Migration\MigrationInterface

<?php
namespace Anelegan\Db\Migration;

interface MigrationInterface
{
    /**
     * @return array
     */
    public function getDependencies();

    /**
     * @return string
     */
    public function getName();

    /**
     * @return bool
     */
    public function setUp();

    /**
     * @return bool
     */
    public function tearDown();
}
  1. 方法 getDependencies 必须返回包含依赖迁移名称的列表。
  2. 方法 getName 必须返回与配置文件中 migration_manageraliases 数组中的键相等的迁移名称。
  3. 方法 setUp 必须更改数据库的模式和/或数据。
  4. 方法 tearDown 必须回滚 setUp 方法的变化。

使用AbstractMigration开发迁移

在迁移包中,您可以找到 Anelegan\Db\Migration\AbstractMigration。这个抽象类可以简化迁移的开发。当继承这个抽象类时,您必须实现以下方法

<?php
namespace Application\Migration;

use Anelegan\Db\Migration\AbstractMigration;

class CreateTesting extends AbstractMigration
{
    /**
     * If your migration does not depend on other migrations, this
     * method can not determine, he has implemented in the abstract
     * class.
     * 
     * @return array
     */
    public function getDependencies()
    {
        return [];
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'testing';
    }
}

抽象类还有两个方法

  • 方法 safeUp - 在事务开始后由 setUp 调用。该方法可以更改数据库。
  • 方法 safeDown - 在事务开始后由 tearDown 调用。该方法必须回滚 safeUp 方法的更改。

迁移示例

创建文件 module/Application/src/Application/Migration/CreateTesting.php,内容如下

<?php
namespace Application\Migration;

use Anelegan\Db\Migration\AbstractMigration;
use Zend\Db\Sql\Ddl\Constraint\UniqueKey;

class CreateTesting extends AbstractMigration
{
    /**
     * Return migration name
     *
     * @return string
     */
    public function getName()
    {
        return 'testing';
    }

    /**
     * Create table with testing name
     *
     * @return bool
     */
    protected function safeUp()
    {
        $tableDefinition = $this->createTable('testing', [
            'id' => $this->primaryKey(),
            'name' => $this->string()->setNullable(false)->addConstraint(new UniqueKey()),
            'description' => $this->text(),
        ], ['comment' => 'Test creating table']);
        $this->execute($tableDefinition);

        return true;
    }

    /**
     * Drop table with testing name
     *
     * @return bool
     */
    protected function safeDown()
    {
        $dropTable = $this->dropTable('testing');
        $this->execute($dropTable);

        return true;
    }
}

安装迁移

所有迁移都必须在配置文件中的 migration_manager 部分中定义。

配置

例如,创建文件 config/autoload/migration.local.php,内容如下

<?php
use Application\Migration\CreateTesting;

return [
    'migration_manager' => [
        'aliases' => [
            // this key "testing" must match result of calling method (new CreateTesting)->getName();
            'testing' => CreateTesting::class, 
        ],
        'factories' => [
            CreateTesting::class => InvokableFactory::class,
        ],
    ],
];

查看可用的迁移列表

打开shell并执行

# php public/index.php migrate list
 Installed migration:
    none
 Available migrations:
    > testing

安装迁移

要安装可用的迁移,请执行

# php public/index.php migrate up
 Available migrations:
    > testing
 Install available migration? [y/n] y
 +  Installing "initialization" migration...
    "initialization" migration successfully installed.
 +  Installing "testing" migration...
    > create table "testing"... done (time: 0.265s)
    "testing" migration successfully installed.

"初始化"迁移默认安装在系统中。此迁移允许您控制已安装的迁移及其依赖关系。

删除已安装的迁移

要删除已安装的迁移,请执行

# php public/index.php migrate down
 Installed migrations:
    > testing
 Uninstall "testing" migration? [y/n]
 -  Uninstall "testing" migration...
    > drop table "testing"... done (time: 0.061s)
    "testing" migration successfully uninstalled.

支持的数据库系统

当前版本支持

  • MySQL
  • PostgreSQL