Liquibase creating databasechangelog twice in Spring project

Liquibase is creating two databasechangelog tables after run the spring project and throws the error “liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/20211112162058_changelog.xml”. It seems that liquibase is running multiple times and generating multiple tables (databasechangelog and database_changelog). Maybe it is causing some kind of conflict.

Screenshot from 2021-11-16 15-09-17

Can you share the contents of the 20211112162058_changelog.xml changelog, and also your configuration settings for liquibase?

1 Like

20211112162058_changelog.xml:

<?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:pro="http://www.liquibase.org/xml/ns/pro" 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/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-3.8.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
    <changeSet author="group (generated)" id="1636744886392-1">
        <createTable tableName="audit_event">
            <column autoIncrement="true" name="id" type="BIGINT">
                <constraints nullable="false" primaryKey="true" primaryKeyName="audit_eventPK"/>
            </column>
            <column name="uuid" type="UUID">
                <constraints nullable="false"/>
            </column>
            <column name="version" type="INTEGER"/>
            <column name="audit_event_date" type="TIMESTAMP WITHOUT TIME ZONE"/>
            <column name="audit_event_type" type="VARCHAR(255)"/>
            <column name="principal" type="VARCHAR(255)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet author="group (generated)" id="1636744886392-2">
        <createTable tableName="audit_event_data">
            <column name="id" type="BIGINT">
                <constraints nullable="false" primaryKey="true"/>
            </column>
            <column name="value" type="VARCHAR(255)"/>
            <column name="name" type="VARCHAR(255)">
                <constraints nullable="false" primaryKey="true"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet author="group (generated)" id="1636744886392-3">
        <createTable tableName="nota">
            <column autoIncrement="true" name="id" type="BIGINT">
                <constraints nullable="false" primaryKey="true" primaryKeyName="notaPK"/>
            </column>
            <column name="uuid" type="UUID">
                <constraints nullable="false"/>
            </column>
            <column name="version" type="INTEGER"/>
            <column name="chave" type="numeric(19, 2)">
                <constraints nullable="false"/>
            </column>
            <column name="data_autorizacao" type="date">
                <constraints nullable="false"/>
            </column>
            <column name="destinada" type="BOOLEAN">
                <constraints nullable="false"/>
            </column>
            <column name="destinatario" type="numeric(19, 2)">
                <constraints nullable="false"/>
            </column>
            <column name="emissao" type="date">
                <constraints nullable="false"/>
            </column>
            <column name="emitente" type="numeric(19, 2)">
                <constraints nullable="false"/>
            </column>
            <column name="id_integracao" type="numeric(19, 2)">
                <constraints nullable="false"/>
            </column>
            <column name="mensagem" type="VARCHAR(255)">
                <constraints nullable="false"/>
            </column>
            <column name="numero" type="numeric(19, 2)">
                <constraints nullable="false"/>
            </column>
            <column name="pdf" type="VARCHAR(255)">
                <constraints nullable="false"/>
            </column>
            <column name="protocolo" type="numeric(19, 2)">
                <constraints nullable="false"/>
            </column>
            <column name="serie" type="numeric(19, 2)">
                <constraints nullable="false"/>
            </column>
            <column name="status" type="VARCHAR(255)">
                <constraints nullable="false"/>
            </column>
            <column name="valor" type="numeric(19, 2)">
                <constraints nullable="false"/>
            </column>
            <column name="xml" type="VARCHAR(255)">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>
    <changeSet author="group (generated)" id="1636744886392-4">
        <createTable tableName="nota_aud">
            <column name="id" type="BIGINT">
                <constraints nullable="false" primaryKey="true" primaryKeyName="nota_audPK"/>
            </column>
            <column name="rev" type="INTEGER">
                <constraints nullable="false" primaryKey="true" primaryKeyName="nota_audPK"/>
            </column>
            <column name="revtype" type="SMALLINT"/>
            <column name="uuid" type="UUID"/>
            <column name="chave" type="numeric(19, 2)"/>
            <column name="data_autorizacao" type="date"/>
            <column name="destinada" type="BOOLEAN"/>
            <column name="destinatario" type="numeric(19, 2)"/>
            <column name="emissao" type="date"/>
            <column name="emitente" type="numeric(19, 2)"/>
            <column name="id_integracao" type="numeric(19, 2)"/>
            <column name="mensagem" type="VARCHAR(255)"/>
            <column name="numero" type="numeric(19, 2)"/>
            <column name="pdf" type="VARCHAR(255)"/>
            <column name="protocolo" type="numeric(19, 2)"/>
            <column name="serie" type="numeric(19, 2)"/>
            <column name="status" type="VARCHAR(255)"/>
            <column name="valor" type="numeric(19, 2)"/>
            <column name="xml" type="VARCHAR(255)"/>
        </createTable>
    </changeSet>
    <changeSet author="group (generated)" id="1636744886392-5">
        <createTable tableName="revinfo">
            <column autoIncrement="true" name="id" type="INTEGER">
                <constraints nullable="false" primaryKey="true" primaryKeyName="revinfoPK"/>
            </column>
            <column name="timestamp" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="ip" type="VARCHAR(255)"/>
            <column name="source" type="VARCHAR(255)"/>
            <column name="user_email" type="VARCHAR(255)"/>
            <column name="user_id" type="UUID"/>
            <column name="user_name" type="VARCHAR(255)"/>
        </createTable>
    </changeSet>
    <changeSet author="group (generated)" id="1636744886392-6">
        <addUniqueConstraint columnNames="uuid" constraintName="UC_AUDIT_EVENTUUID_COL" tableName="audit_event"/>
    </changeSet>
    <changeSet author="group (generated)" id="1636744886392-7">
        <addUniqueConstraint columnNames="uuid" constraintName="UC_NOTAUUID_COL" tableName="nota"/>
    </changeSet>
    <changeSet author="group (generated)" id="1636744886392-8">
        <addForeignKeyConstraint baseColumnNames="rev" baseTableName="nota_aud" constraintName="FK6se9i4akmepfes15wckhwywhf" deferrable="false" initiallyDeferred="false" referencedColumnNames="id" referencedTableName="revinfo" validate="true"/>
    </changeSet>
    <changeSet author="group (generated)" id="1636744886392-9">
        <addForeignKeyConstraint baseColumnNames="id" baseTableName="audit_event_data" constraintName="FKb5hwtx9vxk2e9u08k3wmymcgt" deferrable="false" initiallyDeferred="false" referencedColumnNames="id" referencedTableName="audit_event" validate="true"/>
    </changeSet>
