mysql Liquibase回滚不适用于“Liquibase回滚成功”

zpgglvta  于 2023-01-01  发布在  Mysql
关注(0)|答案(3)|浏览(174)

请,你能帮我回滚行动?这是某种魔术-liquibase报告我成功回滚行动,但没有任何变化。我们使用sql格式的SQL。例如,我有3个文件:
CASE-0-1.sql:

--liquibase formatted sql

--changeset CASE-0-1:1 failOnError:true
CREATE TABLE tt1(
cc1 INT(11));

--rollback DROP TABLE tt1;
--rollback CREATE TABLE tr1(
--rollback cr1 INT(20));

CASE-0-2.sql:

--liquibase formatted sql

--changeset CASE-0-2:1 failOnError:true
CREATE TABLE tt2(
cc2 INT(11));

--rollback DROP TABLE tt2;
--rollback CREATE TABLE tr2(
--rollback cr2 INT(20));

CASE-0-3.sql:

--liquibase formatted sql

--changeset CASE-0-3:1 failOnError:true
CREATE TABLE tt3(
cc3 INT(11));

--rollback DROP TABLE tt3;
--rollback CREATE TABLE tr3(
--rollback cr3 INT(20));

I在进行任何更改之前标记数据库:

/root/liquibase/liquibase --logLevel=debug --classpath=/usr/share/java/mysql-connector-java.jar --driver=com.mysql.jdbc.Driver \
  --url="jdbc:mysql://localhost/liquibase_test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true" \
  --logFile=0state.tag.log --username=trunk --password=Trunk \
  tag "0state"

之后,我应用文件和标签数据库后,每个文件应用:

/root/liquibase/liquibase --logLevel=debug --classpath=/usr/share/java/mysql-connector-java.jar --driver=com.mysql.jdbc.Driver \
  --url="jdbc:mysql://localhost/liquibase_test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true" \
  --changeLogFile=CASE-0-1.sql --logFile=CASE-0-1.sql.update.log --username=trunk --password=Trunk \
  update
/root/liquibase/liquibase --logLevel=debug --classpath=/usr/share/java/mysql-connector-java.jar --driver=com.mysql.jdbc.Driver \
  --url="jdbc:mysql://localhost/liquibase_test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true" \
  --logFile=CASE-0-1.sql.tag.log --username=trunk --password=Trunk \
  tag "CASE-0-1"

/root/liquibase/liquibase --logLevel=debug --classpath=/usr/share/java/mysql-connector-java.jar --driver=com.mysql.jdbc.Driver \
  --url="jdbc:mysql://localhost/liquibase_test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true" \
  --changeLogFile=CASE-0-2.sql --logFile=CASE-0-2.sql.update.log --username=trunk --password=Trunk \
  update
/root/liquibase/liquibase --logLevel=debug --classpath=/usr/share/java/mysql-connector-java.jar --driver=com.mysql.jdbc.Driver \
  --url="jdbc:mysql://localhost/liquibase_test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true" \
  --logFile=CASE-0-2.sql.tag.log --username=trunk --password=Trunk \
  tag "CASE-0-2"

/root/liquibase/liquibase --logLevel=debug --classpath=/usr/share/java/mysql-connector-java.jar --driver=com.mysql.jdbc.Driver \
  --url="jdbc:mysql://localhost/liquibase_test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true" \
  --changeLogFile=CASE-0-3.sql --logFile=CASE-0-3.sql.update.log --username=trunk --password=Trunk \
  update
/root/liquibase/liquibase --logLevel=debug --classpath=/usr/share/java/mysql-connector-java.jar --driver=com.mysql.jdbc.Driver \
  --url="jdbc:mysql://localhost/liquibase_test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true" \
  --logFile=CASE-0-3.sql.tag.log --username=trunk --password=Trunk \
  tag "CASE-0-3"

结果:

[root@mysql]# mysql -e "SELECT ID, AUTHOR, FILENAME, DATEEXECUTED, ORDEREXECUTED, EXECTYPE, DESCRIPTION, COMMENTS, TAG \
>           FROM DATABASECHANGELOG;" liquibase_test
+---------------+-----------+--------------------+---------------------+---------------+----------+-------------+----------+----------+
| ID            | AUTHOR    | FILENAME           | DATEEXECUTED        | ORDEREXECUTED | EXECTYPE | DESCRIPTION | COMMENTS | TAG      |
+---------------+-----------+--------------------+---------------------+---------------+----------+-------------+----------+----------+
| 1506498093527 | liquibase | liquibase-internal | 2017-09-27 10:41:33 |             1 | EXECUTED | empty       |          | 0state   |
| 1             | CASE-0-1  | CASE-0-1.sql       | 2017-09-27 10:41:57 |             2 | EXECUTED | sql         |          | CASE-0-1 |
| 1             | CASE-0-2  | CASE-0-2.sql       | 2017-09-27 10:42:08 |             3 | EXECUTED | sql         |          | CASE-0-2 |
| 1             | CASE-0-3  | CASE-0-3.sql       | 2017-09-27 10:42:12 |             4 | EXECUTED | sql         |          | CASE-0-3 |
+---------------+-----------+--------------------+---------------------+---------------+----------+-------------+----------+----------+
[root@mysql]# mysql -e "SHOW TABLES;" liquibase_test
+--------------------------+
| Tables_in_liquibase_test |
+--------------------------+
| DATABASECHANGELOG        |
| DATABASECHANGELOGLOCK    |
| tt1                      |
| tt2                      |
| tt3                      |
+--------------------------+

