1

I am trying to use my liquibase scripts to set up tables in H2 database for my unit tests in springboot. Below are the liquibase scripts for one of my tables

<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

I am trying to rename the column from MONITORED_GRP_ID to MONITORED_GRP_DISP_ID.

Here is how my application.properties file look like:

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

Below is application-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=

When I run mvn clean install on my SpringBoot app i get the below error:

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$0(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]

Please can someone tell, what am I missing here. When the changeset for column name change is executed, spring gives error.

Akash Sharma
  • 330
  • 3
  • 18

1 Answers1

2

you created table with name <createTable tableName="MSG_MONITORED_GRP", but trying to rename column in table <renameColumn tableName="ALERT_WF"/>. I guess it is copy-paste error, DRY )

borino
  • 1,720
  • 1
  • 16
  • 24