</databaseChangeLog>

liquibase.gradle:

import groovy.xml.*


apply plugin: "org.liquibase.gradle"

dependencies {
    implementation 'org.postgresql:postgresql'
    implementation 'org.liquibase:liquibase-core'

    // implementation 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359'
    // implementation 'org.springframework.boot:spring-boot-starter-data-jpa:2.2.3.RELEASE'
    // implementation 'org.javassist:javassist:3.26.0-GA'
    // implementation 'javax.validation:validation-api:2.0.1.Final'

    liquibaseRuntime 'org.liquibase:liquibase-gradle-plugin:2.0.2'
    liquibaseRuntime 'org.liquibase:liquibase-core'
    liquibaseRuntime 'org.liquibase.ext:liquibase-hibernate5:3.8'
    liquibaseRuntime "org.postgresql:postgresql"
    liquibaseRuntime "com.h2database:h2"

    // liquibaseRuntime 'org.springframework.boot:spring-boot'
    // liquibaseRuntime 'org.springframework:spring-orm'
    // liquibaseRuntime 'org.springframework.boot:spring-boot-starter-data-jpa:2.2.3.RELEASE'
    // liquibaseRuntime 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359'
    // liquibaseRuntime 'org.javassist:javassist:3.26.0-GA'
    // liquibaseRuntime 'javax.validation:validation-api:2.0.1.Final'
    // liquibaseRuntime 'ch.qos.logback:logback-classic:1.2.3' // Fix for https://liquibase.jira.com/browse/CORE-3212
    // liquibaseRuntime sourceSets.main.output
    liquibaseRuntime sourceSets.main.compileClasspath
}