现在,让我们尝试回滚:

/root/liquibase/liquibase --logLevel=debug --classpath=/usr/share/java/mysql-connector-java.jar --driver=com.mysql.jdbc.Driver \
  --url="jdbc:mysql://localhost/liquibase_test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true" \
  --changeLogFile=CASE-0-2.sql --logFile=CASE-0-2.sql.rollback.log --username=trunk --password=Trunk \
  rollback "CASE-0-2"
Liquibase Rollback Successful

看起来一切正常,但未执行回滚:

[root@mysql]# mysql -e "SELECT ID, AUTHOR, FILENAME, DATEEXECUTED, ORDEREXECUTED, EXECTYPE, DESCRIPTION, COMMENTS, TAG \
>           FROM DATABASECHANGELOG;" liquibase_test
+---------------+-----------+--------------------+---------------------+---------------+----------+-------------+----------+----------+
| ID            | AUTHOR    | FILENAME           | DATEEXECUTED        | ORDEREXECUTED | EXECTYPE | DESCRIPTION | COMMENTS | TAG      |
+---------------+-----------+--------------------+---------------------+---------------+----------+-------------+----------+----------+
| 1506498093527 | liquibase | liquibase-internal | 2017-09-27 10:41:33 |             1 | EXECUTED | empty       |          | 0state   |
| 1             | CASE-0-1  | CASE-0-1.sql       | 2017-09-27 10:41:57 |             2 | EXECUTED | sql         |          | CASE-0-1 |
| 1             | CASE-0-2  | CASE-0-2.sql       | 2017-09-27 10:42:08 |             3 | EXECUTED | sql         |          | CASE-0-2 |
| 1             | CASE-0-3  | CASE-0-3.sql       | 2017-09-27 10:42:12 |             4 | EXECUTED | sql         |          | CASE-0-3 |
+---------------+-----------+--------------------+---------------------+---------------+----------+-------------+----------+----------+
[root@mysql]# mysql -e "SHOW TABLES;" liquibase_test
+--------------------------+
| Tables_in_liquibase_test |
+--------------------------+
| DATABASECHANGELOG        |
| DATABASECHANGELOGLOCK    |
| tt1                      |
| tt2                      |
| tt3                      |
+--------------------------+

正如您所看到的,表集与我们所拥有的相同,没有删除表,也没有创建其他表。
回滚日志文件中包含的内容:

[root@mysql]# cat CASE-0-2.sql.rollback.log:
DEBUG 27.09.17 10:53: liquibase: Connected to trunk2@127.0.0.1@jdbc:mysql://localhost/liquibase_test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true
DEBUG 27.09.17 10:53: liquibase: Setting auto commit to false from true
DEBUG 27.09.17 10:53: liquibase: Executing QUERY database command: select count(*) from liquibase_test.DATABASECHANGELOGLOCK
DEBUG 27.09.17 10:53: liquibase: Executing QUERY database command: select count(*) from liquibase_test.DATABASECHANGELOGLOCK
DEBUG 27.09.17 10:53: liquibase: Executing QUERY database command: SELECT LOCKED FROM liquibase_test.DATABASECHANGELOGLOCK WHERE ID=1
DEBUG 27.09.17 10:53: liquibase: Lock Database
DEBUG 27.09.17 10:53: liquibase: Executing UPDATE database command: UPDATE liquibase_test.DATABASECHANGELOGLOCK SET LOCKED = 1, LOCKEDBY = 'mysql-gigantic-dummy-dvaco-trunk-28.aws.srv (172.29.13.83)', LOCKGRANTED = '2017-09-27 10:53:26.625' WHERE ID = 1 AND LOCKED = 0
INFO 27.09.17 10:53: liquibase: Successfully acquired change log lock
DEBUG 27.09.17 10:53: liquibase: Computed checksum for 1506498806750 as 17f1742877fc068db5453ca9db9d60e3
DEBUG 27.09.17 10:53: liquibase: Executing QUERY database command: SELECT MD5SUM FROM liquibase_test.DATABASECHANGELOG WHERE MD5SUM IS NOT NULL LIMIT 1
DEBUG 27.09.17 10:53: liquibase: Executing QUERY database command: select count(*) from liquibase_test.DATABASECHANGELOG
INFO 27.09.17 10:53: liquibase: Reading from liquibase_test.DATABASECHANGELOG
DEBUG 27.09.17 10:53: liquibase: Executing QUERY database command: SELECT * FROM liquibase_test.DATABASECHANGELOG ORDER BY DATEEXECUTED ASC, ORDEREXECUTED ASC
DEBUG 27.09.17 10:53: liquibase: CASE-0-2.sql: CASE-0-2.sql::1::CASE-0-2: Computed checksum for inputStream as 80fa2b893f3b36ef72cf796a9ce61189
DEBUG 27.09.17 10:53: liquibase: CASE-0-2.sql: CASE-0-2.sql::1::CASE-0-2: Computed checksum for 7:80fa2b893f3b36ef72cf796a9ce61189: as 6c11f0a414ae9865f42a220f71f1e3d1
DEBUG 27.09.17 10:53: liquibase: Release Database Lock
DEBUG 27.09.17 10:53: liquibase: Executing UPDATE database command: UPDATE liquibase_test.DATABASECHANGELOGLOCK SET LOCKED = 0, LOCKEDBY = NULL, LOCKGRANTED = NULL WHERE ID = 1
INFO 27.09.17 10:53: liquibase: Successfully released change log lock

