Merge pull request #16015 from anandshastri1990
* pr/16015: Polish "Add failure analyzer for Flyway's bootstrap failure" Add failure analyzer for Flyway's bootstrap failurepull/16046/head
commit
6d286b9be7
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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
|
||||
*
|
||||
* http://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.autoconfigure.flyway;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Exception thrown when no Flyway migration script is available.
|
||||
*
|
||||
* @author Anand Shastri
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.2.0
|
||||
*/
|
||||
public class FlywayMigrationScriptMissingException extends RuntimeException {
|
||||
|
||||
private final List<String> locations;
|
||||
|
||||
FlywayMigrationScriptMissingException(List<String> locations) {
|
||||
super(locations.isEmpty() ? "Migration script locations not configured"
|
||||
: "Cannot find migrations location in: " + locations
|
||||
+ " (please add migrations or check your Flyway configuration)");
|
||||
this.locations = new ArrayList<>(locations);
|
||||
}
|
||||
|
||||
public List<String> getLocations() {
|
||||
return this.locations;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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
|
||||
*
|
||||
* http://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.autoconfigure.flyway;
|
||||
|
||||
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
|
||||
import org.springframework.boot.diagnostics.FailureAnalysis;
|
||||
|
||||
/**
|
||||
* A {@code FailureAnalyzer} that performs analysis of failures caused by a
|
||||
* {@link FlywayMigrationScriptMissingException}.
|
||||
*
|
||||
* @author Anand Shastri
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class FlywayMigrationScriptMissingFailureAnalyzer
|
||||
extends AbstractFailureAnalyzer<FlywayMigrationScriptMissingException> {
|
||||
|
||||
@Override
|
||||
protected FailureAnalysis analyze(Throwable rootFailure,
|
||||
FlywayMigrationScriptMissingException cause) {
|
||||
StringBuilder description = new StringBuilder("Flyway failed to initialize: ");
|
||||
if (cause.getLocations().isEmpty()) {
|
||||
return new FailureAnalysis(description
|
||||
.append("no migration scripts location is configured").toString(),
|
||||
"Check your Flyway configuration", cause);
|
||||
}
|
||||
else {
|
||||
description.append(String.format(
|
||||
"none of the following migration scripts locations could be found:%n%n"));
|
||||
cause.getLocations().forEach((location) -> description
|
||||
.append(String.format("\t- %s%n", location)));
|
||||
return new FailureAnalysis(description.toString(),
|
||||
"Review the locations above or check your Flyway configuration",
|
||||
cause);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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
|
||||
*
|
||||
* http://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.autoconfigure.flyway;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.diagnostics.FailureAnalysis;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link FlywayMigrationScriptMissingFailureAnalyzer}.
|
||||
*
|
||||
* @author Anand Shastri
|
||||
*/
|
||||
public class FlywayMigrationScriptMissingFailureAnalyzerTests {
|
||||
|
||||
@Test
|
||||
public void analysisForMissingScriptLocation() {
|
||||
FailureAnalysis failureAnalysis = performAnalysis();
|
||||
assertThat(failureAnalysis.getDescription())
|
||||
.contains("no migration scripts location is configured");
|
||||
assertThat(failureAnalysis.getAction())
|
||||
.contains("Check your Flyway configuration");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void analysisForScriptLocationsNotFound() {
|
||||
FailureAnalysis failureAnalysis = performAnalysis("classpath:db/migration");
|
||||
assertThat(failureAnalysis.getDescription()).contains(
|
||||
"none of the following migration scripts locations could be found")
|
||||
.contains("classpath:db/migration");
|
||||
assertThat(failureAnalysis.getAction()).contains(
|
||||
"Review the locations above or check your Flyway configuration");
|
||||
}
|
||||
|
||||
private FailureAnalysis performAnalysis(String... locations) {
|
||||
FlywayMigrationScriptMissingException exception = new FlywayMigrationScriptMissingException(
|
||||
Arrays.asList(locations));
|
||||
return new FlywayMigrationScriptMissingFailureAnalyzer().analyze(exception);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue