Rolling Back Migrations with Flyway – 利用Flyway回溯迁徙

最后修改: 2020年 9月 6日

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

1. Introduction

1.绪论

In this short tutorial, we’ll explore a couple of ways to rollback a migration with Flyway.

在这个简短的教程中,我们将探讨使用Flyway.回滚迁移的几种方法。

2. Simulate Rollback with a Migration

2.用迁移模拟回滚

In this section, we’ll rollback our database using a standard migration file.

在本节中,我们将使用一个标准的迁移文件回滚我们的数据库。

In our examples, we’ll use the command-line version of Flyway. However, the core principles are equally applicable to the other formats, such as the core API, Maven plugin, etc.

在我们的例子中,我们将使用Flyway的命令行版本。然而,核心原则同样适用于其他格式,如核心API、Maven插件等。

2.1. Create Migration

2.1.创建迁移

First, let’s add a new book table to our database. In order to do this, we’ll create a migration file called V1_0__create_book_table.sql:

首先,让我们在数据库中添加一个新的book表。为了做到这一点,我们将创建一个名为V1_0__create_book_table.sql的迁移文件。

create table book (
  id numeric,
  title varchar(128),
  author varchar(256),
  constraint pk_book primary key (id)
);

Secondly, let’s apply the migration:

第二,让我们应用迁移。

./flyway migrate

2.2. Simulate Rollback

2.2 模拟回滚

Then, at some point, say we need to reverse the last migration.

然后,在某些时候,说我们需要逆转最后的迁移。

In order to restore the database to before the book table was created, let’s create migration called V2_0__drop_table_book.sql:

为了将数据库恢复到book表创建之前,让我们创建名为V2_0__drop_table_book.sql的迁移。

drop table book;

Next, let’s apply the migration:

接下来,我们来应用迁移。

./flyway migrate

Finally, we can check the history of all the migrations using:

最后,我们可以用以下方法检查所有迁移的历史。

./flyway info

which gives us the following output:

这给了我们以下的输出。

+-----------+---------+-------------------+------+---------------------+---------+
| Category  | Version | Description       | Type | Installed On        | State   |
+-----------+---------+-------------------+------+---------------------+---------+
| Versioned | 1.0     | create book table | SQL  | 2020-08-29 16:07:43 | Success |
| Versioned | 2.0     | drop table book   | SQL  | 2020-08-29 16:08:15 | Success |
+-----------+---------+-------------------+------+---------------------+---------+

Notice that our second migration ran successfully.

请注意,我们的第二次迁移运行成功了。

As far as Flyway is concerned, the second migration file is just another standard migration. The actual restoring of the database to the previous version is done entirely through SQL. For example, in our case, the SQL of dropping the table is the opposite of the first migration, which creates the table.

就Flyway而言,第二个迁移文件只是另一个标准迁移。实际将数据库恢复到以前的版本完全是通过SQL完成的。例如,在我们的案例中,丢弃表的SQL与第一次迁移相反,第一次迁移是创建表的。

Using this method, the audit trail doesn’t show us that the second migration is related to the first, as they have different version numbers. In order to get such an audit trail, we need to use Flyway Undo.

使用这种方法,审计跟踪并没有向我们显示第二次迁移与第一次迁移的关系,因为它们的版本号不同。为了获得这样的审计线索,我们需要使用Flyway Undo。

3. Using Flyway Undo

3.使用Flyway Undo

Firstly, it’s important to note that Flyway Undo is a commercial feature of Flyway and isn’t available in the Community Edition. Therefore, we’ll need either the Pro Edition or Enterprise Edition in order to use this feature.

首先,需要注意的是,Flyway Undo是Flyway的一个商业功能,在社区版中并不存在。因此,我们需要专业版或企业版才能使用这个功能。

3.1. Create Migration Files

3.1.创建迁移文件

First, let’s create a migration file called V1_0__create_book_table.sql:

首先,让我们创建一个名为V1_0__create_book_table.sql的迁移文件。

create table book (
  id numeric,
  title varchar(128),
  author varchar(256),
  constraint pk_book primary key (id)
);

