Coder Social home page Coder Social logo

databasemigrations's Introduction

Database Migrations

Build Status

Migrations

To write a migration, create a class that is discoverable by Finder that you are using, and extend Migration class:

<?php

namespace Acme\App\Migrations;

use ActiveCollab\DatabaseMigrations\Migration\Migration;

class AddUserRolesTable extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function up()
    {
    }
}

If you don't like how Migration class is structured, you can write your migrations any way you want, as long as you implement MigrationInterface:

<?php

namespace Acme\App\Migrations;

use ActiveCollab\DatabaseMigrations\Migration\MigrationInterface;

class AddUserRolesTable implements MigrationInterface
{
    …
}

Migrations that extend Migration class get two important properties injected via constructor:

  1. connection - a ActiveCollab\DatabaseConnection\ConnectionInterface instance with valid connection to the database, and
  2. log - a PSR-3 LoggerInterface instance.
<?php

namespace Acme\App\Migrations;

use ActiveCollab\DatabaseMigrations\Migration\Migration;

class AddUserRolesTable extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function up()
    {
        if (!in_array('user_roles', $this->connection->getTableNames()) {
            $this->logger->debug('{table} not found in the database', ['table' => 'user_roles']);
            $thos->connection->execute('CREATE TABLE STATEMENT');
            $this->logger->debug('{table} created', ['table' => 'user_roles']);
        }
    }
}

Finders

Migration classes are "discovered" using objects that implement FinderInterface interface. All that we expect from finders is that they return an array where key is migration class name, and value is path where we can expect to find the class definition. This makes migration library independent on directory and file structure that you decide to use to organize your migrations.

This library currently implements only one Finder - Migrations in Changesets.

Migrations in a Changeset Finder

What we found to work really well for Active Collab project are migrations that are grouped in changesets. A changeset is a directory that has one or more related migrations. Valid format of the changeset directory name is YYYY-MM-DD-what-this-is-all-about. Here's a couple of valid changeset names:

  • 2016-01-02-add-invoicing-module
  • 2016-03-12-remove-is-trashed-project-field
  • 2016-12-09-fix-users-table-indexes

Timestamp part of the changeset name is used for sorting, and details part is used to make it clear what the changeset is all about.

Command Line

Database Migrations package includes a couple of traits that make implementation of commands that work with migrations easy. These commands are:

  • List all migrations and their status (All)
  • Run all non-executed migrations (Up)
  • Create a new migration file (Create)

In order to use these traits, you need to include them in a regular Symfony Console class, and provide implementation of getMigrations() method. This method needs to return a valid, configured MigrationsInterface instance.

Create helper has extra requirements and extension points that lets you configure how migration stubs are created. Please check protected and abstract methods under Override comment in /src/Command/Create.php file for details.

To Do

  1. Add basic field, index and foreign key management helper methods to the base Migration implementation

databasemigrations's People

Contributors

ilijastuden avatar

Watchers

 avatar  avatar

Forkers

mickaelandrieu

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.