PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 28, 2022

[FIXED] How to migrate my database schema only once when deploying to multiple instances?

 September 28, 2022     continuous-deployment, database-migration, database-schema, deployment, mysql     No comments   

Issue

I have a MySQL database backed web application which I deploy to multiple cloud instances. I am using a database migration tool which means my schema changes are run as part of the deployment (say, via a configuration management tool). However, when I am spinning up multiple instances of my web app, all of these instances will try to apply the schema change to the MySQL database and I can basically end up in an unknown state of my final DB/error out. What is the recommended approach to database schema change deployments in such a setup?

The only possible solution that I see is to simply run the schema change separate from the app deployment process. Any other thoughts?


Solution

Here are some solutions:

  • Separate the migration action into something that you can call independently, and call it explicitly in one place in your production and production-like deployments.

  • Designate one cloud instance as the migration runner. Mark it in some way so that your deployment process can distinguish it from other instances: give it a special hostname; on EC2, set a value in user data; if using Chef, give the instance a Chef role; etc. Make your deployment check whether it is deploying to the migration runner and migrate only if so.

  • Write code that obtains a lock in your database (or possibly in some other singleton resource in your production environment), checks the database schema version, exits if it is at the expected version, and migrates if it is not at the expected version.

Which of these approaches is the best will depend on your deployment environment and tools. In any case, you'll want to continue migrating automatically (e.g. as part of server startup) for convenience in non-production environments.



Answered By - Dave Schweisguth
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,214,208

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing