Polish "Add failure analyzer for missing Liquibase changelog"
See gh-22320
This commit is contained in:
parent
554a962a13
commit
0ce0c3a54a
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.boot.diagnostics.analyzer;
|
package org.springframework.boot.liquibase;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@ import liquibase.exception.ChangeLogParseException;
|
||||||
|
|
||||||
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
|
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
|
||||||
import org.springframework.boot.diagnostics.FailureAnalysis;
|
import org.springframework.boot.diagnostics.FailureAnalysis;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An {@link AbstractFailureAnalyzer} that analyzes exceptions of type
|
* An {@link AbstractFailureAnalyzer} that analyzes exceptions of type
|
||||||
|
@ -33,20 +34,23 @@ class LiquibaseChangelogMissingFailureAnalyzer extends AbstractFailureAnalyzer<C
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected FailureAnalysis analyze(Throwable rootFailure, ChangeLogParseException cause) {
|
protected FailureAnalysis analyze(Throwable rootFailure, ChangeLogParseException cause) {
|
||||||
FileNotFoundException exception = findCause(cause, FileNotFoundException.class);
|
FileNotFoundException fileNotFound = findCause(cause, FileNotFoundException.class);
|
||||||
if (exception != null) {
|
if (fileNotFound != null) {
|
||||||
return new FailureAnalysis(getDescription(cause),
|
String changelogPath = extractChangelogPath(cause);
|
||||||
"Make sure a Liquibase changelog is present at the configured path", cause);
|
if (StringUtils.hasText(changelogPath)) {
|
||||||
|
return new FailureAnalysis(getDescription(changelogPath),
|
||||||
|
"Make sure a Liquibase changelog is present at the configured path.", cause);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getDescription(ChangeLogParseException cause) {
|
|
||||||
return "Liquibase failed to start because no changelog could be found at: " + extractChangelogPath(cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String extractChangelogPath(ChangeLogParseException cause) {
|
private String extractChangelogPath(ChangeLogParseException cause) {
|
||||||
return cause.getMessage().substring("Error parsing ".length());
|
return cause.getMessage().substring("Error parsing ".length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getDescription(String changelogPath) {
|
||||||
|
return "Liquibase failed to start because no changelog could be found at '" + changelogPath + "'.";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -57,7 +57,7 @@ org.springframework.boot.diagnostics.analyzer.IncompatibleConfigurationFailureAn
|
||||||
org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyNameFailureAnalyzer,\
|
org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyNameFailureAnalyzer,\
|
||||||
org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyValueFailureAnalyzer,\
|
org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyValueFailureAnalyzer,\
|
||||||
org.springframework.boot.diagnostics.analyzer.PatternParseFailureAnalyzer,\
|
org.springframework.boot.diagnostics.analyzer.PatternParseFailureAnalyzer,\
|
||||||
org.springframework.boot.diagnostics.analyzer.LiquibaseChangelogMissingFailureAnalyzer
|
org.springframework.boot.liquibase.LiquibaseChangelogMissingFailureAnalyzer
|
||||||
|
|
||||||
# FailureAnalysisReporters
|
# FailureAnalysisReporters
|
||||||
org.springframework.boot.diagnostics.FailureAnalysisReporter=\
|
org.springframework.boot.diagnostics.FailureAnalysisReporter=\
|
||||||
|
|
|
@ -14,12 +14,17 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.boot.diagnostics.analyzer;
|
package org.springframework.boot.liquibase;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import liquibase.integration.spring.SpringLiquibase;
|
import liquibase.integration.spring.SpringLiquibase;
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
import org.springframework.boot.diagnostics.FailureAnalysis;
|
import org.springframework.boot.diagnostics.FailureAnalysis;
|
||||||
|
@ -35,15 +40,26 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
*
|
*
|
||||||
* @author Sebastiaan Fernandez
|
* @author Sebastiaan Fernandez
|
||||||
*/
|
*/
|
||||||
class LiquibaseChangelogMissingFailureAnalyzerTest {
|
class LiquibaseChangelogMissingFailureAnalyzerTests {
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void configureDerbyLogLocation(@TempDir File temp) {
|
||||||
|
System.setProperty("derby.stream.error.file", new File(temp, "derby.log").getAbsolutePath());
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
static void clearDerbyLogLocation(@TempDir File temp) {
|
||||||
|
System.clearProperty("derby.stream.error.file");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void changelogParseExceptionDueToChangelogNotPresent() {
|
void changelogParseExceptionDueToChangelogNotPresent() {
|
||||||
FailureAnalysis analysis = performAnalysis();
|
FailureAnalysis analysis = performAnalysis();
|
||||||
assertThat(analysis.getDescription())
|
assertThat(analysis.getDescription())
|
||||||
.isEqualTo("Liquibase failed to start because no changelog could be found at: "
|
.isEqualTo("Liquibase failed to start because no changelog could be found at '"
|
||||||
+ "classpath:/db/changelog/db.changelog-master.yaml");
|
+ "classpath:/db/changelog/db.changelog-master.yaml'.");
|
||||||
assertThat(analysis.getAction()).isEqualTo("Make sure a Liquibase changelog is present at the configured path");
|
assertThat(analysis.getAction())
|
||||||
|
.isEqualTo("Make sure a Liquibase changelog is present at the configured path.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private FailureAnalysis performAnalysis() {
|
private FailureAnalysis performAnalysis() {
|
||||||
|
@ -53,11 +69,8 @@ class LiquibaseChangelogMissingFailureAnalyzerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
private BeanCreationException createFailure() {
|
private BeanCreationException createFailure() {
|
||||||
try {
|
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
LiquibaseConfiguration.class)) {
|
||||||
context.register(LiquibaseConfiguration.class);
|
|
||||||
context.refresh();
|
|
||||||
context.close();
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
catch (BeanCreationException ex) {
|
catch (BeanCreationException ex) {
|
||||||
|
@ -70,14 +83,13 @@ class LiquibaseChangelogMissingFailureAnalyzerTest {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
DataSource dataSource() {
|
DataSource dataSource() {
|
||||||
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:normal").username("sa").build();
|
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:test").username("sa").build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
SpringLiquibase springLiquibase(DataSource dataSource) {
|
SpringLiquibase springLiquibase(DataSource dataSource) {
|
||||||
SpringLiquibase liquibase = new SpringLiquibase();
|
SpringLiquibase liquibase = new SpringLiquibase();
|
||||||
liquibase.setChangeLog("classpath:/db/changelog/db.changelog-master.yaml");
|
liquibase.setChangeLog("classpath:/db/changelog/db.changelog-master.yaml");
|
||||||
liquibase.setShouldRun(true);
|
|
||||||
liquibase.setDataSource(dataSource);
|
liquibase.setDataSource(dataSource);
|
||||||
return liquibase;
|
return liquibase;
|
||||||
}
|
}
|
Loading…
Reference in New Issue