peehaa/migres

Postgresql 迁移工具

维护者

详细信息

github.com/PeeHaa/migres

源代码

问题

安装次数: 12

依赖关系: 0

建议者: 0

安全: 0

星标: 17

关注者: 3

分支: 0

开放问题: 8

类型:项目

v0.2.0 2019-08-07 19:51 UTC

This package is auto-updated.

Last update: 2024-08-29 04:49:10 UTC


README

PostgreSQL 迁移工具

Latest Stable Version Build Status Build status Coverage Status License

要求

  • PHP 7.4
  • PostgreSQL 9.5

用法

注意:这是 alpha 软件版本。请勿在生产环境中使用(目前)。如果您能测试并在此GitHub issues中提供反馈,我将不胜感激。<3

警告:永远不要在表规范中允许不受信任的输入,因为所有迁移都会转换为原始 SQL!

  • 使用 composer 安装项目 composer install peehaa/migres
  • 运行设置 ./vendor/bin/migres setup
  • 不带参数运行以查看可用的命令 ./vendor/bin/migres

实现了所有原生 PostgreSQL 数据类型,列表可在以下位置找到:https://github.com/PeeHaa/migres/tree/master/src/DataType

目录

创建表

<?php declare(strict_types=1);

namespace Vendor\Migrations;

use PeeHaa\Migres\DataType\BigSerial;
use PeeHaa\Migres\DataType\Boolean;
use PeeHaa\Migres\DataType\CharacterVarying;
use PeeHaa\Migres\MigrationSpecification;
use PeeHaa\Migres\Specification\Table;

class CreateTable extends MigrationSpecification
{
    public function change(): void
    {
        $this->createTable('users', function (Table $table) {
            $table->addColumn('id', new BigSerial());
            $table->addColumn('is_admin', new Boolean())->notNull()->default(false);
            $table->addColumn('name', new CharacterVarying(128))->notNull();
            $table->addColumn('email_address', new CharacterVarying(255))->notNull();
            $table->primaryKey('id');
            $table->addIndex('users_name', 'name');
            $table->addUniqueConstraint('email_address_unq', 'email_address');
        });
    }
}

重命名表

<?php declare(strict_types=1);

namespace Vendor\Migrations;

use PeeHaa\Migres\MigrationSpecification;

class RenameTable extends MigrationSpecification
{
    public function change(): void
    {
        $this->renameTable('users', 'members');
    }
}

删除表

<?php declare(strict_types=1);

namespace Vendor\Migrations;

use PeeHaa\Migres\MigrationSpecification;

class RenameTable extends MigrationSpecification
{
    public function change(): void
    {
        $this->dropTable('members');
    }
}

表方法

表对象定义了以下方法

Table::addColumn(string $name, \Migres\DataType\Type $dataType)

$table->addColumn('column_name', new Integer());
$table->addColumn('column_name', new Integer())->notNull;
$table->addColumn('column_name', new Integer())->default(12);

Table::dropColumn(string $name)

$table->dropColumn('column_name');

Table::renameColumn(string $oldName, string $newName)

$table->renameColumn('old_name', 'new_name');

Table::changeColumn(string $name, \Migres\DataType\Type $dataType)

$table->changeColumn('column_name', new IntegerType());
$table->changeColumn('column_name', new IntegerType())->notNull();
$table->changeColumn('column_name', new IntegerType())->default(12);

Table::primaryKey(string $column, [string ...$columns])

$table->primaryKey('column_name');
$table->primaryKey('column_name1', 'column_name2');

Table::dropPrimaryKey([string $name])

$table->dropPrimaryKey();
$table->dropPrimaryKey('table_name_pkey');

Table::namedPrimaryKey(string $name, string $column, [string ...$columns])

$table->namedPrimaryKey('custom_name_pkey', 'column_name');
$table->namedPrimaryKey('custom_name_pkey', 'column_name1', 'column_name2');

Table::renamePrimaryKey(string $oldName, string $newName)

$table->renamePrimaryKey('old_name', 'new_name');

Table::addUniqueConstraint(string $constraintName, string $column, [string ...$columns])

$table->addUniqueConstraint('constraint_name', 'column_name');
$table->addUniqueConstraint('constraint_name', 'column_name1', 'column_name2');

Table::dropUniqueConstraint(string $constraintName)

$table->dropUniqueConstraint('constraint_name');

Table::addIndex(string $indexName, string $column, [string ...$columns])

$table->addIndex('name_idx', 'column_name');
$table->addIndex('name_idx', 'column_name DESC');
$table->addIndex('name_idx', 'column_name1 DESC', 'column_name2 DESC');

Table::addBtreeIndex(string $indexName, string $column, [string ...$columns])

$table->addBtreeIndex('name_idx', 'column_name');
$table->addBtreeIndex('name_idx', 'column_name DESC');
$table->addBtreeIndex('name_idx', 'column_name1 DESC', 'column_name2 DESC');

Table::addHashIndex(string $indexName, string $column, [string ...$columns])

$table->addHashIndex('name_idx', 'column_name');
$table->addHashIndex('name_idx', 'column_name DESC');
$table->addHashIndex('name_idx', 'column_name1 DESC', 'column_name2 DESC');

Table::addGistIndex(string $indexName, string $column, [string ...$columns])

$table->addGistIndex('name_idx', 'column_name');
$table->addGistIndex('name_idx', 'column_name DESC');
$table->addGistIndex('name_idx', 'column_name1 DESC', 'column_name2 DESC');

Table::addGinIndex(string $indexName, string $column, [string ...$columns])

$table->addGinIndex('name_idx', 'column_name');
$table->addGinIndex('name_idx', 'column_name DESC');
$table->addGinIndex('name_idx', 'column_name1 DESC', 'column_name2 DESC');

Table::dropIndex(string $indexName)

$table->dropIndex('name_idx');

Table::addCheck(string $checkName, string $expression)

$table->addCheck('bigger_than_10_chk', 'column_name > 10');

Table::dropCheck(string $checkName)

$table->dropCheck('bigger_than_10_chk');

命令行

设置

./vendor/bin/migres setup

这将运行设置向导,引导您完成配置设置过程。

创建新迁移

./vendor/bin/migres create NewMigrationName

这将创建一个新的迁移并将文件写入迁移目录。

运行迁移

./vendor/bin/migres migrate [-v[v][v]]

运行迁移

运行回滚

./vendor/bin/migres rollback [-v[v][v]]

回滚迁移