Hi everyone,
I need to add liquibase to an existing spring-boot project for my job, on a MYSQL database.
I followed this tutorial : https://www.baeldung.com/liquibase-refactor-schema-of-java-app
I simply ran an mvn liquibase:generateChangelog
command to generate the init changelog of my database.
Once it was done :
I tried to run “mvn liquibase:update” on command line, and everything was working fine, with this messages :
But when I try to run my app with the auto-update enabled with the mvn spring-boot:run
command, I face this error :
Caused by: liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/xml/changelog_1.xml::1611571499468-1::W100553 (generated):
Reason: liquibase.exception.DatabaseException: Table 'alert' already exists [Failed SQL: (1050) CREATE TABLE alerttest.alert ...
I don’t understand why since everything work fine when I run an update on CLI.
Here is my pom :
<?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 https://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.4.1</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>com.example.testliquibase</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot with liquibase</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<propertyFile>src/main/resources/db/changelog/application.yml</propertyFile>
<changeLogFile>src/main/resources/db/changelog/changelog_master.xml</changeLogFile>
<driver>com.mysql.cj.jdbc.Driver</driver>
</configuration>
</plugin>
</plugins>
</build>
</project>
Here is my application.properties :
url=jdbc:mysql://localhost:3306/alerttest?zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC
username=
password=
spring.liquibase.change-log=classpath:db/changelog/changelog_master.xml
spring.liquibase.enabled=true
logging.level.web=DEBUG
spring.datasource.url=jdbc:mysql://localhost:3306/alerttest?zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC
spring.datasource.username=
spring.datasource.password=
And finally, my changelog master :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
<include file="xml/changelog_1.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
Thanks for your help, I have no clue how to solve this…