SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Enterprise-Scale WordPress
Enterprise-Scale WordPressEnterprise-Scale WordPress
Enterprise-Scale WordPressKeanan Koppenhaver
 
Spring Batch Introduction (and Bitbucket Project)
Spring Batch Introduction (and Bitbucket Project)Spring Batch Introduction (and Bitbucket Project)
Spring Batch Introduction (and Bitbucket Project)Guillermo Daniel Salazar
 
Flux and React.js
Flux and React.jsFlux and React.js
Flux and React.jssara stanford
 
Introduce flux & react in practice
Introduce flux & react in practiceIntroduce flux & react in practice
Introduce flux & react in practiceHsuan Fu Lien
 
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 2Mike Melusky
 
Breaking data
Breaking dataBreaking data
Breaking dataTerry Bunio
 
Database Source Control: Migrations vs State
Database Source Control: Migrations vs StateDatabase Source Control: Migrations vs State
Database Source Control: Migrations vs StateEduardo Piairo
 
GraphQL vs. (the) REST
GraphQL vs. (the) RESTGraphQL vs. (the) REST
GraphQL vs. (the) RESTcoliquio GmbH
 
Sitecore Data Exchange Framework
Sitecore Data Exchange FrameworkSitecore Data Exchange Framework
Sitecore Data Exchange FrameworkRadek Kozłowski
 
DDD Domain Relationships Radars
DDD Domain Relationships RadarsDDD Domain Relationships Radars
DDD Domain Relationships RadarsPhilippe Bourgau
 
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 databaseRed Gate Software
 

Was ist angesagt? (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
 

Ă„hnlich wie Schema migration (DB migration) with Phinx

PHP North-East - Automated Deployment
PHP North-East - Automated DeploymentPHP North-East - Automated Deployment
PHP North-East - Automated DeploymentMichael Peacock
 
Automated Deployment
Automated DeploymentAutomated Deployment
Automated Deploymentphpne
 
Handling Database Deployments
Handling Database DeploymentsHandling Database Deployments
Handling Database DeploymentsMike Willbanks
 
Delivering changes for applications and databases
Delivering changes for applications and databasesDelivering changes for applications and databases
Delivering changes for applications and databasesEduardo Piairo
 
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 AirflowKaxil Naik
 
Datasheet scriptspluginforrd
Datasheet scriptspluginforrdDatasheet scriptspluginforrd
Datasheet scriptspluginforrdMidVision
 
My Saminar On Php
My Saminar On PhpMy Saminar On Php
My Saminar On PhpArjun Kumawat
 
Datasheet was pluginforrd
Datasheet was pluginforrdDatasheet was pluginforrd
Datasheet was pluginforrdMidVision
 
Extension Library - Viagra for XPages
Extension Library - Viagra for XPagesExtension Library - Viagra for XPages
Extension Library - Viagra for XPagesUlrich Krause
 
[DanNotes] XPages - Beyound the Basics
[DanNotes] XPages - Beyound the Basics[DanNotes] XPages - Beyound the Basics
[DanNotes] XPages - Beyound the BasicsUlrich Krause
 
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 DevOps.com
 
Dynamics of Leading Legacy Databases
Dynamics of Leading Legacy DatabasesDynamics of Leading Legacy Databases
Dynamics of Leading Legacy DatabasesCognizant
 
Datasheet foldermanagementpluginforrd
Datasheet foldermanagementpluginforrdDatasheet foldermanagementpluginforrd
Datasheet foldermanagementpluginforrdMidVision
 
APACHE
APACHEAPACHE
APACHEARJUN
 
O365 Developer Bootcamp NJ 2018 - Material
O365 Developer Bootcamp NJ 2018 - MaterialO365 Developer Bootcamp NJ 2018 - Material
O365 Developer Bootcamp NJ 2018 - MaterialThomas Daly
 
Db2 migration -_tips,_tricks,_and_pitfalls
Db2 migration -_tips,_tricks,_and_pitfallsDb2 migration -_tips,_tricks,_and_pitfalls
Db2 migration -_tips,_tricks,_and_pitfallssam2sung2
 
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 & MigrationLinh Nguyen
 
FlywayDB Migration with Spring Boot
FlywayDB Migration with Spring BootFlywayDB Migration with Spring Boot
FlywayDB Migration with Spring BootInexture Solutions
 
XPages -Beyond the Basics
XPages -Beyond the BasicsXPages -Beyond the Basics
XPages -Beyond the BasicsUlrich Krause
 

Ă„hnlich wie 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
 

KĂĽrzlich hochgeladen

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
WhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel AraĂşjo
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 

KĂĽrzlich hochgeladen (20)

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
WhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 âś“Call Girls In Kalyan ( Mumbai ) secure service
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

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