Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
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,45 @@
/*
* 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.diagnostics.analyzer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This failure analyzer should belong with the rest of the Flyway support. For insance HikariDriverConfigurationFailureAnalyzer is in the jdbc package of the auto-configuration module. Can you please move this class to the auto-configuration module as well (in the flyway package)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I have moved this class under flyway package of auto-configuration module.


import org.apache.commons.lang3.StringUtils;

import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis;

/**
* A {@code FailureAnalyzer} that performs analysis of failures caused by a
* {@code IllegalStateException}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't really do that. The purpose of a failure analyzer is to tracker a specific exception. This is a sign that we need to provide such exception if it doesn't exist yet to avoid magic string comparison.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand. I have created a new exception as advised.

*
* @author Anand Shastri
*/
public class FlywayMigrationScriptMissingFailureAnalyzer
extends AbstractFailureAnalyzer<IllegalStateException> {

private static final String MISSING_SCRIPT_MESSAGE = "Cannot find migrations location in";

@Override
protected FailureAnalysis analyze(Throwable rootFailure, IllegalStateException cause) {
if (cause.getMessage().startsWith(MISSING_SCRIPT_MESSAGE)) {
String location = StringUtils.substringBetween(cause.getMessage(), "[", "]");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than parsing the error message, please create a FlywayMigrationScriptNotFoundException or something that could contain the original locations as an argument?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also a case of no location configured at all which would fit here and would require a slightly different failure analysis.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FlywayMigrationScriptNotFoundException is now taking location as an argument.

I have added a separate comment on this thread about failure in case of no location configured.

return new FailureAnalysis("Cannot find migrations location in " + location,
" please add migrations or check your Flyway configuration", cause);
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ org.springframework.boot.diagnostics.analyzer.NoUniqueBeanDefinitionFailureAnaly
org.springframework.boot.diagnostics.analyzer.PortInUseFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.ValidationExceptionFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyNameFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyValueFailureAnalyzer
org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyValueFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.FlywayMigrationScriptMissingFailureAnalyzer

# FailureAnalysisReporters
org.springframework.boot.diagnostics.FailureAnalysisReporter=\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.diagnostics.analyzer;

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 {

private final FlywayMigrationScriptMissingFailureAnalyzer analyzer = new FlywayMigrationScriptMissingFailureAnalyzer();

@Test
public void analysisForFlywayScriptMissingFailureWhenIllegalStateExceptionIsForFlyway() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should change with the case where none of the locations exist and one case where no location is provided (see FlywayAutoConfiguration).

FailureAnalysis failureAnalysis = this.analyzer.analyze(new IllegalStateException(
"Cannot find " + "migrations location in: [classpath:db/migration] "
+ "(please add migrations or check your Flyway configuration)"));

assertThat(failureAnalysis.getDescription())
.endsWith("Cannot find migrations location in classpath:db/migration");
assertThat(failureAnalysis.getAction())
.endsWith(" please add migrations or check your Flyway configuration");
}

@Test
public void analysisForFlywayScriptMissingFailureWhenIllegalStateExceptionIsNotForFlyway() {
FailureAnalysis failureAnalysis = this.analyzer
.analyze(new IllegalStateException("Some other exception"));

assertThat(failureAnalysis).isNull();
}

}