Liquibase vs Flyway – Liquibase vs Flyway

最后修改: 2022年 4月 13日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this era of continuous integration and automated refactoring to database development, we need techniques of evolutionary database design. Tools like Liquibase and Flyway follow these techniques and provide an iterative development approach. In this article, we will study the differences and similarities between Liquibase and Flyway.

在这个持续集成和自动重构数据库开发的时代,我们需要进化数据库设计的技术。像LiquibaseFlyway这样的工具遵循这些技术并提供迭代开发方法。在这篇文章中,我们将研究Liquibase和Flyway之间的异同。

Note that no tool is perfect for all the use cases. Each tool is strong in its own place.

请注意,没有一个工具对所有的用例都是完美的。每个工具都有自己的优势。

2. Similarities Between Liquibase and Flyway

2.Liquibase和Flyway之间的相似之处

Since Liquibase and Flyway implement the design principles of the evolutionary database they offer a lot of similar functionality. Both tools:

由于Liquibase和Flyway实现了进化数据库的设计原则,它们提供了很多类似的功能。这两个工具。

  • Are open-source to an extent and help manage, track and deploy database schema changes.
  • Use a versioned migration approach to a database schema change.
  • Are based on Java and provides extensive support for Java frameworks like Spring Boot and Vert.x.
  • Support integration with build tools like Maven and Gradle.
  • Can run independently from the command line through provided scripts.
  • Support a wide variety of databases.

Now we will discuss the differences between the offerings of these tools.

现在我们将讨论这些工具的产品之间的差异。

3. Differences Between Liquibase and Flyway

3.Liquibase和Flyway之间的差异

3.1. Defining a Change

3.1.定义一个变化

Flyway uses SQL for defining a change. On the other hand, Liquibase provides flexibility to specify a change in different formats including SQL such as XML, YAML, and JSON. With Liquibase we can work with database-agnostic languages and easily apply schema changes to different database types.

Flyway使用SQL来定义一个变化。另一方面,Liquibase提供了灵活性,可以用包括SQL在内的不同格式,如XML、YAML和JSON来指定一个变化。通过Liquibase,我们可以使用与数据库无关的语言,并轻松地将模式变更应用于不同的数据库类型。

Flyway is built around a linear database versioning system that increments on each versioned change. This sometimes can create conflicts with parallel development. The filename of a Flyway script defines the type of migration. For example, a migration should have a convention of prefixes as V (for versioned), U (for undo), and R (for repeatable). It will be followed by a version number and a separator __(two underscores) followed by a description and a suffix .sql such as V01__Add_New_Column.sql.

Flyway是围绕一个线性数据库版本系统建立的,该系统在每个版本的变化上都会有递增。这有时会与并行开发产生冲突。Flyway脚本的文件名定义了迁移>的类型。例如,迁移的前缀应该是V(代表版本化)、U(代表撤销)和R(代表可重复)。它后面会有一个版本号和一个分隔符__(两个下划线),然后是一个描述和一个后缀.sql,比如V01__Add_New_Column.sql

Liquibase migration doesn’t need to follow any of the file name conventions. In Liquibase, changes are managed by one ledger known as a master changelog which will be defined as including all the migrations.

Liquibase的迁移不需要遵循任何的文件名惯例。在Liquibase中,变化是由一个被称为主变化日志的账本管理的,它将被定义为包括所有的迁移。

3.2. Storing a Change

3.2.储存一个变化

Both the tools store the deployed change in a table. Flyway migrations are stored in the database schema with a default table named flyway_schema_history. Similarly, Liquibase stores its deployed migrations in a table named databasechangelog. Both the tools support overriding default configuration to change the table name.

两种工具都将部署的变更存储在一个表中。 Flyway迁移被存储在数据库模式中,有一个默认的表,名为flyway_schema_history。同样的,Liquibase也将其部署的迁移存储在一个名为databasechangelog的表中。这两个工具都支持覆盖默认配置来改变表的名称。

3.3. Execution Order of a Change

3.3.更改的执行顺序

Managing the order of a change is comparatively difficult in Flyway. With Flyway, the order depends on the version number and migration type in the filename. Contrarily, Liquibase uses a separate file named master_changelog in which the changes are deployed in the order they are defined.

