Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Schema migration (DB migration) with Phinx


Published on

Schema migration (DB migration) with Phinx.
What is schema migration? Why you should use schema migration? How you do schema migration using phinx, written on PHP.

Published in: Technology
  • Be the first to comment

Schema migration (DB migration) with Phinx

  1. 1. Schema Migration (DB migration) with Phinx Hadi Ariawan @hadiariawan
  2. 2. What is Schema migration? In software engineering, schema migration (also database migration, database change management) refers to the management of incremental, reversible changes to relational database schemas. 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.
  3. 3. Have you ever face these situations? ● Developer A add several columns on table X, developer A commits and push the codes. Developer B pulls the code and then the app breaks. ● Developer finishes the codes and forgot to give/email the sql alter script to DevOps/Sysadmin guy. When DevOps deploy to production the app breaks. ● DevOps guy deploying more than 1 DB instances. Can you imagine how long it took to apply DB changes if the instances are (for example) 10? ● …..
  4. 4. Why using Schema migration? “The rule is simple. You should never tie database migrations to application deploys or vice versa. By minimising dependencies you enable faster, easier and cleaner deployments.”
  5. 5. Benefits of using Schema Migration ● Separated application and database deployment. ● Ensured same DB structure through all the team member. ● Database versioning (keep track of DB structure changes). ● Easier to maintain incremental database changes.
  6. 6. Why Phinx? ● Be portable amongst the most popular database vendors. ● Be PHP framework independent. ● Have a simple install process. ● Have an easy to use command-line operation. ● Integrate with various other PHP tools (Phing, PHPUnit) and web frameworks.
  7. 7. Why i choose phinx ● Familiar. Because it is written in PHP. ● Command line app. Easy to connect phinx to deployer tool. ● Supports most popular databases (MySQL, PostgreSQL, etc). ● Works independently/standalone. Multi platform and frameworks. ● Helpful for unit testing. ● Their documentation is easy to follow, easy to understand.
  8. 8. Installation Installation is easy. Use composer! php composer.phar require robmorgan/phinx execute this after installation finished. vendor/bin/phinx init
  9. 9. One thing Before Migrating There should be a file named phinx.yml after you are done initialized your project. Phinx.yml file contains your environments and database configurations.
  10. 10. Create a migration file $ php vendor/bin/phinx create BrandTable
  11. 11. Checking migration status
  12. 12. Data types? Indexes? Foreign keys? Check their documentation on this matters. They are pretty straightforward and easy to follow.
  13. 13. Seeder ● Populate reference data ○ Rarely change data ○ Eg: Country data, User type data, etc ● Populate test data ○ Would you run sql command on PhpMyAdmin 100, 1000, 10000 times?
  14. 14. How to create and run a seeder Create a seeder $ phinx seed:create MyNewSeeder Run a seeder $ phinx seed:run -e development -s MyNewSeeder
  15. 15. Seeder Example Here is an example of seeder. A seeder for super user account. This one records will be inserted in users table.
  16. 16. How to generate random data in your seeder? Use Faker! (
  17. 17. How to access DB in your seeder? Often times you encounter cases that need current data in database in order to generate other data. This is how you it on phinx. Use these 2 methods ● fetchRow() ○ For single row ● fetchAll() ○ For multiple rows
  18. 18. Tips on using Phinx ● Read the documentation thoroughly. ● Write migration wisely. Only some method can be reversed. ● Store it in version control (git) and share it with your team/colleague. Phinx joined CakePHP family on June 2017
  19. 19. That’s All! Now you know how, what and why you should use schema migration, right? “Get your database under control” Hadi Ariawan @hadiariawan