-
Notifications
You must be signed in to change notification settings - Fork 41.7k
Support a generalized way to configure Liquibase global properties through Spring Boot configuration #47289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Support a generalized way to configure Liquibase global properties through Spring Boot configuration #47289
Changes from all commits
0545d3a
9ffd144
8664d53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Copyright 2012-present the original author or authors. | ||
| * | ||
| * Licensed 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.springframework.boot.liquibase.autoconfigure; | ||
|
|
||
| import liquibase.Scope; | ||
| import liquibase.exception.LiquibaseException; | ||
| import liquibase.integration.spring.SpringLiquibase; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.springframework.beans.BeansException; | ||
| import org.springframework.beans.factory.DisposableBean; | ||
| import org.springframework.context.ApplicationContext; | ||
| import org.springframework.context.ApplicationContextAware; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * A {@link SpringLiquibase} subclass that creates a Liquibase child scope with a | ||
| * reference to the Spring Environment, allowing the | ||
| * {@link EnvironmentConfigurationValueProvider} to access the correct Environment for | ||
| * configuration values. | ||
| * | ||
| * @author Dylan Miska | ||
| */ | ||
| class EnvironmentAwareSpringLiquibase extends SpringLiquibase implements ApplicationContextAware, DisposableBean { | ||
|
|
||
| private @Nullable String environmentId; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not supposed to be nullable. I know that |
||
|
|
||
| @Override | ||
| public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
| this.environmentId = applicationContext.getId(); | ||
| } | ||
|
|
||
| @Override | ||
| public void afterPropertiesSet() throws LiquibaseException { | ||
| try { | ||
| Map<String, Object> scopeValues = new HashMap<>(); | ||
| scopeValues.put(EnvironmentConfigurationValueProvider.SPRING_ENV_ID_KEY, this.environmentId); | ||
| Scope.child(scopeValues, EnvironmentAwareSpringLiquibase.super::afterPropertiesSet); | ||
| } | ||
| catch (Exception ex) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can have two catch blocks here |
||
| if (ex instanceof LiquibaseException) { | ||
| throw (LiquibaseException) ex; | ||
| } | ||
| throw new LiquibaseException(ex); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void destroy() { | ||
| if (this.environmentId != null) { | ||
| EnvironmentConfigurationValueProvider.unregisterEnvironment(this.environmentId); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really would like that the responsability of this was in Liquibase's scope, and not here. |
||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| * Copyright 2012-present the original author or authors. | ||
| * | ||
| * Licensed 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.springframework.boot.liquibase.autoconfigure; | ||
|
|
||
| import liquibase.configuration.AbstractConfigurationValueProvider; | ||
| import liquibase.configuration.ProvidedValue; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.springframework.core.env.Environment; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| /** | ||
| * A Liquibase {@code ConfigurationValueProvider} that passes through properties defined | ||
| * in the Spring {@link Environment} using the exact-name convention: | ||
| * | ||
| * <pre> | ||
| * spring.liquibase.properties.<liquibaseKey> = <value> | ||
| * </pre> | ||
| * | ||
| * For example: <pre> | ||
| * spring.liquibase.properties.liquibase.duplicateFileMode = WARN | ||
| * spring.liquibase.properties.liquibase.searchPath = classpath:/db,file:./external | ||
| * </pre> | ||
| * | ||
| * No relaxed binding or key transformation is performed. Keys are looked up exactly as | ||
| * provided by Liquibase (including dots and casing), prefixed with | ||
| * {@code spring.liquibase.properties.}. | ||
| * | ||
| * @author Dylan Miska | ||
| */ | ||
| public final class EnvironmentConfigurationValueProvider extends AbstractConfigurationValueProvider { | ||
|
|
||
| private static final String PREFIX = "spring.liquibase.properties."; | ||
|
|
||
| public static final String SPRING_ENV_ID_KEY = "spring.environment.id"; | ||
|
|
||
| private static final Map<String, Environment> environments = new ConcurrentHashMap<>(); | ||
|
|
||
| static void registerEnvironment(String environmentId, Environment environment) { | ||
| environments.put(environmentId, environment); | ||
| } | ||
|
|
||
| static void unregisterEnvironment(String environmentId) { | ||
| environments.remove(environmentId); | ||
| } | ||
|
|
||
| @Override | ||
| public int getPrecedence() { | ||
| return 100; | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable ProvidedValue getProvidedValue(String... keyAndAliases) { | ||
| if (keyAndAliases == null) { | ||
| return null; | ||
| } | ||
|
|
||
| String environmentId = liquibase.Scope.getCurrentScope().get(SPRING_ENV_ID_KEY, String.class); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could add additional strategies here, such as inferring the env if there's only one, for example. I opted for the simplest solution for now, but would love input on this.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not aware of how scope works but can't we just put the |
||
| if (environmentId == null) { | ||
| return null; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I considered adding logging here if we get a request for a config from this provider but we can't infer the env but I didn't see any other logging in the module, so I skipped for now. |
||
| } | ||
|
|
||
| Environment environment = environments.get(environmentId); | ||
| if (environment == null) { | ||
| return null; | ||
| } | ||
|
|
||
| for (String requestedKey : keyAndAliases) { | ||
| if (requestedKey != null) { | ||
| String propertyName = PREFIX + requestedKey; | ||
| String value = environment.getProperty(propertyName); | ||
| if (value != null) { | ||
| return new ProvidedValue(requestedKey, requestedKey, value, | ||
| "Spring Environment property '" + propertyName + "'", this); | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep the original signature. Even if it's not used by the method's body, it's a breaking change.