Phinx talk
-
Upload
michael-peacock -
Category
Technology
-
view
335 -
download
0
Transcript of Phinx talk
PHINX
PAINLESS DATABASE MIGRATIONS WITH PHP
@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
DATABASE MIGRATIONSEasily manage changes to your database schemaRoll back if you need toStore them in version control, share with colleagues
INSTALLATION
COMPOSER.JSON{ "require": { "robmorgan/phinx": "*" }}
INSTALLcurl -s https://getcomposer.org/installer | phpphp composer.phar install
INITIALISE YOUR PROJECTphp vendor/bin/phinx initmkdir migrations
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
CREATE MIGRATIONphp vendor/bin/phinx create CreateUsersTable
<?php
use Phinx\Migration\AbstractMigration;
class CreateUsersTable extends AbstractMigration{ //public function change() { } public function up() {}
public function down(){}}
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(); }
ROLLBACK CODE: DOWNpublic function down() { $this->dropTable('users'); }
TABLE APIPowerful table API lets you create migrations in a relativelydatabase agnostic way. Underlying functionality differs per
database.
TABLE API: CAVEATSNot all field types are supported; typically follows ANSI SQLcolumn typesid field is automatically created (primary key, autoincrement)
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 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);
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.
MIGRATING