我检查了不同的变体,不同的Liquibase文件格式,不同的命令行选项-没用。Liquibase总是说"Liquibase回滚成功",但什么也不做。
请帮助。可能有人使用回滚操作与sql,或其他格式?哪里的错误?我做错了什么?它是工作liquibase功能在所有?

ztyzrc3y

ztyzrc3y1#

希望您仍然对帮助感兴趣,但据我所知,您没有包含CASE-0-3 SQL脚本,因此liquibase不知道您提供的CASE-0-2标记之外应用的更改-因此它无法回滚。
我的团队所做的是创建一个更健壮的changelog.xml,它引用我们的.sql脚本,而不是使用.sql脚本作为changelog文件。
如果有帮助就告诉我!

owfi6suc

owfi6suc2#

我解决了这个问题,发现它工作正常。问题如下:在迁移/更新过程中,--changeLogFile参数获取文件名并在--classpath文件夹中找到它。然后,它将文件名及其完整路径存储在其日志表中。
但是在回滚时,同样的方法不起作用。您需要在--changeLogFile中提供文件名的完整路径。它只是将此参数与日志表进行比较。在本例中,它不使用类路径来在搜索日志表之前预先添加到changeLogFile。

xytpbqjk

xytpbqjk3#

对于那些谁将面临类似的问题,这里是一个可能的原因的问题:
在回滚过程中,liquibase会将给定参数与数据库更改日志表中的相应参数进行比较。
假设我们要调用一个回滚,并且我们希望在项目目录之外执行,那么,我们有下一个命令:

liquibase --url="some-jdbc-url" --changelog-file="/some/path/to/the/db/changelog/changelog-1.json" --username=someusername --password=somepassword rollback-count 1

在我们的数据库echangelog表中还有下一个内容:

|   id  |   author  |       filename               |      dateexecuted     |orderexecuted|exectype|       md5sum                     |  description   |comments|tag|liquibase|contexts|labels|deployment_id|
|-------|-----------|------------------------------|-----------------------|-------------|--------|----------------------------------|----------------|--------|---|---------|--------|------|-------------|
|some-id|some-author|/db/changelog/changelog-1.json|2022-12-29 10:00:00.000|     1       |EXECUTED|8:2645faaa8ad312262c8dc1111eeeeeee|some-description|        |   |  4.18.0 |        |      | 1112233445  |

请注意,表中的文件名不同(例如,因为启动期间在项目目录中调用了变更日志)
因此,在这种情况下,尽管打印了Liquibase command 'rollback-count' was executed successfully,但回滚不会产生任何影响。

这是因为回滚命令中提供的changelog路径与表中的路径不相等:(/some/path/to/the/db/changelog/changelog-1.json/db/changelog/changelog-1.json)。
它们必须相等

要解决这个问题,我们需要转到相应的目录,这样我们到changelog的路径就相同了。在我们的示例中,我们应该转到/some/path/to/the并调用下一个命令:

liquibase --url="some-jdbc-url" --changelog-file="/db/changelog/changelog-1.json" --username=someusername --password=somepassword rollback-count 1

现在,由于--changelog-file值和filename值相等,因此回滚会进行更改。

因此,在调查过程中,请注意参数匹配(在命令和数据库更改日志表中提供)。检查提供的更改日志路径、计算的校验和、作者等。

相关问题