Generate Changelog with Liquibase and JPA Entities

2019-08-01 15:42发布

问题:

I would like to introduce liquibase to my project and first of all I would like to generate a changelog file according my hibernate entities.

What I have so far:

application.properties

# HIBERNATE
# Write out schema into sql script
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=target/schema-h2.sql
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect

# DATABASE
spring.datasource.platform=h2
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true

# LIQUIBASE
spring.liquibase.enabled=true
spring.liquibase.url=jdbc:h2:mem:testdb
spring.liquibase.user=sa
spring.liquibase.password=
spring.liquibase.changeLog=classpath:/db/h2/liquibase-changelog-master.xml

pom.xml

<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>${liquibase.version}</version>
</dependency>

...

<plugins>
    <plugin>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-maven-plugin</artifactId>
        <version>${liquibase.version}</version>
        <configuration>                  
            <changeLogFile>s/m/r/db/h2/liquibase-changelog-master.xml</changeLogFile>
            <diffChangeLogFile> s/m/r/db/h2/changelog/${timestamp}_changelog.xml</diffChangeLogFile>
            <outputChangeLogFile> s/m/r/db/h2/changelog/myaddon.db.changelog.xml</outputChangeLogFile>
            <driver>org.h2.Driver</driver>
            <url>jdbc:h2:mem:testdb</url>
            <username>sa</username>
            <password></password>
            <referenceUrl>hibernate:spring:ch.hemisoft.immo.domain?hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy</referenceUrl>
            <verbose>true</verbose>
            <logging>trace</logging>
        </configuration>
        <dependencies>
            <dependency>
               <groupId>org.liquibase.ext</groupId>
               <artifactId>liquibase-hibernate5</artifactId>
               <version>${liquibase-hibernate5.version}</version>
            </dependency>
            <dependency>
               <groupId>org.liquibase</groupId>
               <artifactId>liquibase-core</artifactId>
               <version>${liquibase.version}</version>
            </dependency>
            <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-beans</artifactId>
               <version>${spring.version}</version>
            </dependency>
            <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-core</artifactId>
               <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
                <version>2.1.4.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>javax.validation</groupId>
                <artifactId>validation-api</artifactId>
                <version>${validation-api.version}</version>
            </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <version>${h2.version}</version>
            </dependency>
            <dependency>
               <groupId>org.javassist</groupId>
               <artifactId>javassist</artifactId>
               <version>${javassist.version}</version>
            </dependency>
        </dependencies>            
    </plugin> 
</plugins>

When trying to generate the changelog file using mvn liquibase:generateChangeLog then I get something like:

// A big list of artifacts ...

[INFO] Starte Liquibase am So, 28 Apr 2019 19:09:24 MESZ (Version 3.6.3, kompiliert am 2019-01-29 11:34:48)
[INFO] Settings
----------------------------
[INFO]     driver: org.h2.Driver
[INFO]     url: jdbc:h2:mem:testdb
[INFO]     username: sa
[INFO]     password: *****
[INFO]     use empty password: false
[INFO]     properties file: null
[INFO]     properties file will override? false
[INFO]     prompt on non-local database? true
[INFO]     clear checksums? false
[INFO]     defaultSchemaName: null
[INFO]     diffTypes: null
[INFO]     dataDir: null
[INFO] ------------------------------------------------------------------------
[INFO] Executing on Database: jdbc:h2:mem:testdb
[INFO] Loading artifacts into URLClassLoader

// A big list of artifacts ...

[INFO] Generating Change Log from database SA @ jdbc:h2:mem:testdb (Default Schema: PUBLIC)
[INFO] SELECT SEQUENCE_NAME FROM INFORMATION_SCHEMA.SEQUENCES WHERE SEQUENCE_SCHEMA = 'PUBLIC' AND IS_GENERATED=FALSE
[INFO] src\main\resources\db\h2\changelog\myaddon.db.changelog.xml exists, appending
[INFO] No changes found, nothing to do
[INFO] Output written to Change Log file, src/main/resources/db/h2/changelog/myaddon.db.changelog.xml
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.161 s
[INFO] Finished at: 2019-04-28T19:09:26+02:00
[INFO] ------------------------------------------------------------------------

As you see, it says No changes found, nothing to do, but I would expect having my first changelog with initial table creations.

What I also see, it seems it tries to create a changelog file from the empty database instead from hibernate classes:

Generating Change Log from database SA @ jdbc:h2:mem:testdb (Default Schema: PUBLIC)

The output file myaddon.db.changelog.xml looks as following:

<?xml version="1.1" 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.6.xsd" />

Thanks for helping.

回答1:

If you want to generate changelog starting with your hibernate entities you can use

mvn liquibase:diff

first you need to add dialect to your referenceUrl (missing in your configuration)

<referenceUrl>hibernate:spring:ch.hemisoft.immo.domain?hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&amp;dialect=org.hibernate.dialect.H2Dialect</referenceUrl>