liquibase diff 不起作用

Posted

技术标签:

【中文标题】liquibase diff 不起作用【英文标题】:liquibase diff does not works 【发布时间】:2020-05-01 23:19:54 【问题描述】:

我正在尝试使用 Hibernate 和 Liquibase 使用 Code-First 方法制作 Spring Boot 2.2.x 应用程序 (Java 13)。我想根据实体对象和数据库之间的差异生成迁移。

操作系统 - Win 10 和 Mint 18 构建系统 - gradle 6.0。 数据库 - Postgres SQL。

我安装了 liquibase 实用程序 (3.8.2)。

我的 gradle 脚本:

    repositories 
        maven 
            url "https://plugins.gradle.org/m2/"
        
    
    dependencies 
        classpath 'net.ltgt.gradle:gradle-apt-plugin:0.18'
        classpath 'org.postgresql:postgresql:42.2.9'
        classpath 'org.liquibase:liquibase-core:3.8.2'
        classpath "org.liquibase:liquibase-gradle-plugin:2.0.2"
    


plugins 
    id 'org.springframework.boot' version '2.2.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'org.liquibase.gradle' version '2.0.2'
    id 'java'



group = 'com.goodt.drive'
version = '0.0.1-SNAPSHOT'

sourceCompatibility = '13'
targetCompatibility = '13'


diff.dependsOn compileJava
diffChangeLog.dependsOn compileJava
generateChangelog.dependsOn compileJava

dependencies 

    liquibaseRuntime 'org.liquibase:liquibase-core:3.8.2'
    liquibaseRuntime "jakarta.xml.bind:jakarta.xml.bind-api:2.3.2"
    liquibaseRuntime 'org.springframework.boot:spring-boot:2.2.1.RELEASE'
    liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:2.0.1'
    liquibaseRuntime 'org.postgresql:postgresql:42.2.9'
    liquibaseRuntime 'org.hibernate:hibernate-core:5.4.10.Final'
    liquibaseRuntime 'ch.qos.logback:logback-core:1.2.3'
    liquibaseRuntime 'ch.qos.logback:logback-classic:1.2.3'
    liquibaseRuntime sourceSets.main.output 



def dbChangeLog = "$projectDir/src/main/resources/db/changelog/changelog.xml"
def generatedChangeLog = "$projectDir/src/main/resources/db/changelog/generated_changelog.xml"


liquibase 
    activities 
        main 
            changeLogFile dbChangeLog
            outputFile generatedChangeLog
            driver "org.postgresql.Driver"
            classpath "$projectDir/lib/postgresql-42.2.9.jar"
            url "jdbc:postgresql://localhost:5432/webportal"
            username "developer"
            password "123"
            referenceUrl "hibernate:spring:com.wissance.webportal.application.model.entities?dialect=org.hibernate.dialect.PostgreSQL94Dialect"
            //referenceDriver 'liquibase.ext.hibernate.database.connection.HibernateDriver'
        
    

当我像这样从 gradle 脚本运行 liquibase diff 命令时:.\gradlew.bat diff 我得到以下输出:

