项目作者: PeeHaa

项目描述 :
Postgresql migration tool
高级语言: PHP
项目地址: git://github.com/PeeHaa/migres.git
创建时间: 2019-07-11T17:13:05Z
项目社区:https://github.com/PeeHaa/migres

开源协议:MIT License

下载


Migres

The PostgreSQL migration tool

Latest Stable Version
Build Status
Build status
Coverage Status
License

Requirements

  • PHP 7.4
  • PostgreSQL 9.5

Usage

Note: this is alpha software. Do not use in production (yet). I would appreciate if you could test it and provide feedback in GitHub issues. <3

Warning: never allow untrusted input in table specifications as all migrations are translated to raw SQL!

  • Add the project using composer composer install peehaa/migres
  • Run the setup ./vendor/bin/migres setup
  • Run without arguments to view the available commands ./vendor/bin/migres

All native PostgreSQL data types are implemented and the list can be found at: https://github.com/PeeHaa/migres/tree/master/src/DataType

TOC

Creating a table

  1. <?php declare(strict_types=1);
  2. namespace Vendor\Migrations;
  3. use PeeHaa\Migres\DataType\BigSerial;
  4. use PeeHaa\Migres\DataType\Boolean;
  5. use PeeHaa\Migres\DataType\CharacterVarying;
  6. use PeeHaa\Migres\MigrationSpecification;
  7. use PeeHaa\Migres\Specification\Table;
  8. class CreateTable extends MigrationSpecification
  9. {
  10. public function change(): void
  11. {
  12. $this->createTable('users', function (Table $table) {
  13. $table->addColumn('id', new BigSerial());
  14. $table->addColumn('is_admin', new Boolean())->notNull()->default(false);
  15. $table->addColumn('name', new CharacterVarying(128))->notNull();
  16. $table->addColumn('email_address', new CharacterVarying(255))->notNull();
  17. $table->primaryKey('id');
  18. $table->addIndex('users_name', 'name');
  19. $table->addUniqueConstraint('email_address_unq', 'email_address');
  20. });
  21. }
  22. }

Renaming a table

  1. <?php declare(strict_types=1);
  2. namespace Vendor\Migrations;
  3. use PeeHaa\Migres\MigrationSpecification;
  4. class RenameTable extends MigrationSpecification
  5. {
  6. public function change(): void
  7. {
  8. $this->renameTable('users', 'members');
  9. }
  10. }

Dropping a table

  1. <?php declare(strict_types=1);
  2. namespace Vendor\Migrations;
  3. use PeeHaa\Migres\MigrationSpecification;
  4. class RenameTable extends MigrationSpecification
  5. {
  6. public function change(): void
  7. {
  8. $this->dropTable('members');
  9. }
  10. }

Table methods

The table object defines the following methods:

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

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

Table::dropColumn(string $name)

  1. $table->dropColumn('column_name');

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

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

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

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

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

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

Table::dropPrimaryKey([string $name])

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

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

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

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

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

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

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

Table::dropUniqueConstraint(string $constraintName)

  1. $table->dropUniqueConstraint('constraint_name');

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

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

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

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

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

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

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

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

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

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

Table::dropIndex(string $indexName)

  1. $table->dropIndex('name_idx');

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

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

Table::dropCheck(string $checkName)

  1. $table->dropCheck('bigger_than_10_chk');

Command line

Setup

```shell script
./vendor/bin/migres setup

  1. This will run the setup wizard which guides you through the process of setting up the configuration.
  2. #### Create new migration
  3. ```shell script
  4. ./vendor/bin/migres create NewMigrationName

This will create a new migration and writes the file to the migrations directory.

Run migrations

```shell script
./vendor/bin/migres migrate [-v[v][v]]

  1. Run the migrations
  2. #### Run rollbacks
  3. ```shell script
  4. ./vendor/bin/migres rollback [-v[v][v]]

Rolls back the migrations