Slide 1

Slide 1 text

PHP Migrations with Phinx Andy Gale

Slide 2

Slide 2 text

Andy Gale @andygale on Twitter Bristol Rovers fan #UTG Bristol DevOps Organiser What do you do? Owner and DevOps Consultant at Hello Future

Slide 3

Slide 3 text

Hello Future • @hellofutur3 on Twitter • DevOps, Continuous Delivery, Chef and cloud automation consultancy • Web application development What do we do?

Slide 4

Slide 4 text

Migrations • Changes to databases • Store them in your SCM system (Git, SVN, Mercurial, CVS…) • Handy for Continuous Integration, Continuous Delivery and Continuous Deployment What do you mean by that?

Slide 5

Slide 5 text

Phinx • PHP framework independent • Simple to install (composer!) • Easy to use • Difficult to spell Handy PHP database migration tool

Slide 6

Slide 6 text

Phinx • Portable amongst different database vendors (MySQL, PostgreSQL, SQLite, SQL Server) • Good for existing projects with a custom framework where no existing migration tools exist • Also good for new projects Handy PHP database migration tool

Slide 7

Slide 7 text

Phinx Handy PHP database migration tool Get up and running $ php composer.phar require robmorgan/phinx $ php composer.phar install --no-dev $ mkdir migrations $ php vendor/bin/phinx init

Slide 8

Slide 8 text

Phinx Handy PHP database migration tool Configuration in a YAML file phinx.yml Store this outside the document root

Slide 9

Slide 9 text

Phinx Handy PHP database migration tool Create a migration $ phinx create MyNewMigration

Slide 10

Slide 10 text

Phinx

Slide 11

Slide 11 text

Phinx /** * Migrate Up. */ public function up() { $users = $this->table('users'); $users->addColumn('username', ‘string’, array('limit' => 20)) ->addColumn('password', ‘string’, array('limit' => 40)) ->addColumn('salt', 'string', array('limit' => 40)) ->addColumn('email', ‘string’, array('limit' => 100)) ->addIndex(array('username', ‘email’), array('unique' => true)) ->save(); }

Slide 12

Slide 12 text

Phinx /** * Migrate Down. */ public function down() { $this->dropTable('users'); } Obviously you should consider if this is a good idea!

Slide 13

Slide 13 text

Phinx /** * Change. */ public function change() { $users = $this->table('users'); $users->addColumn('username', ‘string’, array('limit' => 20)) ->addColumn('password', ‘string’, array('limit' => 40)) ->addColumn('salt', 'string', array('limit' => 40)) ->addColumn('email', ‘string’, array('limit' => 100)) ->addIndex(array('username', ‘email’), array('unique' => true)) ->create(); } Change will automatically create the down migration for you, again this may not be what you want!!

Slide 14

Slide 14 text

Phinx /** * Migrate Up. */ table('users'); $table->renameColumn('biography', 'bio'); } Rename a column

Slide 15

Slide 15 text

Phinx table('users'); $table->rename('legacy_users'); } public function down() { $table = $this->table('legacy_users'); $table->rename('users'); } Rename a table

Slide 16

Slide 16 text

Phinx table('tags'); $table->addColumn('name', 'string', array('limit' => 30)) ->update(); } Alter limit

Slide 17

Slide 17 text

Phinx execute('DELETE FROM users'); } Some SQL

Slide 18

Slide 18 text

Phinx Handy PHP database migration tool Run migrations $ phinx migrate Rollback migrations $ phinx rollback Get status of which migrations have been run $ phinx status

Slide 19

Slide 19 text

Phinx Handy PHP database migration tool Funky web app $ php -S localhost:8000 vendor/robmorgan/ phinx/app/web.php

Slide 20

Slide 20 text

Phinx • Use on existing projects • Good for CD with projects as you can do migrations as part of your deployment scripts (think about the order you do things) • Simple to use, simple solution to most problems Recap