Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.grails.plugins.databasemigration

import groovy.transform.CompileStatic
import org.springframework.boot.autoconfigure.AutoConfigurationImportFilter
import org.springframework.boot.autoconfigure.AutoConfigurationMetadata

/**
* Automatically excludes Spring Boot's {@code LiquibaseAutoConfiguration} when the
* Grails Database Migration Plugin is on the classpath.
*
* <p>Spring Boot auto-configures its own Liquibase instance via
* {@code LiquibaseAutoConfiguration}, which creates a {@code SpringLiquibase} bean
* that conflicts with the plugin's own {@link DatabaseMigrationGrailsPlugin#doWithApplicationContext()}
* lifecycle management. Without this exclusion, both Spring Boot and the plugin
* attempt to run Liquibase migrations, causing duplicate changelog execution,
* lock contention on the {@code DATABASECHANGELOGLOCK} table, or startup failures.</p>
*
* <p>Previously, users had to manually add {@code spring.liquibase.enabled: false}
* to {@code application.yml}. This filter removes that requirement by automatically
* filtering out the conflicting auto-configuration during Spring Boot's
* auto-configuration discovery phase.</p>
*
* <p>Registered via {@code META-INF/spring.factories} as an
* {@link AutoConfigurationImportFilter}. This runs before auto-configuration
* bytecode is loaded, so there is no performance overhead from excluded classes.</p>
*
* @since 7.0.8
* @see AutoConfigurationImportFilter
* @see DatabaseMigrationGrailsPlugin
*/
@CompileStatic
class LiquibaseAutoConfigurationExcluder implements AutoConfigurationImportFilter {

/**
* Spring Boot Liquibase auto-configuration class that conflicts with the
* Grails Database Migration Plugin. Excluded unconditionally when the
* plugin is on the classpath.
*/
private static final Set<String> EXCLUDED_AUTO_CONFIGURATIONS = [
'org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration',
] as Set<String>

@Override
boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata autoConfigurationMetadata) {
boolean[] matches = new boolean[autoConfigurationClasses.length]
for (int i = 0; i < autoConfigurationClasses.length; i++) {
matches[i] = !EXCLUDED_AUTO_CONFIGURATIONS.contains(autoConfigurationClasses[i])
}
return matches
}

/**
* Returns the set of auto-configuration class names that this filter excludes.
* Exposed for testing and diagnostic purposes.
*
* @return unmodifiable set of excluded class names
*/
static Set<String> getExcludedAutoConfigurations() {
return Collections.unmodifiableSet(EXCLUDED_AUTO_CONFIGURATIONS)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Automatically exclude Spring Boot's Liquibase auto-configuration that conflicts
# with the Grails Database Migration Plugin's own Liquibase lifecycle management.
# See: LiquibaseAutoConfigurationExcluder javadoc for details.
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
org.grails.plugins.databasemigration.LiquibaseAutoConfigurationExcluder
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.grails.plugins.databasemigration

import org.springframework.boot.autoconfigure.AutoConfigurationImportFilter
import org.springframework.boot.autoconfigure.AutoConfigurationMetadata
import org.springframework.core.io.support.SpringFactoriesLoader
import spock.lang.Specification

class LiquibaseAutoConfigurationExcluderSpec extends Specification {

LiquibaseAutoConfigurationExcluder excluder = new LiquibaseAutoConfigurationExcluder()

def "excludes LiquibaseAutoConfiguration"() {
given:
String[] candidates = [
'org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration',
'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration',
'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration',
] as String[]

when:
boolean[] matches = excluder.match(candidates, Stub(AutoConfigurationMetadata))

then:
!matches[0] // LiquibaseAutoConfiguration excluded
matches[1] // DataSourceAutoConfiguration allowed
matches[2] // HibernateJpaAutoConfiguration allowed
}

def "allows all non-Liquibase auto-configurations"() {
given:
String[] candidates = [
'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration',
'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration',
'org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration',
] as String[]

when:
boolean[] matches = excluder.match(candidates, Stub(AutoConfigurationMetadata))

then:
matches.every { it }
}

def "handles empty candidate list"() {
given:
String[] candidates = [] as String[]

when:
boolean[] matches = excluder.match(candidates, Stub(AutoConfigurationMetadata))

then:
matches.length == 0
}

def "handles null entries in candidate list"() {
given:
String[] candidates = [null, 'org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration', null] as String[]

when:
boolean[] matches = excluder.match(candidates, Stub(AutoConfigurationMetadata))

then:
matches[0] // null is not in excluded set, so allowed
!matches[1] // LiquibaseAutoConfiguration excluded
matches[2] // null allowed
}

def "excluded set contains exactly LiquibaseAutoConfiguration"() {
expect:
LiquibaseAutoConfigurationExcluder.excludedAutoConfigurations == [
'org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration',
] as Set
}

def "excluded set is unmodifiable"() {
when:
LiquibaseAutoConfigurationExcluder.excludedAutoConfigurations.add('something')

then:
thrown(UnsupportedOperationException)
}

def "registered in spring.factories as AutoConfigurationImportFilter"() {
when:
List<AutoConfigurationImportFilter> filters = SpringFactoriesLoader.loadFactories(
AutoConfigurationImportFilter, getClass().classLoader
)

then:
filters.any { it instanceof LiquibaseAutoConfigurationExcluder }
}
}
Loading