if (!project.hasProperty("runList")) {
    project.ext.runList = "main"
}

liquibase {
    activities {
        main {
            driver "org.postgresql.Driver"
            url "${liquibase_database_url}"
            username "${liquibase_database_username}"
            password "${liquibase_database_pwd}"
            changeLogFile "$projectDir/src/main/resources/db/changelog-master.xml"
            referenceUrl "${liquibase_reference_url}" +
                    "?dialect=" +
                    "org.hibernate.dialect.PostgreSQLDialect" +
                    "&hibernate.physical_naming_strategy=" +
                    "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy" +
                    "&hibernate.implicit_naming_strategy=" +
                    "org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy"
            databaseChangeLogTableName 'databasechangelog'
            databaseChangeLogLockTableName 'databasechangeloglock'
            defaultSchemaName "public"
            classpath "$projectDir/src/main/resources/"
        }
        diffLog {
            driver "org.postgresql.Driver"
            url "${liquibase_database_url}"
            username "${liquibase_database_username}"
            password "${liquibase_database_pwd}"
            if (!project.hasProperty("runDesc")) {
                changeLogFile "$projectDir/src/main/resources/db/changelog/" + new Date().format("yyyyMMddHHmmss") + "_changelog.xml"
            } else {
                changeLogFile "$projectDir/src/main/resources/db/changelog/" + new Date().format("yyyyMMddHHmmss") + "_" + runDesc + "_changelog.xml"

            }

            referenceUrl "${liquibase_reference_url}" +
                    "?dialect=" +
                    "org.hibernate.dialect.PostgreSQLDialect" +
                    "&hibernate.physical_naming_strategy=" +
                    "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy" +
                    "&hibernate.implicit_naming_strategy=" +
                    "org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy"
            defaultSchemaName "public"
            classpath "$buildDir/classes/java/main"
            databaseChangeLogTableName 'databasechangelog'
            databaseChangeLogLockTableName 'databasechangeloglock'
            excludeObjects "hibernate_sequence"
        }
    }

    runList = project.ext.runList
}

task liquibaseHelp(type: JavaExec) {
    group = "liquibase"
    classpath configurations.liquibaseRuntime
    main = "liquibase.integration.commandline.Main"
    args "--help"
}

