SpringBoot中使用liquibase和H2数据库进行单元测试

Using liquibase and H2 database for unit tests in SpringBoot

我正在尝试使用我的 liquibase 脚本在 H2 数据库中为我在 springboot 中的单元测试设置表。下面是我的一张表的 liquibase 脚本

<changeSet  id="MSG_MONITORED_GRP:CREATE" author="sharakab">
        <createTable catalogName="${rec_owner}" tableName="MSG_MONITORED_GRP" tablespace="${table_space_rec_data}">
            <column name="MESSAGE_ID" type="VARCHAR2(100 CHAR)">
                <constraints nullable="false" />
            </column>
            <column name="MONITORED_GRP_ID" type="VARCHAR2(20 CHAR)"/>
            <column name="MONITORED_GRP_NAME" type="VARCHAR2(50 CHAR)"/>
            <column name="RECORD_CREATION_DATE" type="TIMESTAMP(6)"/>
        </createTable>

        <addPrimaryKey catalogName="${rec_owner}" columnNames="MESSAGE_ID" constraintName="PK_MSG_MONITORED_GRP"
                       tableName="MSG_MONITORED_GRP" tablespace="${table_space_rec_index}"/>
    </changeSet>
    
    //another change set
              
     <changeSet  id="MSG_MONITORED_GRP::ALTER_MONITORED_GRP_NAME" author="sharakab">
        <renameColumn catalogName="${rec_owner}" oldColumnName="MONITORED_GRP_ID" newColumnName="MONITORED_GRP_DISP_ID" columnDataType="VARCHAR2(200 CHAR)" tableName="ALERT_WF"/>
        <rollback>
            <renameColumn catalogName="${rec_owner}" oldColumnName="MONITORED_GRP_DISP_ID" newColumnName="MONITORED_GRP_ID" columnDataType="VARCHAR2(20 CHAR)" tableName="ALERT_WF"/>
        </rollback>
    </changeSet>

    // another change set

我正在尝试将列从 MONITORED_GRP_ID 重命名为 MONITORED_GRP_DISP_ID。

这是我的 application.properties 文件的样子:

spring.liquibase.change-log=classpath:sql/master.xml
spring.datasource.driver-class-name=${spring_datasource_driver-class-name:oracle.jdbc.driver.OracleDriver}
spring.datasource.url=${spring_datasource_url:jdbc:oracle:thin:@FRDAT3D.de.db.com:1825/FRDAT3D.de.db.com}
spring.datasource.username=${jdbc_username:}
spring.datasource.password=${spring_jdbc_password:}

spring.liquibase.enabled=${spring_liquibase_enabled:false}
liquibase.change-log=classpath:/db/changelog-profiles/changelog-master.xml

下面是申请-test.properties

database.based.client.key.enabled=${database_based_client_key_enabled:true}
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.username=test
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver   

spring.liquibase.enabled=true
spring.liquibase.change-log=classpath:/sql/master_owner.xml
db.owner.schema=
db.user.schema=

当我在我的 SpringBoot 应用程序上 运行 mvn clean install 时,出现以下错误:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-08-24 10:35:55.096 ERROR 24568 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibas
e.exception.MigrationFailedException: Migration failed for change set classpath:/sql/tables/MSG_MONITORED_GRP.xml::MSG_MONITORED_GRP::ALTER_MONITORED_GRP_NAME::sharakab:
     Reason: liquibase.exception.DatabaseException: Column "MONITORED_GRP_ID" not found; SQL statement:
ALTER TABLE PUBLIC.ALERT_WF ALTER COLUMN MONITORED_GRP_ID RENAME TO MONITORED_GRP_DISP_ID [42122-200] [Failed SQL: (42122) ALTER TABLE PUBLIC.ALERT_WF ALTER COLUMN MONITORED_GRP_ID RENAME TO MONITORED_GRP_DISP_ID]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1803) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=13=](AbstractBeanFactory.java:323) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
        at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:125) [spring-boot-test-2.2.1.RELEASE.jar:2.2.1.RELEASE]
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99) [spring-test-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) [spring-test-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123) [spring-test-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190) [spring-test-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132) [spring-test-5.2.1.RELEASE.jar:5.2.1.RELEASE]

请有人告诉我,我在这里缺少什么。当执行列名称更改的变更集时,spring 给出错误。

您使用名称 <createTable tableName="MSG_MONITORED_GRP" 创建了 table,但试图重命名 table <renameColumn tableName="ALERT_WF"/> 中的列。我猜是复制粘贴错误,DRY )