Phinx talk
-
Upload
michael-peacock -
Category
Technology
-
view
336 -
download
0
Transcript of Phinx talk
![Page 1: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/1.jpg)
PHINX
PAINLESS DATABASE MIGRATIONS WITH PHP
![Page 2: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/2.jpg)
@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](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/3.jpg)
DATABASE MIGRATIONSEasily manage changes to your database schemaRoll back if you need toStore them in version control, share with colleagues
![Page 4: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/4.jpg)
INSTALLATION
COMPOSER.JSON{ "require": { "robmorgan/phinx": "*" }}
INSTALLcurl -s https://getcomposer.org/installer | phpphp composer.phar install
![Page 5: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/5.jpg)
INITIALISE YOUR PROJECTphp vendor/bin/phinx initmkdir migrations
![Page 6: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/6.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/7.jpg)
CREATE MIGRATIONphp vendor/bin/phinx create CreateUsersTable
![Page 8: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/8.jpg)
<?php
use Phinx\Migration\AbstractMigration;
class CreateUsersTable extends AbstractMigration{ //public function change() { } public function up() {}
public function down(){}}
![Page 9: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/10.jpg)
ROLLBACK CODE: DOWNpublic function down() { $this->dropTable('users'); }
![Page 11: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/11.jpg)
TABLE APIPowerful table API lets you create migrations in a relativelydatabase agnostic way. Underlying functionality differs per
database.
![Page 12: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/12.jpg)
TABLE API: CAVEATSNot all field types are supported; typically follows ANSI SQLcolumn typesid field is automatically created (primary key, autoincrement)
![Page 13: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/14.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/15.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/16.jpg)
MIGRATING
![Page 17: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/17.jpg)
![Page 18: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/18.jpg)
![Page 19: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/19.jpg)
![Page 20: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/20.jpg)
![Page 21: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/21.jpg)
![Page 22: Phinx talk](https://reader035.fdocuments.net/reader035/viewer/2022081401/5589750ad8b42a8f6d8b458e/html5/thumbnails/22.jpg)