task initMigrations() {
    group = 'Liquibase'
    description = 'Setting up migrations startup'

    doLast {
        def dbFolder = new File("$projectDir/src/main/resources/db")

        if (!dbFolder.exists()) {
            dbFolder.mkdirs()
            def changelogFolder = new File("$projectDir/src/main/resources/db/changelog")

            new File(dbFolder, 'changelog-master.xml').withWriterAppend { writer ->
                // Create MarkupBuilder with 4 space indent
                def xml = new MarkupBuilder(new IndentPrinter(writer, "    ", true))

                xml.doubleQuotes = true

                xml.mkp.xmlDeclaration(version: '1.0', encoding: 'utf-8', standalone: 'no')

                xml.databaseChangeLog('xmlns': 'http://www.liquibase.org/xml/ns/dbchangelog',
                        'xmlns:ext': 'http://www.liquibase.org/xml/ns/dbchangelog-ext',
                        'xmlns:pro': 'http://www.liquibase.org/xml/ns/pro',
                        '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/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-3.8.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd') {

                    mkp.yield('\r\n')
                    mkp.comment("AUTO-GENERATED FILE.  DO NOT MODIFY. changelog-master.xml is generated by the liquibase initMigrations gradle task")
                    mkp.yield('\r\n')

                    property(name: 'now', value: 'now()', dbms: 'h2')
                    property(name: 'now', value: 'current_timestamp', dbms: 'postgresql')
                    mkp.yield('\n')
                    property(name: 'floatType', value: 'float4', dbms: 'postgresql, h2')
                    property(name: 'floatType', value: 'float', dbms: 'mysql, oracle, mssql, mariadb')
                    property(name: 'clobType', value: 'longvarchar', dbms: 'h2')
                    property(name: 'clobType', value: 'clob', dbms: 'mysql, oracle, mssql, mariadb, postgresql')
                    property(name: 'uuidType', value: 'uuid', dbms: 'h2, postgresql')

                    mkp.yield('\n')
                    mkp.yield('\n    ')
                    mkp.comment('Database Changeset')
                    includeAll(path: 'db/changelog/', relativeToChangelogFile: 'false')

                    mkp.yield('\n')
                    mkp.yield('\n')
                    mkp.comment("AUTO-GENERATED FILE.  DO NOT MODIFY. changelog-master.xml is generated by the liquibase initMigrations gradle task")
                }
            }

            if (!changelogFolder.exists()) {

                changelogFolder.mkdirs()
                def initialSchemaChangelog = new File(changelogFolder, '00000000000000_initial_schema.xml')

                if (!initialSchemaChangelog.exists()) {
                    initialSchemaChangelog.withWriterAppend { writer ->
                        def xml = new MarkupBuilder(new IndentPrinter(writer, "    ", true))
                        xml.doubleQuotes = true
                        xml.mkp.xmlDeclaration(version: '1.0', encoding: 'utf-8', standalone: 'no')

                        xml.databaseChangeLog('xmlns': 'http://www.liquibase.org/xml/ns/dbchangelog',
                                'xmlns:ext': 'http://www.liquibase.org/xml/ns/dbchangelog-ext',
                                'xmlns:pro': 'http://www.liquibase.org/xml/ns/pro',
                                '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/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-3.8.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd') {

                            mkp.yield('\r\n')
                            mkp.comment("AUTO-GENERATED FILE.  DO NOT MODIFY. 00000000000000_initial_schema.xml is generated by the liquibase initMigrations gradle task")
                            mkp.yield('\n')
                            mkp.yield('\n    ')
                            mkp.comment('Hibernate Envers')
                            changeSet(author: 'carlos.perche', id: '00000000000000-1') {
                                createSequence(sequenceName: 'hibernate_sequence')
                            }
                            mkp.yield('\n')
                            mkp.yield('\n')
                            mkp.comment("AUTO-GENERATED FILE.  DO NOT MODIFY. 00000000000000_initial_schema.xml is generated by the liquibase initMigrations gradle task")
                        }
                    }
                }
            }
        } else {
            logger.quiet("[Migrations have already been started]")
        }

    }
}

task makeMigrations(type: Exec) {
    doFirst {
        println '>>> make migrations'
    }
    group = 'Liquibase'
    description = 'Produces a migration'
    workingDir "${rootDir}"
    commandLine './gradlew', 'diffChangelog', '-PrunList=diffLog'
}

task migrate() {
    doFirst {
        println 'migrate'
    }
}

initMigrations {
    dependsOn liquibase: dropAll
    finalizedBy makeMigrations
}

makeMigrations {
    dependsOn clean
    dependsOn compileJava
    finalizedBy validate
}

migrate {
    dependsOn validate
    finalizedBy diff
    finalizedBy update
}

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

gradle.properties:

profile=dev
# Spring-boot version should match the one managed by
spring_boot_version=2.2.7.RELEASE
spring_dependency_management_version=1.0.9.RELEASE
# Gradle plugin version
dependency_check_gradle_version=5.3.1
spring_javaformat_gradle_plugin_version=0.0.20
jib_plugin_version=2.3.0
liquibase_plugin_version=2.0.4
liquibase_hibernate5_version=3.8
gradle_node_plugin_version=2.2.4
apt_plugin_version=0.21
checkstyle_version=8.32
sonarqube_plugin_version=2.8
git_properties_plugin_version=2.2.2
keycloak_version=11.0.2
spring_no_http_plugin_version=0.0.4.RELEASE
jaxb_runtime_version=2.3.3

#liquibase
liquibase_reference_url=hibernate:spring:br.com.groupsoftware.core.jpa.domain,br.com.groupsoftware.integradornf.domain
liquibase_database_url=jdbc:postgresql://localhost:5432/integrador_nf
liquibase_database_username=postgres
liquibase_database_pwd=postgres