> Task :diff
liquibase-plugin: Running the 'main' activity...
12:48:14.372 INFO  [liquibase.integration.commandline.Main]: Starting Liquibase at ёЁ, 15  эт. 2020 12:48:14 YEKT (version 3.8.2 #26 built at Tue Nov 26 04:53:39 UTC 2019)
12:48:15.323 INFO  [liquibase.integration.commandline.Main]: No Liquibase Pro license key supplied. Please set liquibaseProLicenseKey on command line or in liquibase.properties to use Liquibase Pro features.
12:48:15.325 INFO  [liquibase.integration.commandline.Main]: Liquibase Community 3.8.2 by Datical
12:48:15.614 ERROR [liquibase.integration.commandline.Main]: Unexpected error running Liquibase: java.lang.RuntimeException: Cannot find database driver: Driver class was not specified and could not be determined from the url (hibernate:spring:com.wissance.webportal.application.model.entities?dialect=org.hibernate.dialect.PostgreSQL94Dialect)
liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: java.lang.RuntimeException: Cannot find database driver: Driver class was not specified and could not be determined from the url (hibernate:spring:com.wissance.webportal.application.model.entities?dialect=org.hibernate.dialect.PostgreSQL94Dialect)
        at liquibase.integration.commandline.CommandLineUtils.createDatabaseObject(CommandLineUtils.java:132)
        at liquibase.integration.commandline.Main.createReferenceDatabaseFromCommandParams(Main.java:1604)
        at liquibase.integration.commandline.Main.doMigration(Main.java:1200)
        at liquibase.integration.commandline.Main.run(Main.java:229)
        at liquibase.integration.commandline.Main.main(Main.java:143)
Caused by: liquibase.exception.DatabaseException: java.lang.RuntimeException: Cannot find database driver: Driver class was not specified and could not be determined from the url (hibernate:spring:com.wissance.webportal.application.model.entities?dialect=org.hibernate.dialect.PostgreSQL94Dialect)
        at liquibase.database.DatabaseFactory.openConnection(DatabaseFactory.java:263)
        at liquibase.database.DatabaseFactory.openDatabase(DatabaseFactory.java:149)
        at liquibase.integration.commandline.CommandLineUtils.createDatabaseObject(CommandLineUtils.java:97)
        ... 4 common frames omitted
Caused by: java.lang.RuntimeException: Cannot find database driver: Driver class was not specified and could not be determined from the url (hibernate:spring:com.wissance.webportal.application.model.entities?dialect=org.hibernate.dialect.PostgreSQL94Dialect)
        at liquibase.database.DatabaseFactory.openConnection(DatabaseFactory.java:200)
        ... 6 common frames omitted

【问题讨论】:

【参考方案1】:

堆栈跟踪表明 Liquibase Cannot find database driverDriver class was not specified and could not be determined from the url (hibernate:spring:com.wissance.webportal.application.model.entities?dialect=org.hibernate.dialect.PostgreSQL94Dialect)

这表明您应该将 liquibase-hibernate jar 添加到您的依赖项中,并取消注释 gradle 脚本中的 referenceDriver 行。

【讨论】:

谢谢,史蒂夫。但这不仅可以帮助我使 diff 正常运行,而且我会在下面发布解决方案,因为我不能在这里发布。【参考方案2】:
    我将 liquibase(在 gradle 中)更新到了 3.8.4 版 我添加了带有 liquibase-hibernate 和 spring-data-jpa 的 liquibaseRuntime 行 我已添加到 gradle 脚本的 liquibase-hibernate 和 spring-data-jpa 类路径 部分。

另外,我写了一篇关于如何使用 liquibase 的文章,你可以在这里找到它:https://m-ushakov.medium.com/code-first-with-spring-boot-hibernate-and-liquibase-48f5c9998d95

整个 Gradle 脚本:

buildscript 
    repositories 
        maven 
            url "https://plugins.gradle.org/m2/"
        
    
    dependencies 
        classpath 'net.ltgt.gradle:gradle-apt-plugin:0.18'
        classpath 'org.postgresql:postgresql:42.2.9'
        classpath 'org.liquibase.ext:liquibase-hibernate5:3.8'
        classpath 'org.liquibase:liquibase-core:3.8.4'
        classpath 'org.liquibase:liquibase-gradle-plugin:2.0.2'
        classpath 'org.springframework.data:spring-data-jpa:2.2.1.RELEASE'
    


plugins 
    id 'org.springframework.boot' version '2.2.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'org.liquibase.gradle' version '2.0.2'
    id 'java'


group = 'com.wissance.webportal'
version = '0.0.1-SNAPSHOT'

sourceCompatibility = '13'
targetCompatibility = '13'

ext 
    set('springCloudVersion', "Hoxton.RC2")
    set('queryDslVersion',    "4.1.3")
    set('swaggerVersion',     "2.9.2")



configurations 
    compileOnly 
        extendsFrom annotationProcessor
    


repositories 
    mavenCentral()
    maven  url 'https://repo.spring.io/milestone' 


diff.dependsOn compileJava
diffChangeLog.dependsOn compileJava
generateChangelog.dependsOn compileJava

dependencies 
    // spring boot
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'org.springframework.boot:spring-boot-starter-hateoas'
    compile 'org.springframework.boot:spring-boot-starter-web'

    // spring cloud
    //implementation 'org.springframework.cloud:spring-cloud-starter-config'
    //implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    
    // JAX-B dependencies for JDK 9+
    implementation "jakarta.xml.bind:jakarta.xml.bind-api:2.3.2"
    implementation "org.glassfish.jaxb:jaxb-runtime:2.3.2"
    
    // other
    compile 'org.postgresql:postgresql:42.2.1'
    compile 'com.h2database:h2'
    compile 'org.hibernate:hibernate-core:5.4.10.Final'
    compileOnly 'org.projectlombok:lombok'
    
    //runtimeOnly 'org.postgresql:postgresql'
    
    runtime 'javax.xml.bind:jaxb-api'
    //runtime 'org.liquibase:liquibase-core'
    liquibaseRuntime 'org.liquibase:liquibase-core:3.8.4'
    liquibaseRuntime "jakarta.xml.bind:jakarta.xml.bind-api:2.3.2"
    liquibaseRuntime 'org.springframework.boot:spring-boot:2.2.1.RELEASE'
    liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:2.0.1'
    liquibaseRuntime 'org.postgresql:postgresql:42.2.9'
    liquibaseRuntime 'org.springframework.data:spring-data-jpa:2.2.1.RELEASE'
    liquibaseRuntime 'org.hibernate:hibernate-core:5.4.10.Final'
    liquibaseRuntime 'org.liquibase.ext:liquibase-hibernate5:3.8'
    liquibaseRuntime 'ch.qos.logback:logback-core:1.2.3'
    liquibaseRuntime 'ch.qos.logback:logback-classic:1.2.3'
    liquibaseRuntime sourceSets.main.output 

    // TESTS
    //testImplementation('org.springframework.boot:spring-boot-starter-test') 
    //    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    //
    
    testCompile('org.junit.jupiter:junit-jupiter-engine:5.2.0')

    // QueryDsl
    compile "com.querydsl:querydsl-core:$queryDslVersion"
    compile "com.querydsl:querydsl-jpa:$queryDslVersion"

    // Swagger
    compile "io.springfox:springfox-swagger2:$swaggerVersion"
    compile "io.springfox:springfox-swagger-ui:$swaggerVersion"

    annotationProcessor (
        "org.projectlombok:lombok",
        "com.querydsl:querydsl-apt:$queryDslVersion:jpa",
        "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final",
        "javax.annotation:javax.annotation-api:1.3.2"
    )


dependencyManagement 
    imports 
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:$springCloudVersion"
    


def dbChangeLog = "$projectDir/src/main/resources/db/changelog/changelog.xml"
def generatedChangeLog = "$projectDir/src/main/resources/db/changelog/generated_changelog.xml"

/*task copyToLib(type: Copy) 
    into "$buildDir/output/libs"
    from configurations.runtime
*/

liquibase 
    activities 
        main 
            changeLogFile dbChangeLog
            outputFile generatedChangeLog
            driver "org.postgresql.Driver"
            // classpath "$projectDir/lib/postgresql-42.2.9.jar"
            url "jdbc:postgresql://localhost:5432/webportal
            password "123"
            referenceUrl "hibernate:spring:com.wissance.webportal.application.model.entities?dialect=org.hibernate.dialect.PostgreSQL94Dialect"
            referenceDriver 'liquibase.ext.hibernate.database.connection.HibernateDriver'
        
    


jar 
    enabled = true
    manifest 
        attributes 'Main-Class': 'com.wissance.webportal.Application'
    


test 
    useJUnitPlatform()

【讨论】:

【参考方案3】:

不确定这是否与您遇到的错误有关,但 Liquibase 最近发布了一个 3.8.4 版本,该版本纠正了 Java 9+ 中的一些 Spring Boot 错误。 https://www.liquibase.org/2019/12/liquibase-3-8-4-released.html

我会尝试运行它,看看它是否可以为您解决问题。

【讨论】:

我刚刚检查了 3.8.4 liquibase 版本并得到了相同的行为。但是,如果您检查一下您的环境,我将不胜感激(也许我做错了什么)。谢谢。

以上是关于liquibase diff 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

使用 Spring-Boot 启动时的 Liquibase 迁移不起作用

生成 forIndexName 的 Liquibase 差异在 h2 上不起作用

Liquibase / Spring Boot / Postgres - 模式名称不起作用

Grails 数据库迁移 - dbm-gorm-diff 不起作用

设置为 diff-highlight 的 Git 寻呼机在 Windows 10 中不起作用

H2(版本 2.0.202)auto_increment 不起作用