Phinx talk
Upcoming SlideShare
Loading in...5
×
 

Phinx talk

on

  • 244 views

 

Statistics

Views

Total Views
244
Views on SlideShare
241
Embed Views
3

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 3

https://twitter.com 3

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Phinx talk Phinx talk Presentation Transcript

  • 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