Doctrine is an ORM open source application written in PHP: it is widely used, integrated in Symfony and easy to use in Zend Framework, Silex etc...  The main purpose of an ORM application is database mapping, providing objects and entities to be used within your projects. 

Our software development team worked on this tool and on other projects too. Now we are going to talk about Doctrine Migrations, an useful tool for database versioning.

Due to the increasing diffusion of continuous deployment, provisioning and general automation of start-up stages, also database schema updating needs to be automated; this means that you will be no more able to access your database query browser and perform manual queries.   

Doctrine Migrations tool came up to solve this issue: it is a command line tool to create, manage and perform your migrations.

$ ./doctrine list migrations
Doctrine Command Line Interface version 2.0.0BETA3-DEV

Usage:
  [options] command [arguments]

Options:
  --help           -h Display this help message.
  --quiet          -q Do not output any message.
  --verbose        -v Increase verbosity of messages.
  --version        -V Display this program version.
  --color          -c Force ANSI color output.
  --no-interaction -n Do not ask any interactive question.

Available commands for the "migrations" namespace:
  :diff      Generate a migration by comparing your current database to your mapping information.
  :execute   Execute a single migration version up or down manually.
  :generate  Generate a blank migration class.
  :migrate   Execute a migration to a specified version or the latest available version.
  :status    View the status of a set of migrations.
  :version   Manually add and delete migration versions from the version table.

Each single Migrations is a php class, generated via "generate" command and uniquely identified by its name. This class contains the steps needed to perform updating or to undo it, in case of unsuccessful update process.  

namespace DoctrineMigrations;

use Doctrine\DBAL\Migrations\AbstractMigration,
    Doctrine\DBAL\Schema\Schema;

class Version20100416130401 extends AbstractMigration
{
    public function up(Schema $schema)
    {
	$this->addSql('CREATE TABLE users (id INT NOT NULL, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB');
    }

    public function down(Schema $schema)
    {
	$this->addSql('DROP TABLE users);
    }
}

An interesting option offered by such approach (besides the version management itself and the simple handling of up and down operations within classes) is that by accessing PHP, you can perform search queries and updating operations of any complexity on single piece of data.  You can also avoid maintaining and versioning complex sql scripts. 

Comments

comments powered by Disqus

cloudparty

Follow Us