SlideShare uma empresa Scribd logo
1 de 19
Baixar para ler offline
Schema Migration
(DB migration)
with Phinx
Hadi Ariawan
@hadiariawan
github.com/hadiariawan
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.
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?
● …..
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.”
http://www.brunton-spall.co.uk/post/2014/05/06/database-migrations-done-right/
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.
Why Phinx?
https://phinx.org/
● 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.
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.
Installation
Installation is easy. Use composer!
php composer.phar require robmorgan/phinx
execute this after installation finished.
vendor/bin/phinx init
http://docs.phinx.org/en/latest/install.html
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.
Create a migration file
$ php vendor/bin/phinx create BrandTable
Checking migration status
Data types? Indexes? Foreign keys?
Check their documentation on this matters.
https://book.cakephp.org/3.0/en/phinx/migrations.html#working-with-columns
They are pretty straightforward and easy to follow.
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?
How to create and run a seeder
Create a seeder
$ phinx seed:create MyNewSeeder
Run a seeder
$ phinx seed:run -e development -s MyNewSeeder
Seeder Example
Here is an example of seeder.
A seeder for super user account.
This one records will be inserted in users
table.
How to generate random data in your seeder?
Use Faker! (https://github.com/fzaninotto/Faker)
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
https://book.cakephp.org/3.0/en/phinx/migrations.html#fetching-rows
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
https://bakery.cakephp.org/2017/06/23/welcoming-phinx-to-the-cakephp-family.html
That’s All!
Now you know how, what and why you should use schema migration, right?
“Get your database under control”
Hadi Ariawan
@hadiariawan
github.com/hadiariawan

Mais conteúdo relacionado

Mais procurados

Mais procurados (11)

Enterprise-Scale WordPress
Enterprise-Scale WordPressEnterprise-Scale WordPress
Enterprise-Scale WordPress
 
Spring Batch Introduction (and Bitbucket Project)
Spring Batch Introduction (and Bitbucket Project)Spring Batch Introduction (and Bitbucket Project)
Spring Batch Introduction (and Bitbucket Project)
 
Flux and React.js
Flux and React.jsFlux and React.js
Flux and React.js
 
Introduce flux & react in practice
Introduce flux & react in practiceIntroduce flux & react in practice
Introduce flux & react in practice
 
Reactive Web Development with Spring Boot 2
Reactive Web Development with Spring Boot 2Reactive Web Development with Spring Boot 2
Reactive Web Development with Spring Boot 2
 
Breaking data
Breaking dataBreaking data
Breaking data
 
Database Source Control: Migrations vs State
Database Source Control: Migrations vs StateDatabase Source Control: Migrations vs State
Database Source Control: Migrations vs State
 
GraphQL vs. (the) REST
GraphQL vs. (the) RESTGraphQL vs. (the) REST
GraphQL vs. (the) REST
 
Sitecore Data Exchange Framework
Sitecore Data Exchange FrameworkSitecore Data Exchange Framework
Sitecore Data Exchange Framework
 
DDD Domain Relationships Radars
DDD Domain Relationships RadarsDDD Domain Relationships Radars
DDD Domain Relationships Radars
 
Moving from application automation to true DevOps by including the database
Moving from application automation to true DevOps by including the databaseMoving from application automation to true DevOps by including the database
Moving from application automation to true DevOps by including the database
 

Semelhante a Schema migration (DB migration) with Phinx

PHP North-East - Automated Deployment
PHP North-East - Automated DeploymentPHP North-East - Automated Deployment
PHP North-East - Automated Deployment
Michael Peacock
 
Datasheet scriptspluginforrd
Datasheet scriptspluginforrdDatasheet scriptspluginforrd
Datasheet scriptspluginforrd
MidVision
 
Datasheet was pluginforrd
Datasheet was pluginforrdDatasheet was pluginforrd
Datasheet was pluginforrd
MidVision
 
Datasheet foldermanagementpluginforrd
Datasheet foldermanagementpluginforrdDatasheet foldermanagementpluginforrd
Datasheet foldermanagementpluginforrd
MidVision
 
APACHE
APACHEAPACHE
APACHE
ARJUN
 
Db2 migration -_tips,_tricks,_and_pitfalls
Db2 migration -_tips,_tricks,_and_pitfallsDb2 migration -_tips,_tricks,_and_pitfalls
Db2 migration -_tips,_tricks,_and_pitfalls
sam2sung2
 

Semelhante a Schema migration (DB migration) with Phinx (20)

PHP North-East - Automated Deployment
PHP North-East - Automated DeploymentPHP North-East - Automated Deployment
PHP North-East - Automated Deployment
 
Automated Deployment
Automated DeploymentAutomated Deployment
Automated Deployment
 
Handling Database Deployments
Handling Database DeploymentsHandling Database Deployments
Handling Database Deployments
 
Delivering changes for applications and databases
Delivering changes for applications and databasesDelivering changes for applications and databases
Delivering changes for applications and databases
 
Building and deploying LLM applications with Apache Airflow
Building and deploying LLM applications with Apache AirflowBuilding and deploying LLM applications with Apache Airflow
Building and deploying LLM applications with Apache Airflow
 
Datasheet scriptspluginforrd
Datasheet scriptspluginforrdDatasheet scriptspluginforrd
Datasheet scriptspluginforrd
 
resume
resumeresume
resume
 
My Saminar On Php
My Saminar On PhpMy Saminar On Php
My Saminar On Php
 
Datasheet was pluginforrd
Datasheet was pluginforrdDatasheet was pluginforrd
Datasheet was pluginforrd
 
Extension Library - Viagra for XPages
Extension Library - Viagra for XPagesExtension Library - Viagra for XPages
Extension Library - Viagra for XPages
 
[DanNotes] XPages - Beyound the Basics
[DanNotes] XPages - Beyound the Basics[DanNotes] XPages - Beyound the Basics
[DanNotes] XPages - Beyound the Basics
 
Achieving Full Stack DevOps at Colonial Life
Achieving Full Stack DevOps at Colonial Life Achieving Full Stack DevOps at Colonial Life
Achieving Full Stack DevOps at Colonial Life
 
Dynamics of Leading Legacy Databases
Dynamics of Leading Legacy DatabasesDynamics of Leading Legacy Databases
Dynamics of Leading Legacy Databases
 
Datasheet foldermanagementpluginforrd
Datasheet foldermanagementpluginforrdDatasheet foldermanagementpluginforrd
Datasheet foldermanagementpluginforrd
 
APACHE
APACHEAPACHE
APACHE
 
O365 Developer Bootcamp NJ 2018 - Material
O365 Developer Bootcamp NJ 2018 - MaterialO365 Developer Bootcamp NJ 2018 - Material
O365 Developer Bootcamp NJ 2018 - Material
 
Db2 migration -_tips,_tricks,_and_pitfalls
Db2 migration -_tips,_tricks,_and_pitfallsDb2 migration -_tips,_tricks,_and_pitfalls
Db2 migration -_tips,_tricks,_and_pitfalls
 
BW Migration to HANA Part 2 - SUM DMO Tool for SAP Upgrade & Migration
BW Migration to HANA Part 2 - SUM DMO Tool for SAP Upgrade & MigrationBW Migration to HANA Part 2 - SUM DMO Tool for SAP Upgrade & Migration
BW Migration to HANA Part 2 - SUM DMO Tool for SAP Upgrade & Migration
 
FlywayDB Migration with Spring Boot
FlywayDB Migration with Spring BootFlywayDB Migration with Spring Boot
FlywayDB Migration with Spring Boot
 
XPages -Beyond the Basics
XPages -Beyond the BasicsXPages -Beyond the Basics
XPages -Beyond the Basics
 

Último

Último (20)

Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Schema migration (DB migration) with Phinx

  • 1. Schema Migration (DB migration) with Phinx Hadi Ariawan @hadiariawan github.com/hadiariawan
  • 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. 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. 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.” http://www.brunton-spall.co.uk/post/2014/05/06/database-migrations-done-right/
  • 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. Why Phinx? https://phinx.org/ ● 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. 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. Installation Installation is easy. Use composer! php composer.phar require robmorgan/phinx execute this after installation finished. vendor/bin/phinx init http://docs.phinx.org/en/latest/install.html
  • 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. Create a migration file $ php vendor/bin/phinx create BrandTable
  • 12. Data types? Indexes? Foreign keys? Check their documentation on this matters. https://book.cakephp.org/3.0/en/phinx/migrations.html#working-with-columns They are pretty straightforward and easy to follow.
  • 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. 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. Seeder Example Here is an example of seeder. A seeder for super user account. This one records will be inserted in users table.
  • 16. How to generate random data in your seeder? Use Faker! (https://github.com/fzaninotto/Faker)
  • 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 https://book.cakephp.org/3.0/en/phinx/migrations.html#fetching-rows
  • 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 https://bakery.cakephp.org/2017/06/23/welcoming-phinx-to-the-cakephp-family.html
  • 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 github.com/hadiariawan