Phinx talk

  • 202 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
202
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
1
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. PHINX PAINLESS DATABASE MIGRATIONS WITH PHP
  • 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. DATABASE MIGRATIONS Easily manage changes to your database schema Roll back if you need to Store them in version control, share with colleagues
  • 4. INSTALLATION COMPOSER.JSON { "require": { "robmorgan/phinx": "*" } } INSTALL curl -s https://getcomposer.org/installer | php php composer.phar install
  • 5. INITIALISE YOUR PROJECT php vendor/bin/phinx init mkdir migrations
  • 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. CREATE MIGRATION php vendor/bin/phinx create CreateUsersTable
  • 8. <?php use PhinxMigrationAbstractMigration; class CreateUsersTable extends AbstractMigration { //public function change() { } public function up() {} public function down(){} }
  • 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. ROLLBACK CODE: DOWN public function down() { $this->dropTable('users'); }
  • 11. TABLE API Powerful table API lets you create migrations in a relatively database agnostic way. Underlying functionality differs per database.
  • 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. 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. 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. 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. MIGRATING