SlideShare a Scribd company logo
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

More Related Content

PPTX
An introduction to Laravel Passport
KEY
Phpne august-2012-symfony-components-friends
PPT
Dance for the puppet master: G6 Tech Talk
PDF
Codeigniter : Two Step View - Concept Implementation
PPT
Powerful and flexible templates with Twig
PDF
Codeigniter : Using Third Party Components - Zend Framework Components
PDF
What happens in laravel 4 bootstraping
PDF
Getting Started-with-Laravel
An introduction to Laravel Passport
Phpne august-2012-symfony-components-friends
Dance for the puppet master: G6 Tech Talk
Codeigniter : Two Step View - Concept Implementation
Powerful and flexible templates with Twig
Codeigniter : Using Third Party Components - Zend Framework Components
What happens in laravel 4 bootstraping
Getting Started-with-Laravel

What's hot (20)

PDF
Extending the WordPress REST API - Josh Pollock
PDF
Bootstrat REST APIs with Laravel 5
KEY
CodeIgniter 3.0
PPTX
Dealing with Continuous Data Processing, ConFoo 2012
PDF
Getting out of Callback Hell in PHP
PDF
Using the new WordPress REST API
PDF
Inside Bokete: Web Application with Mojolicious and others
PDF
Developing apps using Perl
PDF
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
PDF
Keeping it Small: Getting to know the Slim Micro Framework
PDF
RESTful web services
PPTX
Webrtc mojo
PPTX
REST APIs in Laravel 101
PPT
Slim RedBeanPHP and Knockout
PDF
Add loop shortcode
PDF
Keeping it small - Getting to know the Slim PHP micro framework
PDF
Mojolicious
PPTX
16.mysql stored procedures in laravel
PDF
Perl web frameworks
KEY
Mojo as a_client
Extending the WordPress REST API - Josh Pollock
Bootstrat REST APIs with Laravel 5
CodeIgniter 3.0
Dealing with Continuous Data Processing, ConFoo 2012
Getting out of Callback Hell in PHP
Using the new WordPress REST API
Inside Bokete: Web Application with Mojolicious and others
Developing apps using Perl
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
Keeping it Small: Getting to know the Slim Micro Framework
RESTful web services
Webrtc mojo
REST APIs in Laravel 101
Slim RedBeanPHP and Knockout
Add loop shortcode
Keeping it small - Getting to know the Slim PHP micro framework
Mojolicious
16.mysql stored procedures in laravel
Perl web frameworks
Mojo as a_client
Ad

Similar to Phinx talk (20)

PDF
Patterns and Tools for Database Versioning, Migration, Data Loading and Test ...
ODP
Exploring Symfony's Code
PPTX
Laravel for Web Artisans
PDF
Flask and Angular: An approach to build robust platforms
PDF
Php summary
PPTX
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
PDF
Web applications with Catalyst
PPTX
Vitta Minicurso Laravel - Hackathon League of Legends
ODP
Zend Framework 1.9 Setup & Using Zend_Tool
PDF
Getting to know Laravel 5
PDF
What's New In Laravel 5
KEY
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
PDF
PHP and Rich Internet Applications
KEY
PPT
Php classes in mumbai
ODP
Codegnitorppt
PDF
Burn down the silos! Helping dev and ops gel on high availability websites
PDF
Doctrine and NoSQL
PPTX
CHAPTER six DataBase Driven Websites.pptx
Patterns and Tools for Database Versioning, Migration, Data Loading and Test ...
Exploring Symfony's Code
Laravel for Web Artisans
Flask and Angular: An approach to build robust platforms
Php summary
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
Web applications with Catalyst
Vitta Minicurso Laravel - Hackathon League of Legends
Zend Framework 1.9 Setup & Using Zend_Tool
Getting to know Laravel 5
What's New In Laravel 5
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
PHP and Rich Internet Applications
Php classes in mumbai
Codegnitorppt
Burn down the silos! Helping dev and ops gel on high availability websites
Doctrine and NoSQL
CHAPTER six DataBase Driven Websites.pptx
Ad