Secondly, let’s create the corresponding undo migration file U1_0__create_book_table.sql:

其次,让我们创建相应的撤销迁移文件U1_0__create_book_table.sql

drop table book;

In our undo migration, notice how the filename-prefix is ‘U’ compared with the normal migration prefix of ‘V’. Also, in our undo migration files, we write the SQL that reverses the changes of the corresponding migration file. In our case, we’re dropping the table that’s created by the normal migration.

在我们的撤销迁移中,注意到文件名的前缀是 “U”,而正常迁移的前缀是 “V”。另外,在我们的撤销迁移文件中,我们写了一个SQL语句来逆转相应迁移文件的变化。在我们的例子中,我们要丢弃由正常迁移创建的表。

3.2. Apply Migrations

3.2.应用迁移

Next, let’s check the current state of the migrations:

接下来,让我们检查一下当前的迁移状态。

./flyway -pro info

This gives us the following output:

这给我们提供了以下输出。

+-----------+---------+-------------------+------+--------------+---------+----------+
| Category  | Version | Description       | Type | Installed On | State   | Undoable |
+-----------+---------+-------------------+------+--------------+---------+----------+
| Versioned | 1.0     | create book table | SQL  |              | Pending | Yes      |
+-----------+---------+-------------------+------+--------------+---------+----------+

Notice the last column, Undoable, which indicates Flyway has detected an undo migration file that accompanies our normal migration file.

请注意最后一栏,Undoable,这表明Flyway已经检测到了一个伴随着我们正常迁移文件的撤销迁移文件。

Next, let’s apply our migrations:

接下来,让我们应用我们的迁移。

./flyway migrate

When it completes, our migrations are complete, and our schema has a new book table:

当它完成后,我们的迁移就完成了,我们的模式有了一个新的书表。

                List of relations
 Schema |         Name          | Type  |  Owner   
--------+-----------------------+-------+----------
 public | book                  | table | baeldung
 public | flyway_schema_history | table | baeldung
(2 rows)

3.3. Rollback the Last Migration

3.3.回滚上次迁移的数据

Finally, let’s undo the last migration using the command line:

最后,让我们用命令行撤消最后一次迁移。

./flyway -pro undo

After the command has run successfully, we can check the status of the migrations again:

命令运行成功后,我们可以再次检查迁移的状态。

./flyway -pro info

which gives us the following output:

这给了我们以下的输出。

+-----------+---------+-------------------+----------+---------------------+---------+----------+
| Category  | Version | Description       | Type     | Installed On        | State   | Undoable |
+-----------+---------+-------------------+----------+---------------------+---------+----------+
| Versioned | 1.0     | create book table | SQL      | 2020-08-22 15:48:00 | Undone  |          |
| Undo      | 1.0     | create book table | UNDO_SQL | 2020-08-22 15:49:47 | Success |          |
| Versioned | 1.0     | create book table | SQL      |                     | Pending | Yes      |
+-----------+---------+-------------------+----------+---------------------+---------+----------+

Notice how the undo has been successful, and the first migration is back to pending. Also, in contrast to the first method, the audit trail clearly shows the migrations that were rolled back.

注意到撤消是如何成功的,第一次迁移又恢复到了待定状态。另外,与第一种方法相比,审计跟踪清楚地显示了被回滚的迁移。

Although Flyway Undo can be useful, it assumes that the whole migration has succeeded. For example, it may not work as expected if a migration fails partway through.

虽然Flyway Undo很有用,但它假定整个迁移已经成功。例如,如果迁移在中途失败,它可能不会像预期那样工作。

4. Conclusion

4.总结

In this short tutorial, we looked at restoring our database using a standard migration. We also looked at the official way of rolling back migrations using Flyway Undo. As usual, all of our code that relates to this tutorial can be found over on GitHub.

在这个简短的教程中,我们研究了使用标准迁移来恢复我们的数据库。我们还研究了使用Flyway Undo来回滚迁移的官方方法。像往常一样,我们所有与本教程相关的代码都可以在GitHub上找到