Phinx talk

22
PHINX PAINLESS DATABASE MIGRATIONS WITH PHP

Transcript of Phinx talk

Page 1: Phinx talk

PHINX

PAINLESS DATABASE MIGRATIONS WITH PHP

Page 2: Phinx talk

@MICHAELPEACOCKCTO @ 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

Page 3: Phinx talk

DATABASE MIGRATIONSEasily manage changes to your database schemaRoll back if you need toStore them in version control, share with colleagues

Page 4: Phinx talk

INSTALLATION

COMPOSER.JSON{ "require": { "robmorgan/phinx": "*" }}

INSTALLcurl -s https://getcomposer.org/installer | phpphp composer.phar install

Page 5: Phinx talk

INITIALISE YOUR PROJECTphp vendor/bin/phinx initmkdir migrations

Page 6: Phinx talk

CONFIGURE DATABASE SETTINGSpaths: 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

Page 7: Phinx talk

CREATE MIGRATIONphp vendor/bin/phinx create CreateUsersTable

Page 8: Phinx talk

<?php

use Phinx\Migration\AbstractMigration;

class CreateUsersTable extends AbstractMigration{ //public function change() { } public function up() {}

public function down(){}}

Page 9: Phinx talk

CREATE A TABLEpublic 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(); }

Page 10: Phinx talk

ROLLBACK CODE: DOWNpublic function down() { $this->dropTable('users'); }

Page 11: Phinx talk

TABLE APIPowerful table API lets you create migrations in a relativelydatabase agnostic way. Underlying functionality differs per

database.

Page 12: Phinx talk

TABLE API: CAVEATSNot all field types are supported; typically follows ANSI SQLcolumn typesid field is automatically created (primary key, autoincrement)

Page 13: Phinx talk

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'));

Page 14: Phinx talk

ABSTRACT ADAPTERAll 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);

Page 15: Phinx talk

CHANGEPhinx has a change method which, provided it uses create()

and update() methods from the table API, it will work out howto 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.

Page 16: Phinx talk

MIGRATING

Page 17: Phinx talk
Page 18: Phinx talk
Page 19: Phinx talk
Page 20: Phinx talk
Page 21: Phinx talk
Page 22: Phinx talk