0
PHINX
PAINLESS DATABASE MIGRATIONS WITH PHP
@MICHAELPEACOCK
CTO @ Ground Six
www.michaelpeacock.co.uk
@michaelpeacock
Author: latest book 'Creating development enviro...
DATABASE MIGRATIONS
Easily manage changes to your database schema
Roll back if you need to
Store them in version control, ...
INSTALLATION
COMPOSER.JSON
{
"require": {
"robmorgan/phinx": "*"
}
}
INSTALL
curl -s https://getcomposer.org/installer | p...
INITIALISE YOUR PROJECT
php vendor/bin/phinx init
mkdir migrations
CONFIGURE DATABASE SETTINGS
paths:
migrations: %%PHINX_CONFIG_DIR%%/migrations
environments:
default_migration_table: phin...
CREATE MIGRATION
php vendor/bin/phinx create CreateUsersTable
<?php
use PhinxMigrationAbstractMigration;
class CreateUsersTable extends AbstractMigration
{
//public function change() {...
CREATE A TABLE
public function up()
{
$users = $this->table('users');
$users->addColumn('name', 'string', array('limit' =>...
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 di...
TABLE API: CAVEATS
Not all field types are supported; typically follows ANSI SQL
column types
id field is automatically cr...
A DIFFERENT PRIMARY KEY
$users = $this->table('users', array('id' => 'user_id'));
Non autoincrement primary keys
$user_pro...
ABSTRACT ADAPTER
All implemented database engines implement these (and
more)
public function query($sql);
public function ...
CHANGE
Phinx has a change method which, provided it uses create()
and update() methods from the table API, it will work ou...
MIGRATING
Phinx talk
Phinx talk
Phinx talk
Phinx talk
Phinx talk
Phinx talk
Upcoming SlideShare
Loading in...5
×

Phinx talk

350

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
350
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Phinx talk"

  1. 1. PHINX PAINLESS DATABASE MIGRATIONS WITH PHP
  2. 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. 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. 4. INSTALLATION COMPOSER.JSON { "require": { "robmorgan/phinx": "*" } } INSTALL curl -s https://getcomposer.org/installer | php php composer.phar install
  5. 5. INITIALISE YOUR PROJECT php vendor/bin/phinx init mkdir migrations
  6. 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. 7. CREATE MIGRATION php vendor/bin/phinx create CreateUsersTable
  8. 8. <?php use PhinxMigrationAbstractMigration; class CreateUsersTable extends AbstractMigration { //public function change() { } public function up() {} public function down(){} }
  9. 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. 10. ROLLBACK CODE: DOWN public function down() { $this->dropTable('users'); }
  11. 11. TABLE API Powerful table API lets you create migrations in a relatively database agnostic way. Underlying functionality differs per database.
  12. 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. 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. 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. 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.
  16. 16. MIGRATING
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×