The PostgreSQL migration tool
- PHP 7.4
- PostgreSQL 9.5
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
<?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');
}
}
The table object defines the following methods:
$table->addColumn('column_name', new Integer());
$table->addColumn('column_name', new Integer())->notNull;
$table->addColumn('column_name', new Integer())->default(12);
$table->dropColumn('column_name');
$table->renameColumn('old_name', 'new_name');
$table->changeColumn('column_name', new IntegerType());
$table->changeColumn('column_name', new IntegerType())->notNull();
$table->changeColumn('column_name', new IntegerType())->default(12);
$table->primaryKey('column_name');
$table->primaryKey('column_name1', 'column_name2');
$table->dropPrimaryKey();
$table->dropPrimaryKey('table_name_pkey');
$table->namedPrimaryKey('custom_name_pkey', 'column_name');
$table->namedPrimaryKey('custom_name_pkey', 'column_name1', 'column_name2');
$table->renamePrimaryKey('old_name', 'new_name');
$table->addUniqueConstraint('constraint_name', 'column_name');
$table->addUniqueConstraint('constraint_name', 'column_name1', 'column_name2');
$table->dropUniqueConstraint('constraint_name');
$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('name_idx', 'column_name');
$table->addBtreeIndex('name_idx', 'column_name DESC');
$table->addBtreeIndex('name_idx', 'column_name1 DESC', 'column_name2 DESC');
$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('name_idx', 'column_name');
$table->addGistIndex('name_idx', 'column_name DESC');
$table->addGistIndex('name_idx', 'column_name1 DESC', 'column_name2 DESC');
$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('name_idx');
$table->addCheck('bigger_than_10_chk', 'column_name > 10');
$table->dropCheck('bigger_than_10_chk');
./vendor/bin/migres setup
This will run the setup wizard which guides you through the process of setting up the configuration.
./vendor/bin/migres create NewMigrationName
This will create a new migration and writes the file to the migrations directory.
./vendor/bin/migres migrate [-v[v][v]]
Run the migrations
./vendor/bin/migres rollback [-v[v][v]]
Rolls back the migrations