Software Development Community Question

How to handle database migrations in a fully automated CI/CD pipeline without data loss?

Posted on 2024-03-22 • In Software Development • By Richard Wilson

We've automated our application code deployments, but database changes are still a manual, scary process. How are modern DevOps teams handling SQL schema changes within the CI/CD pipeline? We use PostgreSQL and are worried about a failed migration locking up the production database or causing data corruption. Are there specific tools that allow for "dry runs" or automated rollbacks of the database state?

3 Answers 11,230 Views

All answers to this question 3 Answers

Susan Walker Answered on 2024-03-25
Answered

You absolutely need to look into tools like Liquibase or Flyway. These tools treat your database schema as version-controlled code. In a CI/CD pipeline, the tool checks a "schema_version" table and applies only the missing migration scripts. For safety, we always run migrations in a staging environment that is a clone of production first. Also, follow the "expand and contract" pattern: first, add the new column (expand), then deploy code that uses both, and finally delete the old column (contract). This avoids downtime and allows the code to function even if a rollback is needed.

Steven Hall Answered on 2024-03-27
Answered

Are you using a specific ORM like Entity Framework or Sequelize, or are you writing raw SQL migration scripts for your pipeline?

Jeffrey Young Commented on 2024-03-29
Commented

We are using raw SQL scripts because we want full control over the execution plan. My main fear is a long-running ALTER TABLE command locking the UI. How do you handle migrations that might take several minutes to run on a table with millions of rows?

Linda Young Answered on 2024-03-31
Answered

We use a "pre-deployment" job in our pipeline that runs the migration. If the migration script fails, the entire deployment stops before the new code is even touched.

Susan Walker Commented on 2024-04-01
Commented

This is a solid fail-safe, Linda. It ensures the environment state matches the code requirements before anything goes live.

Write a Comment

Your email address will not be published. Required fields are marked (*)

Join the LearnersEra Community

Ask questions, share knowledge and learn from professionals across technology, project management and certification topics.

Change Country and Nearest City

Select your country and nearest city to see courses available in your region.