Top Banner
Rob Morgan @_rjm_ #phinx #php #phptour
67

PHP Tour 2016 Phinx Presentation

Apr 11, 2017

Download

Technology

Rob Morgan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PHP Tour 2016 Phinx Presentation

Rob Morgan @_rjm_ #phinx #php #phptour

Page 2: PHP Tour 2016 Phinx Presentation

What is a Database or Schema Migration?

Page 3: PHP Tour 2016 Phinx Presentation

“A schema migration is performed on a database whenever it is necessary to update

or revert that database's schema to some newer or older version. Migrations are

performed programmatically by using a schema migration tool.”

Page 4: PHP Tour 2016 Phinx Presentation

source: https://en.wikipedia.org/wiki/Schema_migration

Page 5: PHP Tour 2016 Phinx Presentation
Page 6: PHP Tour 2016 Phinx Presentation

What is Phinx?

“Phinx is a tool that allows you to migrate your database schema over time”

Page 7: PHP Tour 2016 Phinx Presentation

Rob Morgan

• Creator & Lead Developer of Phinx

• Writing PHP for almost 15 years

• Email: [email protected]

• https://robmorgan.id.au

@_rjm_

Page 8: PHP Tour 2016 Phinx Presentation

asd

Australia

Page 9: PHP Tour 2016 Phinx Presentation
Page 10: PHP Tour 2016 Phinx Presentation

Brief History• Open Sourced in 2012 under the MIT License

• 38 Releases to Date

• 1.2 million Downloads and 115+ Contributors

• Used in CakePHP 3.0.0

• Built on top of Symfony Components (and only 3)

Page 11: PHP Tour 2016 Phinx Presentation

Features• Write database migrations using PHP code

• Migrate up and down

• Seed data after database creation

• Take advantage of SCM features such as branching

• Integrate with any app

• Get going in less than 5 minutes

Page 12: PHP Tour 2016 Phinx Presentation

Supported Databases

Page 13: PHP Tour 2016 Phinx Presentation

Test MatrixMySQL PostgreSQL SQLite SQL Server

PHP 5.4 ❌ ❌ ❌ ❌

PHP 5.5 ❌ ❌ ❌ ❌

PHP 5.6 ❌ ❌ ❌ ❌

PHP 7 ❌ ❌ ❌ ❌

HHVM ❌

Page 14: PHP Tour 2016 Phinx Presentation

Why did I write Phinx?

Page 15: PHP Tour 2016 Phinx Presentation
Page 16: PHP Tour 2016 Phinx Presentation

We started to look for better solutions

Page 17: PHP Tour 2016 Phinx Presentation

Phing with DbDeploy

Page 18: PHP Tour 2016 Phinx Presentation

We developed on Mac & Linux, but deployed on

Windows

Page 19: PHP Tour 2016 Phinx Presentation
Page 20: PHP Tour 2016 Phinx Presentation

Why Should I Use Phinx?

Page 21: PHP Tour 2016 Phinx Presentation

Phinx is Fast

Page 22: PHP Tour 2016 Phinx Presentation

It requires PHP 5.4

Page 23: PHP Tour 2016 Phinx Presentation

Production Ready

Page 24: PHP Tour 2016 Phinx Presentation

How can I install Phinx?

• Pear

• Composer

• Build a Phar package (see Github)

Page 25: PHP Tour 2016 Phinx Presentation

How can I install Phinx?

• Pear

• Composer

• Build a Phar package (see Github)

Page 26: PHP Tour 2016 Phinx Presentation

It’s a command line application (although it does contain a web application)

Page 27: PHP Tour 2016 Phinx Presentation

$ vendor/bin/phinx

Page 28: PHP Tour 2016 Phinx Presentation
Page 29: PHP Tour 2016 Phinx Presentation

Creating a migration

Page 30: PHP Tour 2016 Phinx Presentation

$ vendor/bin/phinx create CreatePostsTable

Page 31: PHP Tour 2016 Phinx Presentation

Phinx by Rob Morgan - https://phinx.org. version 0.5.3

using config file ./phinx.yml using config parser yaml using migration path /Users/robbym/Code/phinx/db/migrations using seed path /Users/robbym/Code/phinx/db/seeds using migration base class Phinx\Migration\AbstractMigration using default template created ./db/migrations/20160309162303_create_posts_table.php

