Database Migrations in PHP: Modern Tools and Practices
Database migrations are an integral part of modern PHP development. They allow development teams to synchronize database schema changes, track change history, and automatically apply them across different environments (local environment, staging, production). In this article, we will review the main libraries and tools for migrations in PHP, their installation, configuration, and usage examples.
What are migrations and why are they needed?
A database migration is a script that describes a schema change: creating a table, adding/removing columns, indexes, foreign keys. Migrations allow you to:
- Version database structure changes along with application code.
- Automate deployment on new servers.
- Roll back changes when necessary.
- Work in a team without conflicts.
Popular libraries for migrations in PHP
1. Phinx (robmorgan/phinx)
Phinx is one of the most popular standalone migration libraries. It is not tied to a specific framework and supports MySQL, PostgreSQL, SQLite, SQL Server.
Installation via Composer:
composer require robmorgan/phinxCreating the phinx.yml configuration file:
paths: migrations: '%%PHINX_CONFIG_DIR%%/db/migrations' seeds: '%%PHINX_CONFIG_DIR%%/db/seeds'
environments: default_migration_table: phinxlog default_database: development development: adapter: mysql host: localhost name: myapp_db user: root pass: '' port: 3306 charset: utf8Example migration (creating the users table):
<?php
use Phinx\\Migration\\AbstractMigration;
class CreateUsersTable extends AbstractMigration{ public function change() { $table = $this->table('users', ['id' => false, 'primary_key' => ['id']]); $table->addColumn('id', 'biginteger', ['identity' => true]) ->addColumn('username', 'string', ['limit' => 50]) ->addColumn('email', 'string', ['limit' => 100]) ->addColumn('created_at', 'datetime') ->addColumn('updated_at', 'datetime', ['null' => true]) ->addIndex(['email'], ['unique' => true]) ->create(); }}Running migrations:
vendor/bin/phinx migrate -e development2. Doctrine Migrations (doctrine/migrations)
Doctrine Migrations is part of the Doctrine ORM ecosystem. It is ideal for projects already using Doctrine. Supports all popular database management systems.
Installation:
composer require doctrine/migrationsConfiguration (migrations.php):
<?phpreturn [ 'table_storage' => [ 'table_name' => 'doctrine_migration_versions', ], 'migrations_paths' => [ 'App\\Migrations' => './migrations', ], 'all_or_nothing' => true, 'transactional' => true, 'check_database_platform' => true,];Example migration:
<?php
namespace App\\Migrations;
use Doctrine\\DBAL\\Schema\\Schema;use Doctrine\\Migrations\\AbstractMigration;
class Version20240101000000 extends AbstractMigration{ public function up(Schema $schema): void { $table = $schema->createTable('products'); $table->addColumn('id', 'integer', ['autoincrement' => true]); $table->addColumn('name', 'string', ['length' => 255]); $table->addColumn('price', 'decimal', ['precision' => 10, 'scale' => 2]); $table->se