More from Michael Peacock (20)

PPTX
Immutable Infrastructure with Packer Ansible and Terraform
PPTX
Test driven APIs with Laravel
PPTX
Symfony Workflow Component - Introductory Lightning Talk
PPTX
Alexa, lets make a skill
PPTX
API Development with Laravel
PDF
Refactoring to symfony components
PPT
Introduction to OOP with PHP
KEY
KEY
Evolution of a big data project
PPTX
Real time voice call integration - Confoo 2012
PPTX
Data at Scale - Michael Peacock, Cloud Connect 2012
PPTX
Supermondays twilio
PPTX
PHP & Twilio
PPTX
PHP Continuous Data Processing
PPTX
PHP North East Registry Pattern
PPTX
PHP North East - Registry Design Pattern
PPTX
Supermondays: Jenkins CI lightning talk
PPTX
Corporate Structures - September 2010
PPTX
PHP North-East - Automated Deployment
PPTX
Abstracting functionality with centralised content
Immutable Infrastructure with Packer Ansible and Terraform
Test driven APIs with Laravel
Symfony Workflow Component - Introductory Lightning Talk
Alexa, lets make a skill
API Development with Laravel
Refactoring to symfony components
Introduction to OOP with PHP
Evolution of a big data project
Real time voice call integration - Confoo 2012
Data at Scale - Michael Peacock, Cloud Connect 2012
Supermondays twilio
PHP & Twilio
PHP Continuous Data Processing
PHP North East Registry Pattern
PHP North East - Registry Design Pattern
Supermondays: Jenkins CI lightning talk
Corporate Structures - September 2010
PHP North-East - Automated Deployment
Abstracting functionality with centralised content

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Getting Started with Data Integration: FME Form 101
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Big Data Technologies - Introduction.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Machine Learning_overview_presentation.pptx
PPTX
Tartificialntelligence_presentation.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
A Presentation on Artificial Intelligence
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
1. Introduction to Computer Programming.pptx
PDF
Approach and Philosophy of On baking technology
PDF
Spectral efficient network and resource selection model in 5G networks
Machine learning based COVID-19 study performance prediction
The Rise and Fall of 3GPP – Time for a Sabbatical?
Getting Started with Data Integration: FME Form 101
Digital-Transformation-Roadmap-for-Companies.pptx
MYSQL Presentation for SQL database connectivity
Big Data Technologies - Introduction.pptx
Unlocking AI with Model Context Protocol (MCP)
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine Learning_overview_presentation.pptx
Tartificialntelligence_presentation.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Group 1 Presentation -Planning and Decision Making .pptx
A comparative analysis of optical character recognition models for extracting...
Diabetes mellitus diagnosis method based random forest with bat algorithm
A Presentation on Artificial Intelligence
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
1. Introduction to Computer Programming.pptx
Approach and Philosophy of On baking technology
Spectral efficient network and resource selection model in 5G networks

Phinx talk

  • 2. @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
  • 3. DATABASE MIGRATIONS Easily manage changes 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 php vendor/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. CREATE MIGRATION php vendor/bin/phinx create CreateUsersTable
  • 8. <?php use PhinxMigrationAbstractMigration; class CreateUsersTable extends AbstractMigration { //public function change() { } public function up() {} public function down(){} }
  • 9. 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(); }
  • 10. ROLLBACK CODE: DOWN public function down() { $this->dropTable('users'); }
  • 11. TABLE API Powerful table API lets you create migrations in a relatively database agnostic way. Underlying functionality differs per database.
  • 12. TABLE API: CAVEATS Not all field types are supported; typically follows ANSI SQL column types id field is automatically created (primary key, auto increment)
  • 13. 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'));
  • 14. 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);
  • 15. 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.