PHP Tour 2016 Phinx Presentation

RobMorgan14 1,070 views 67 slides May 24, 2016
Slide 1
Slide 1 of 67
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67

About This Presentation

Phinx is one of the most popular standalone database migration tools. Learn the basic concepts of Phinx with its creator Rob Morgan. This presentation covers adding it to a project, creating new migrations and then executing them, . You are also exposed to more advanced features of Phinx and everyda...


Slide Content

Rob Morgan
@_rjm_
#phinx #php #phptour

What is a Database or
Schema Migration?

“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.”

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

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

Rob Morgan
•Creator & Lead Developer of Phinx
•Writing PHP for almost 15 years
•Email: [email protected]
•https://robmorgan.id.au
@_rjm_

asd
Australia

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)

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

Supported Databases

Test Matrix
MySQL PostgreSQL SQLite SQL Server
PHP 5.4 ❌ ❌ ❌ ❌
PHP 5.5 ❌ ❌ ❌ ❌
PHP 5.6 ❌ ❌ ❌ ❌
PHP 7 ❌ ❌ ❌ ❌
HHVM ❌

Why did I write Phinx?

We started to look for
better solutions

Phing with DbDeploy

We developed on Mac &
Linux, but deployed on
Windows

Why Should I Use
Phinx?

Phinx is Fast

It requires PHP 5.4

Production Ready

How can I install Phinx?
•Pear
•Composer
•Build a Phar package (see Github)

How can I install Phinx?
•Pear
•Composer
•Build a Phar package (see Github)

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

$ vendor/bin/phinx

Creating a migration

$ vendor/bin/phinx create CreatePostsTable

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

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

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

Reversible Migrations

Only some methods
can be reversed!

createTable
renameTable
addColumn
renameColumn
addIndex
addForeignIndex

Seed Command

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

Real World Use Case

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

The Customer
•He wants a competitor to Wordpress
•Needs it tomorrow
•Willing to pay alot some money

Phinx can help!

(but not do everything)

But first we must
install it…

Installing via
Composer

# first require Phinx
$ php composer.phar require robmorgan/phinx
# then install it including the dependencies
$ php composer.phar install

Creating a Migration

What’s coming in
Phinx 0.6.0?

Docker Support?

# 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

Lightweight ORM

API Freeze towards
1.0

What about the future?

Multiple Database
Support

Migration Generator

Data Transformation

Documentation

Please Read before
opening an issue!

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!

Cheers!
•Rob Morgan (@_rjm_)
•https://phinx.org
•https://robmorgan.id.au
Rob Morgan
@_rjm_
#phinx #php #phptour