如何在Spring Boot应用程序中配置liquibase以生成具有@Entity和数据库之间差异的文件?

问题描述 投票:0回答:1

我想使用命令 mvn liquibase:generateChangeLog 或 mvn liquibase:dif 生成实体中的更改,但我得到空文件。
我想生成允许我获取数据库和实体之间的更改的文件。有什么想法吗?
在我的 application.properties 中:
spring.datasource.url= jdbc:postgresql://localhost:5432/db
spring.datasource.username= 用户名
spring.datasource.password= pswd

spring.messages.basename=消息
spring.messages.cache-duration=3600

spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=validate

Liquibase 配置

spring.liquibase.enabled=true
spring.liquibase.change-log=classpath:db/changelog/db.changelog.yml
在我的 pom.xml 中:


    <dependency>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-core</artifactId>
                <version>4.25.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.liquibase.ext</groupId>
                <artifactId>liquibase-hibernate6</artifactId>
                <version>4.25.0</version>
            </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>4.25.0</version>
                    <configuration>
                        <propertyFile>src/main/resources/db/changelog/liquibase.properties</propertyFile>
    <!--                    <verbose>true</verbose>-->
                        <outputChangeLogFile>src/main/resources/db/changelog/db-changelog-master.yml</outputChangeLogFile>
    <!--                    <changeLogFile>src/main/resources/db/changelog/db-changelog-master.yml</changeLogFile>-->
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>generateChangeLog</goal>
                                <goal>update</goal>
                                <goal>diff</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>```
my entity : 

    ``` @Entity
    @Getter
    @Setter
    @Table(name = "test_user")
    public class TestUser {
    
        @Id
        @Column(name = "id")
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        @Column(name = "username")
        private String userName;
    
        @Column(name = "first_name")
        private String firstName;
    
        @Column(name = "last_name")
        private String lastName;
    
        @Column(name = "email")
        private String email;
    }```


java spring-boot spring-data-jpa liquibase
1个回答
0
投票

为了生成实体与数据库之间的变更日志差异。您必须使用扩展名

liquibase-hibernate5
(如果您使用的是 Hibernate 5)或
liquibase-hibernate6

然后在

referenceUrl
中配置源 (
liquibase.properties
) 来包含实体,目标 (
url
) 指向数据库的当前状态。

以下是

liquibase-maven-plugin
的配置(仅
<build>
标签的部分):

    <build>
        <plugins>
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>${liquibase.version}</version>
                <configuration>
                    <propertyFile>${liquibase.propertyFile}</propertyFile>
                    <outputChangeLogFile>
                        ${project.basedir}/src/main/resources/db/changelog/init-changelog.xml
                    </outputChangeLogFile>
                    <diffChangeLogFile>
                        ${project.basedir}/src/main/resources/db/changelog/${maven.build.timestamp}-changelog.xml
                    </diffChangeLogFile>
                    <systemProperties>
                        <user.name>nthung.vlvn</user.name>
                    </systemProperties>
                    <changeLogDirectory>${project.basedir}/src/main/resources/db</changeLogDirectory>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.liquibase.ext</groupId>
                        <artifactId>liquibase-hibernate5</artifactId>
                        <version>${liquibase-hibernate.version}</version>
                    </dependency>

                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-data-jpa</artifactId>
                        <version>${boot.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

我使用一些属性/变量,它们将在

<properties>
块上声明,如下所示:

    <properties>
        <liquibase.propertyFile>liquibase.properties</liquibase.propertyFile>
        <liquibase.changeLogFile>/db/changelog/db.changelog-master.xml</liquibase.changeLogFile>
        <liquibase.changeSetAuthor>nthung.vlvn</liquibase.changeSetAuthor>
        <liquibase.rollbackCount>1</liquibase.rollbackCount>
        <liquibase.verbose>false</liquibase.verbose>
        <liquibase-hibernate.version>${liquibase.version}</liquibase-hibernate.version>
        <maven.build.timestamp.format>yyyy/MM/dd-HHmm</maven.build.timestamp.format>
    </properties>

重要的部分是指定

url
到目标(数据库)和
referenceUrl
到实体包。

下面是

url
指向 PostgreSQL 的示例。

url=jdbc:postgresql://<db_host>:5432/<db_name>
username=<your-db-username>
password=<your-db-password>

referenceURL=hibernate:spring:<your-fqdn-entity-package>?dialect=org.hibernate.dialect.PostgreSQL10Dialect&hibernate.physical_naming_strategy=org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy

您必须更换

<db_host>
<db_name>
<your-db-username>
<your-db-username>
(即:
net.ddns.vlvn_lab.entity

有关配置的更多详细信息

referenceUrl
,您可以参考官方文档:Liquibase Hibernate Extension Wiki

© www.soinside.com 2019 - 2024. All rights reserved.