Git Product home page Git Product logo

migres's Introduction

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

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

Renaming a table

<?php declare(strict_types=1);

namespace Vendor\Migrations;

use PeeHaa\Migres\MigrationSpecification;

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

Dropping a table

<?php declare(strict_types=1);

namespace Vendor\Migrations;

use PeeHaa\Migres\MigrationSpecification;

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

Table methods

The table object defines the following methods:

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');

Command line

Setup

./vendor/bin/migres setup

This will run the setup wizard which guides you through the process of setting up the configuration.

Create new migration

./vendor/bin/migres create NewMigrationName

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

Run migrations

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

Run the migrations

Run rollbacks

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

Rolls back the migrations

migres's People

Contributors

peehaa avatar

Stargazers

 avatar Wilson Montoya avatar Timothy Jacobs avatar Félix Gagnon-Grenier avatar Cyril de Wit avatar Mehmet Korkmaz avatar  avatar Haydar KÜLEKCİ avatar Thomas Gachet avatar Marc Addeo avatar  avatar Joël avatar Isaac Rozas García avatar Kevin avatar James Mallison avatar  avatar Ekin avatar

Watchers

James Cloos avatar Ekin avatar Félix Gagnon-Grenier avatar

migres's Issues

Implement references

$table->addForeignKey('name_fkey', 'column_name')->references('other_table', 'column_name');
$table->addForeignKey('name_fkey', 'column_name')
    ->references('other_table', 'column_name')
    ->onDeleteCascade()
;
$table->addForeignKey('name_fkey', 'column_name')
    ->references('other_table', 'column_name')
    ->onDeleteRestrict()
;
$table->addForeignKey('name_fkey', 'column_name')
    ->references('other_table', 'column_name')
    ->onDeleteNoAction()
;
$table->addForeignKey('name_fkey', 'column_name')
    ->references('other_table', 'column_name')
    ->onUpdateCascade()
;
$table->addForeignKey('name_fkey', 'column_name')
    ->references('other_table', 'column_name')
    ->onUpdateRestrict()
;
$table->addForeignKey('name_fkey', 'column_name')
    ->references('other_table', 'column_name')
    ->onUpdateNoAction()
;

Implement type safe defaults

Currently the type declaration of Column::default() is mixed.

We instead should return Typed columns so we can declare the types on the default

Remove exits from commands

Currently we are using exits in commands. Remove these and handle errors higher up so we can also programmatically call commands instead of only from the CLI.

Add more index methods

$table->addIndex('users_name', 'name')->concurrently();
$table->addIndex('users_name', 'name')->where("name = 'foobar'");

Table::changeColumn should have an optional data type

Currently the data type is required for the changeColumn method.

This should probably just be optional as you may want to change just the column options.

Another related thing is column options not set on changeColumn calls should not change

Simplify the retrospector by simply getting the SQL definitions

Using pg_get_constraintdef we can get the definitions of constraints directly instead of having to build it ourselves. This makes the retrospector a lot simpler and saner.

SELECT conrelid::regclass AS table_from
     , conname
     , pg_get_constraintdef(c.oid)
FROM   pg_constraint c
JOIN   pg_namespace n ON n.oid = c.connamespace
WHERE  n.nspname = 'public' -- your schema here
ORDER  BY conrelid::regclass::text, contype DESC;

We can limit the query by targeting contype = 'f', contype = 'p', contype = 'u'

Separate all actions

Current there are several actions handled by the same ActionHandler (e.g. Index).
Move these to dedicated action handlers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.