在Flyway中,管理更改的顺序是相对困难的。在Flyway中,顺序取决于文件名中的版本号和迁移类型。相反,Liquibase使用一个单独的文件,名为master_changelog,其中的更改是按照定义的顺序部署的。

3.4. Rollback a Change

3.4.回滚一项变更

Now, let’s discuss one of the main aspects of database migration. Rollback is needed whenever a bad change has caused a catastrophic issue in the application. Liquibase provides a way to roll back everything or undo specific migrations (available only on paid versions).

现在,让我们来讨论数据库迁移的一个主要方面。当一个错误的改变导致应用程序出现灾难性的问题时,就需要回滚。Liquibase提供了一种回滚一切或撤销特定迁移的方法(仅在付费版本中可用)。

Flyway also has a undo migration, which can be deployed with a file name that starts with U followed by the version that needs to be undone. Its paid version also offers even more complex undo functionality.

Flyway也有撤销迁移功能,可以用一个以U开头的文件名来部署,后面是需要撤销的版本。其付费版本还提供更复杂的撤销功能。

Both the tools offer a decent rollback functionality, but considering only the free version, Flyway offers a good-to-use solution.

这两个工具都提供了一个体面的回滚功能,但考虑到只有免费版本,Flyway提供了一个良好的使用解决方案。

3.5. Selective Deployment of a Change

3.5.有选择地部署一项变革

There are use cases where we need to deploy a change to only one environment. Liquibase wins here when we’ve to selectively deploy a change. Flyway is also capable of doing it, but you would have to set up a different configuration file for each environment or database. With Liquibase we can easily add labels and contexts to ensure deployment in certain places.

在一些使用案例中,我们需要将一个变更部署到一个环境中。当我们需要有选择地部署一个变化时,Liquibase在这里获胜。Flyway也能做到这一点,但你必须为每个环境或数据库设置不同的配置文件。使用Liquibase,我们可以轻松地添加标签和上下文,以确保在某些地方部署。

3.6. Java-Based Migration

3.6.基于Java的迁移

Both tools are strongly Java Oriented and provide Java-based migration. Flyway and Liquibase allow defining a migration within a Java file. This can be advantageous in some scenarios.

这两种工具都是面向Java的,并提供基于Java的迁移。FlywayLiquibase允许在一个Java文件中定义迁移。在某些情况下,这可能是有利的。

3.7. Snapshots & Comparing Databases

3.7.快照和比较数据库

Liquibase allows users to take a snapshot of the current state of the database. We can use this state to compare it to another database. This would be very helpful in scenarios like failover and database replication. Flyway on the other hand doesn’t support any of the snapshot features.

Liquibase允许用户对数据库的当前状态进行快照。我们可以用这个状态与另一个数据库进行比较。这在故障转移和数据库复制等场景中会非常有帮助。另一方面,Flyway并不支持任何快照功能。

3.8. Conditional Deployment

3.8.有条件的部署

Liquibase offers an added feature called pre-conditions. Preconditions allow users to apply changes based on the current state of the database. A changeset will only execute if it passes these preconditions.

Liquibase提供了一个额外的功能,称为预设条件。预设条件允许用户根据数据库的当前状态来应用更改。一个变更集只有在通过这些前提条件时才会执行。

Flyway on the other hand doesn’t support this. But through procedures, we can apply conditions in most SQL-based databases.

另一方面,Flyway并不支持这一点。但是通过程序,我们可以在大多数基于SQL的数据库中应用条件。

4. Conclusion

4.总结

In this article, we compared two of the most used database migration tools Liquibase and Flyway. There are always tradeoffs to make when choosing between tools. There are no strong drawbacks or advantages either of the tools have when compared to one another. You can pick either Liquibase or Flyway based on your application needs and check most of the boxes.

在这篇文章中,我们比较了两个最常用的数据库迁移工具Liquibase和Flyway。在选择工具时,总是要做一些权衡。两种工具相比较,并没有什么强烈的缺点或优势。你可以根据你的应用需求来选择Liquibase或Flyway,并勾选大部分的选项。