如何使用 liquibase 在 Spring 引导中将您的数据库回滚到特定的变更集 ID?

How to rollback your DB to a specific changeset ID in Spring Boot using liquibase?

我有一个基于 Spring Boot 的基本项目,它使用 DDLDML 脚本来使用 Liquibase 数据库版本控制来填充我的 H2 独立数据库。

正在创建表格,并且也在其中填充数据,没有任何问题。但是,我试图回滚到较早的 changeset 版本,但出现以下错误。不确定是我的 maven 配置不正确还是我的命令不正确。

请指导。

命令:

mvn liquibase:rollback -Dliquibase.rollbackTag=01-insertData-addresses-users

错误:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.516 s
[INFO] Finished at: 2019-05-07T16:30:05-04:00
[INFO] ------------------------------------------------------------------------
[ERROR] No plugin found for prefix 'liquibase' in the current project and in the plugin groups [org.mule.tools, org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (C:\Users\R649526\.m2\repository), orgc-public (http://repo-proxy.org
org.net/maven/content/groups/orgc-public/), myrepo (http://repo.orgc.net/maven/content/repositories/MYREPO)] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException

src/main/resources/db/changelog/db.changelog-master.yaml

databaseChangeLog:
  - changeSet:
      id: 01-createTable-addresses-users
      author: Nital Chandel
      changes:
        - sqlFile:
            dbms: h2
            encoding: utf8
            endDelimiter: ;
            path: scripts/01/01-ddl.sql
            relativeToChangelogFile: true
            splitStatements: true
            stripComments: true

  - changeSet:
      id: 01-insertData-addresses-users
      author: Nital Chandel
      changes:
        - sqlFile:
            dbms: h2
            encoding: utf8
            path: scripts/01/01-dml.sql
            relativeToChangelogFile: true
            splitStatements: true
            stripComments: true

  - changeSet:
      id: 02-createTable-project
      author: Nital Chandel
      changes:
        - sqlFile:
            dbms: h2
            encoding: utf8
            endDelimiter: ;
            path: scripts/02/02-ddl.sql
            relativeToChangelogFile: true
            splitStatements: true
            stripComments: true

  - changeSet:
      id: 02-insertData-project
      author: Nital Chandel
      changes:
        - sqlFile:
            dbms: h2
            encoding: utf8
            path: scripts/02/02-dml.sql
            relativeToChangelogFile: true
            splitStatements: true
            stripComments: true

src/main/resources/db/changelog/scripts/01/01-ddl.sql

CREATE TABLE ADDRESSES
(
  ID     NUMBER        NOT NULL,
  STREET VARCHAR2(100) NOT NULL,
  CITY   VARCHAR2(100),
  PIN    NUMBER(6),
  CONSTRAINT ADDRESSES_PK PRIMARY KEY (ID)
);

CREATE TABLE USERS
(
  ID      NUMBER       NOT NULL,
  NAME    VARCHAR2(50) NOT NULL,
  EMAIL   VARCHAR2(100),
  PHONE   NUMBER,
  ADDRESS NUMBER       NOT NULL,
  CONSTRAINT USERS_PK PRIMARY KEY (ID),
  CONSTRAINT USERS_FK FOREIGN KEY (ADDRESS) REFERENCES ADDRESSES (ID)
);          

src/main/resources/db/changelog/scripts/01/01-dml.sql

insert into ADDRESSES(ID, STREET, CITY, PIN) values (1, 'street1', 'city1', 111111);
insert into ADDRESSES(ID, STREET, CITY) values (2, 'street2', 'city2');

insert into USERS(ID, NAME, EMAIL, ADDRESS) values (1, 'Soumitra', 'soumitra@email.com', 1);
insert into USERS(ID, NAME, EMAIL, PHONE, ADDRESS) values (2, 'Suman', 'suman@email.com', 1254789541, 2);

src/main/resources/db/changelog/scripts/02/02-ddl.sql

CREATE TABLE PROJECT
(
  ID      NUMBER              NOT NULL PRIMARY KEY,
  NAME    VARCHAR2(256)       NOT NULL,
  CODE    VARCHAR2(10),
  ENABLED CHAR(1) DEFAULT 'Y' NOT NULL
);

src/main/resources/db/changelog/scripts/02/02-dml.sql

insert into PROJECT(ID, NAME, CODE) values (1, 'Project 1', 'A');
insert into PROJECT(ID, NAME, CODE) values (2, 'Project 2', 'B');

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>projectstar</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>projectstar</name>
    <description>Project management tool</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

注意: 我已经经历过类似的 SO link (Liquibase Rollback Spring boot) 但它对我没有帮助,原因有二。首先,它没有显示 maven 和 spring-boot 配置如何执行回滚,其次,我的示例中的变更集是使用指向 .sql 文件的 .yaml 文件编写的,这与上面提到的一个link。请不要将此标记为重复。

Not sure if my maven configuration is incorrect or my command is incorrect.

错误消息No plugin found for prefix 'liquibase' in the current project表示Maven找不到具有该名称的插件。所以,在这种情况下,你的maven配置不完整。

您需要在 pom.xml 文件中定义 liquibase-maven-plugin。此外,您可能需要调整其配置以指向 Spring Boot 使用的位置。