This Post is follw-up to several blog articles.
Flyway https://devopsaiml.com/flyway-the-sleek-solution-to-database-migrations-automation-and-more/
LiquiBase https://devopsaiml.com/liquibase-your-swiss-army-knife-for-database-version-control/
When working in an Agile development environment with a focus on CI/CD (Continuous Integration/Continuous Deployment), managing database schema changes becomes an essential aspect of the software development lifecycle. Various tools like Dacpac, Liquibase, and Flyway offer ways to manage these changes, but each has its own set of features, advantages, and disadvantages. Here’s a comparison based on different criteria:
Database Support
- Dacpac: Primarily supports SQL Server.
- Liquibase: Supports a wide range of databases like MySQL, Oracle, SQL Server, PostgreSQL, and more.
- Flyway: Similar to Liquibase, it supports various databases like MySQL, Oracle, SQL Server, and PostgreSQL among others.
Language
- Dacpac: Works with T-SQL and relies on a model-based approach.
- Liquibase: Uses XML, YAML, or JSON for change scripts.
- Flyway: Uses plain SQL or Java-based migrations for change scripts.
Tooling & Ecosystem
- Dacpac: Integrated well with Microsoft tools like Visual Studio, SSMS, and Azure DevOps.
- Liquibase: Provides a CLI and integrates with Maven, Gradle, and other build tools. Also has plugins/extensions for various databases.
- Flyway: Provides a simple CLI, API, and plugins for Maven and Gradle.
Versioning
- Dacpac: Model-based, state-driven approach where the desired end-state is versioned.
- Liquibase: Change-set based, each change is versioned and stored in a changelog file.
- Flyway: Version-based, each migration script is a new version.
Rollbacks
- Dacpac: Typically requires a previous version of a Dacpac to rollback, not as straightforward as the other tools.
- Liquibase: Built-in rollback capabilities based on the changelog.
- Flyway: Supports undo migrations which can be written to rollback the last applied version.
Complexity & Learning Curve
- Dacpac: Easier for SQL Server specialists, may have a steeper learning curve for others.
- Liquibase: More flexible but can be complex due to its XML/JSON/YAML based changes.
- Flyway: Simplicity is a key feature, easy to get started, especially if you prefer writing plain SQL.
Community and Documentation
- Dacpac: Good documentation but less community support compared to Liquibase and Flyway.
- Liquibase: Strong community support, extensive documentation, and many third-party integrations.
- Flyway: Well-documented, strong community, and supported by Redgate since 2019.
CI/CD Integration
- Dacpac: Works well with Azure DevOps and other CI/CD tools but is generally limited to SQL Server.
- Liquibase: Easily integrates with Jenkins, GitLab CI, and other CI/CD tools through command-line or plugins.
- Flyway: Can be easily integrated into CI/CD pipelines through its command-line interface or plugins for build tools like Maven and Gradle.
Conclusion
- Dacpac is most suited for teams heavily invested in the Microsoft ecosystem and SQL Server.
- Liquibase offers the most flexibility and wide-ranging database support, making it suitable for polyglot persistence architectures.
- Flyway is excellent for teams who prefer simplicity and want to work with plain SQL while also having the ability to support various databases.
Your choice among Dacpac, Liquibase, and Flyway will depend on your specific needs, the databases you’re working with, and the complexity of your project.
When it comes to flexibility in handling rollbacks, Liquibase generally stands out. Here’s why:
Liquibase
- Rollback Support: Liquibase explicitly focuses on enabling database changes to be rolled back. You can specify rollback scripts for every change set in your change log.
- Automatic Rollback: For many types of changes, Liquibase can automatically generate the rollback SQL for you.
- Tagging: You can tag database states and rollback to those tags.
- Changelog: Each change is meticulously recorded in the database changelog, allowing a detailed view of what has been applied and making it easier to decide what to rollback.
Flyway
- Undo Migrations: Flyway introduced the concept of “undo” migrations which allows you to write SQL scripts to undo previous migrations. However, these have to be authored manually.
- Version Control: Flyway does keep meticulous version control but doesn’t inherently provide an automatic rollback mechanism. You have to manage this yourself by creating undo scripts.
Dacpac (SQL Server Data Tools)
- State-Based: Dacpac uses a state-based approach which may make rollback more complicated. You generally need to have a previous version of the Dacpac file to revert to an earlier state.
- No Explicit Rollback: There’s no built-in rollback mechanism. You’d usually restore to a previous backup or apply a previous Dacpac to revert changes, which may not be practical in all situations.
Summary
If rollback flexibility is a primary concern, Liquibase offers the most built-in capabilities and would likely be the best choice. Flyway offers some flexibility but expects you to manage the rollback scripts manually. Dacpac is the least flexible in this regard, making it more challenging to handle rollbacks in a more dynamic, Agile environment with frequent changes.
Of course, the “best” tool may depend on other factors such as the specific databases you are using, your development environment, and your team’s expertise. But strictly in terms of rollback capabilities, Liquibase has the edge.
How rollback works?
Rolling back a column addition in a database can be a tricky operation, particularly if data has been added to the new column. Below is how you might perform a rollback operation for the addition of a new column in Liquibase, Flyway, and Dacpac:
Liquibase:
- Initial Change: When you initially add the column, you would define a changeset in your XML/YAML/JSON changelog file.
<changeSet id="1" author="example">
<addColumn tableName="your_table">
<column name="new_column" type="varchar(255)" />
</addColumn>
</changeSet>
Code language: HTML, XML (xml)
Rollback: In Liquibase, you have the option to include a rollback command within the changeset.
<rollback>
<dropColumn tableName="your_table" columnName="new_column"/>
</rollback>
Code language: HTML, XML (xml)
Performing the Rollback: To actually rollback, you would use the Liquibase CLI command:
liquibase rollbackCount 1
This would undo the last change, which was adding a new column.
Flyway:
- Initial Change: You add a new versioned migration script to add the column:
ALTER TABLE your_table ADD COLUMN new_column VARCHAR(255);
Undo Migration: You then create an undo script to remove the column:
ALTER TABLE your_table DROP COLUMN new_column;
Performing the Rollback: To rollback, you’d run:
flyway undo
Dacpac (SQL Server Data Tools):
- Initial Change: You would add the column through a GUI or by altering the database project and then publish the changes to generate a new Dacpac.
- Rollback: Since Dacpac is state-based, you would either:
- Restore to a backup of the database if you have one.
- Revert the changes in your database project to remove the new column and then publish to generate a new Dacpac. You would then deploy this new Dacpac.
- Performing the Rollback: Deploy the Dacpac via SQL Server Management Studio, Azure Data Studio, or an equivalent tool.
In summary, Liquibase provides built-in support for rollbacks and arguably offers the most flexibility in this specific scenario. Flyway allows for manual creation of undo scripts to handle rollbacks, while Dacpac is somewhat rigid, requiring you to revert to previous states or backups.
Handling failures gracefully
Handling failures gracefully is crucial for any database migration or versioning tool. Here’s how Dacpac, Liquibase, and Flyway handle failures:
Liquibase
- Atomic Changesets: Liquibase aims to make each changeset atomic. If a changeset fails, all the changes in that changeset are rolled back.
- Database Changelog: Liquibase maintains a
DATABASECHANGELOGtable that keeps track of all changes that have been successfully applied. Failed changesets won’t be marked as executed in this table. - Preconditions: You can specify preconditions that must be met for a changeset to run. If the precondition fails, the changeset is skipped, and the migration can be halted based on the configuration.
- Fail Fast: By default, Liquibase stops executing subsequent changesets if one fails.
- Error Messages: Detailed error messages are usually provided for debugging purposes.
Flyway
- Versioned Migrations: Flyway migrations are versioned, and the tool halts if a migration fails, ensuring no partial updates to the schema.
- Schema History Table: Like Liquibase, Flyway maintains a history table (
flyway_schema_history) that keeps track of successfully applied migrations. Failed migrations are not marked as successful. - Callbacks: You can define custom behavior on failure using callbacks.
- Repair: Flyway provides a
repaircommand that will fix theflyway_schema_historytable by removing any failed migrations on databases that support DDL transactions. - Error Messages: Flyway also provides detailed error messages and allows custom scripts to run on failure if needed.
Dacpac (SQL Server Data Tools)
- Transactional: If a deployment fails, SQL Server’s transactional capabilities can be used to roll back the changes automatically.
- Error Reporting: Failures are reported in detail, often with recommendations on how to resolve them.
- Logs: Dacpac deployments can be configured to provide verbose logs, which can be helpful for debugging failures.
- State-based Approach: Since Dacpac uses a state-based approach, the database should remain at its original state if the deployment fails.
- Script Inspection: You have the option to generate the SQL script that will be run, allowing you to inspect it for potential issues before applying.
Summary
- Liquibase: Offers atomic changesets and preconditions, providing a highly configurable way to manage failures. Also has detailed logging.
- Flyway: Provides a simple, straightforward way to manage failures with its versioned migrations and
repaircommand. It also allows the definition of custom behavior through callbacks. - Dacpac: Uses SQL Server’s native transactional capabilities to roll back failed deployments, with detailed logging and error messages for debugging.
All three tools have robust ways of handling failures, but they do so differently, reflecting their different design philosophies. Liquibase and Flyway are more geared toward incremental change management and offer more flexibility through their changelog and version history tables. Dacpac, being more state-driven, relies on SQL Server’s capabilities for transactional integrity.
Feel free to add comments and share.