Page 32: PHP Tour 2016 Phinx Presentation

Phinx by Rob Morgan - https://phinx.org. version 0.5.3

using config file ./phinx.yml using config parser yaml using migration path /Users/robbym/Code/phinx/db/migrations using seed path /Users/robbym/Code/phinx/db/seeds using migration base class Phinx\Migration\AbstractMigration using default template created ./db/migrations/20160309162303_create_posts_table.php

Page 33: PHP Tour 2016 Phinx Presentation

Best Practises• Write environment agnostic code

• Version control your migrations

• Enforce default values in the migrations themselves

• Avoid custom SQL if possible

• Use the change method by default

Page 34: PHP Tour 2016 Phinx Presentation

Reversible Migrations

Page 35: PHP Tour 2016 Phinx Presentation
Page 36: PHP Tour 2016 Phinx Presentation
Page 37: PHP Tour 2016 Phinx Presentation

Only some methods can be reversed!

Page 38: PHP Tour 2016 Phinx Presentation

createTable

renameTable

addColumn

renameColumn

addIndex

addForeignIndex

Page 39: PHP Tour 2016 Phinx Presentation

Seed Command

Page 40: PHP Tour 2016 Phinx Presentation

$ vendor/bin/phinx seed:create UserSeeder $ vendor/bin/phinx seed:run -e development

Page 41: PHP Tour 2016 Phinx Presentation
Page 42: PHP Tour 2016 Phinx Presentation

Real World Use Case

Page 43: PHP Tour 2016 Phinx Presentation

Let’s pretend our Customer wants us to build a new

web application

Page 44: PHP Tour 2016 Phinx Presentation

The Customer

• He wants a competitor to Wordpress

• Needs it tomorrow

• Willing to pay alot some money

Page 45: PHP Tour 2016 Phinx Presentation
Page 46: PHP Tour 2016 Phinx Presentation

Phinx can help!(but not do everything)

Page 47: PHP Tour 2016 Phinx Presentation

But first we must install it…

Page 48: PHP Tour 2016 Phinx Presentation

Installing via Composer

Page 49: PHP Tour 2016 Phinx Presentation

# first require Phinx $ php composer.phar require robmorgan/phinx

# then install it including the dependencies $ php composer.phar install

Page 50: PHP Tour 2016 Phinx Presentation
Page 51: PHP Tour 2016 Phinx Presentation

Creating a Migration

Page 52: PHP Tour 2016 Phinx Presentation
Page 53: PHP Tour 2016 Phinx Presentation
Page 54: PHP Tour 2016 Phinx Presentation

What’s coming in Phinx 0.6.0?

Page 55: PHP Tour 2016 Phinx Presentation

Docker Support?

Page 56: PHP Tour 2016 Phinx Presentation

# setup your project $ docker run --rm -v $(pwd):/app robmorgan/phinx init

# create a migration $ docker run --rm -v $(pwd):/app robmorgan/phinx create MyFirstMigration

# migrate your db $ docker run --rm -v $(pwd):/app --link db:db robmorgan/phinx migrate -e development

Page 57: PHP Tour 2016 Phinx Presentation

Lightweight ORM

Page 58: PHP Tour 2016 Phinx Presentation

API Freeze towards 1.0

Page 59: PHP Tour 2016 Phinx Presentation

What about the future?

Page 60: PHP Tour 2016 Phinx Presentation

Multiple Database Support

Page 61: PHP Tour 2016 Phinx Presentation

Migration Generator

Page 62: PHP Tour 2016 Phinx Presentation

Data Transformation

Page 63: PHP Tour 2016 Phinx Presentation

Documentation

Page 64: PHP Tour 2016 Phinx Presentation
Page 65: PHP Tour 2016 Phinx Presentation

Please Read before opening an issue!

Page 66: PHP Tour 2016 Phinx Presentation

Contributing• Before you open an issue, search the existing

ones!

• Fixing the documentation is a great place to start

• Read the CONTRIBUTING.md file on Github

• At the end of the day, nobody bites and its only PHP!

Page 67: PHP Tour 2016 Phinx Presentation

Cheers!• Rob Morgan (@_rjm_)

• https://phinx.org

• https://robmorgan.id.au

Rob Morgan @_rjm_ #phinx #php #phptour