-
Notifications
You must be signed in to change notification settings - Fork 41.6k
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
Open
dmiska25
wants to merge
3
commits into
spring-projects:main
Choose a base branch
from
dmiska25:feature/liquibase-global-properties-pass-through
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+531
−27
Open
Support a generalized way to configure Liquibase global properties through Spring Boot configuration #47289
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
0545d3a
Add EnvironmentConfigurationValueProvider for Liquibase property pass…
dmiska25 9ffd144
Move liquibase provider registration to during SpringLiquibase creati…
dmiska25 8664d53
Refactor Liquibase integration to support environment-aware property …
dmiska25 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...ava/org/springframework/boot/liquibase/autoconfigure/EnvironmentAwareSpringLiquibase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
@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) { | ||
if (ex instanceof LiquibaseException) { | ||
throw (LiquibaseException) ex; | ||
} | ||
throw new LiquibaseException(ex); | ||
} | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
if (this.environmentId != null) { | ||
EnvironmentConfigurationValueProvider.unregisterEnvironment(this.environmentId); | ||
} | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
...g/springframework/boot/liquibase/autoconfigure/EnvironmentConfigurationValueProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
if (environmentId == null) { | ||
return null; | ||
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; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.