Few hours of each weekend I try to spend on developing my Financier app, which is nothing more than my interactive wallet to invest in market stock. It’s really easy command line app, that has two table in database. I have some plans for this application, that is why I’ve decided to implement library to manage database migrations.
I choose Doctrine Migrations that I usually use when I am working with symfony framework. This time, I wanted to use it on application where I don’t use any framework. First time I had to dive into documentation of this library, and it was terrible like diving into the dark ocean.
In this post I will describe how to use Doctrine Migrations without any framework (like Symfony, Laravel, Lumen or Zend), just plain PHP. Don’t worry, it’s easy, so don’t be scared.
Doctrine Migrations, install it by composer
You need composer. Composer is a naturally choice if you want to use any PHP library. I warn you, you don’t find it in officiation documentation. Fortunately, GitHub exists.
composer require doctrine/migrations
After execution above command, you have to create two files in root of your project. First is migrations.xml
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-migrations xmlns="http://doctrine-project.org/schemas/migrations/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/migrations/configuration
http://doctrine-project.org/schemas/migrations/configuration.xsd">
<name>Doctrine Sandbox Migrations</name>
<migrations-namespace>DoctrineMigrations</migrations-namespace>
<table name="doctrine_migration_versions" />
<migrations-directory>/app/migrations</migrations-directory>
</doctrine-migrations>
You define here:
migrations-directory
– where you will keep your migrations;migrations-namespace
– each migration is a class, so you need to define namespace;
Second file is migrations-db.php
, which stores access to database.
<?php
return [
'dbname' => '',
'user' => '',
'password' => '',
'host' => '',
'driver' => 'pdo_mysql',
];
If you created correctly both files, you can use ./vendor/bin/doctrine-migrations
from your command line. You should see similar to that:
Doctrine Migrations v1.5.0
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
help Displays help for a command
list Lists commands
migrations
migrations:execute Execute a single migration version up or down manually.
migrations:generate Generate a blank migration class.
migrations:latest Outputs the latest version number
migrations:migrate Execute a migration to a specified version or the latest available version.
migrations:status View the status of a set of migrations.
migrations:up-to-date Tells you if your schema is up-to-date.
migrations:version Manually add and delete migration versions from the version table.