PHINX
PAINLESS DATABASE MIGRATIONS WITH PHP
@MICHAELPEACOCK
CTO @ Ground Six
www.michaelpeacock.co.uk
@michaelpeacock
Author: latest book 'Creating development environments
with Vagrant' due to be published within the next few
weeks
Occasionally yabbers on at conferences and user groups
DATABASE MIGRATIONS
Easily manage changes to your database schema
Roll back if you need to
Store them in version control, share with colleagues
INSTALLATION
COMPOSER.JSON
{
"require": {
"robmorgan/phinx": "*"
}
}
INSTALL
curl -s https://getcomposer.org/installer | php
php composer.phar install
INITIALISE YOUR PROJECT
php vendor/bin/phinx init
mkdir migrations
CONFIGURE DATABASE SETTINGS
paths:
migrations: %%PHINX_CONFIG_DIR%%/migrations
environments:
default_migration_table: phinxlog
default_database: development
production:
adapter: mysql
host: localhost
name: production_db
user: root
pass: ''
port: 3306
development:
adapter: mysql
host: localhost
name: development_db
user: root
pass: ''
port: 3306
CREATE MIGRATION
php vendor/bin/phinx create CreateUsersTable
<?php
use PhinxMigrationAbstractMigration;
class CreateUsersTable extends AbstractMigration
{
//public function change() { }
public function up() {}
public function down(){}
}
CREATE A TABLE
public function up()
{
$users = $this->table('users');
$users->addColumn('name', 'string', array('limit' => 100,
'null' => false))
->addColumn('username', 'string', array('limit' => 100, 'null' => false
->addColumn('email', 'string', array('limit' => 200, 'null' => false))
->addColumn('password_hash', 'string', array('limit' => 100, 'null' =>
->addIndex(array('username', 'password_hash'))
->save();
}
ROLLBACK CODE: DOWN
public function down()
{
$this->dropTable('users');
}
TABLE API
Powerful table API lets you create migrations in a relatively
database agnostic way. Underlying functionality differs per
database.
TABLE API: CAVEATS
Not all field types are supported; typically follows ANSI SQL
column types
id field is automatically created (primary key, auto
increment)
A DIFFERENT PRIMARY KEY
$users = $this->table('users', array('id' => 'user_id'));
Non autoincrement primary keys
$user_profiles = $this->table('user_profiles', array('primary_key' => 'user_id'));
ABSTRACT ADAPTER
All implemented database engines implement these (and
more)
public function query($sql);
public function createTable(Table $table);
public function renameTable($tableName, $newName);
public function dropTable($tableName);
public function hasColumn($tableName, $columnName);
public function addColumn(Table $table, Column $column);
public function renameColumn($tableName, $columnName, $newColumnName);
public function changeColumn($tableName, $columnName, Column $newColumn);
public function dropColumn($tableName, $columnName);
public function hasIndex($tableName, $columns);
public function addIndex(Table $table, Index $index);
public function dropIndex($tableName, $columns);
public function dropDatabase($name);
CHANGE
Phinx has a change method which, provided it uses create()
and update() methods from the table API, it will work out how
to either up or down based off the change() code. Requires the
change method to be present. Doesn't work with the save()
method we have used.
MIGRATING
Phinx talk
Phinx talk
Phinx talk
Phinx talk
Phinx talk
Phinx talk

Phinx talk

  • 1.
  • 2.
    @MICHAELPEACOCK CTO @ GroundSix www.michaelpeacock.co.uk @michaelpeacock Author: latest book 'Creating development environments with Vagrant' due to be published within the next few weeks Occasionally yabbers on at conferences and user groups
  • 3.
    DATABASE MIGRATIONS Easily managechanges to your database schema Roll back if you need to Store them in version control, share with colleagues
  • 4.
    INSTALLATION COMPOSER.JSON { "require": { "robmorgan/phinx": "*" } } INSTALL curl-s https://getcomposer.org/installer | php php composer.phar install
  • 5.
    INITIALISE YOUR PROJECT phpvendor/bin/phinx init mkdir migrations
  • 6.
    CONFIGURE DATABASE SETTINGS paths: migrations:%%PHINX_CONFIG_DIR%%/migrations environments: default_migration_table: phinxlog default_database: development production: adapter: mysql host: localhost name: production_db user: root pass: '' port: 3306 development: adapter: mysql host: localhost name: development_db user: root pass: '' port: 3306
  • 7.
  • 8.
    <?php use PhinxMigrationAbstractMigration; class CreateUsersTableextends AbstractMigration { //public function change() { } public function up() {} public function down(){} }
  • 9.
    CREATE A TABLE publicfunction up() { $users = $this->table('users'); $users->addColumn('name', 'string', array('limit' => 100, 'null' => false)) ->addColumn('username', 'string', array('limit' => 100, 'null' => false ->addColumn('email', 'string', array('limit' => 200, 'null' => false)) ->addColumn('password_hash', 'string', array('limit' => 100, 'null' => ->addIndex(array('username', 'password_hash')) ->save(); }
  • 10.
    ROLLBACK CODE: DOWN publicfunction down() { $this->dropTable('users'); }
  • 11.
    TABLE API Powerful tableAPI lets you create migrations in a relatively database agnostic way. Underlying functionality differs per database.
  • 12.
    TABLE API: CAVEATS Notall field types are supported; typically follows ANSI SQL column types id field is automatically created (primary key, auto increment)
  • 13.
    A DIFFERENT PRIMARYKEY $users = $this->table('users', array('id' => 'user_id')); Non autoincrement primary keys $user_profiles = $this->table('user_profiles', array('primary_key' => 'user_id'));
  • 14.
    ABSTRACT ADAPTER All implementeddatabase engines implement these (and more) public function query($sql); public function createTable(Table $table); public function renameTable($tableName, $newName); public function dropTable($tableName); public function hasColumn($tableName, $columnName); public function addColumn(Table $table, Column $column); public function renameColumn($tableName, $columnName, $newColumnName); public function changeColumn($tableName, $columnName, Column $newColumn); public function dropColumn($tableName, $columnName); public function hasIndex($tableName, $columns); public function addIndex(Table $table, Index $index); public function dropIndex($tableName, $columns); public function dropDatabase($name);
  • 15.
    CHANGE Phinx has achange method which, provided it uses create() and update() methods from the table API, it will work out how to either up or down based off the change() code. Requires the change method to be present. Doesn't work with the save() method we have used.
  • 16.