Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug when Flyway doesn't work on Spring Boot?

Tags:

I am using Maven and Spring Boot. I run the application using mvn spring-boot:run.

https://flywaydb.org/documentation/plugins/springboot says Flyway should be called on Spring Boot start.

So my pom.xml contains the dependency to Flyway.

<dependency>     <groupId>org.flywaydb</groupId>     <artifactId>flyway-core</artifactId>     <version>4.1.2</version> </dependency> 

The first time I ran the Maven command above it downloaded Flyway stuff, so I think the dependency is working.

I have the following files:

./src/main/resources/db/migration/V123__foo.sql ./src/main/resources/application.properties 

The above article implied it should "just work", but I don't understand where it would find the JDBC URL to the database. So I added the following to the application.properties file:

flyway.url=jdbc:postgresql://localhost:5432/services?user=postgres&password=postgres flyway.enabled=true 

When Spring Boot starts up (and loads and makes available my web application) there are no logs from Flyway. I think Flyway is ignored.

What can I do? Or, more generally, how would I go about debugging this problem myself?

like image 672
Adrian Smith Avatar asked Apr 19 '17 13:04

Adrian Smith


People also ask

How do I enable Flyway in spring boot?

Spring Boot comes with out-of-the-box integration for Flyway. Spring Boot will then automatically autowire Flyway with its DataSource and invoke it on startup. You can then configure a good number of Flyway properties directly from your application. properties or application.

How do I enable debugging in spring boot?

You can also enable a “debug” mode by starting your application with a --debug flag. You can also specify debug=true in your application. properties . When the debug mode is enabled, a selection of core loggers (embedded container, Hibernate, and Spring Boot) are configured to output more information.


1 Answers

Nobody has posted an answer so I'll post what I found out.

M. Deinum was indeed correct in their comments to the question, the problem was a lack of a data source.

My original question was what the approach should be to debugging this kind of issue. Obviously one option is to post to stackoverflow :) But I wanted to know how to do it myself.

Spring Boot has a number of classes which look at your code and classpath, and act appropriately. For example, there are classes providing implementations to the rules like "if Flyway is on the path, and there is a data source, then execute Flyway". That rule wasn't getting triggered in my case, because I had no data source.

It's not the case that the code you write calls Spring Boot, it's the other way around, Spring Boot (external to your code) inspects your code and decides what to do based on rules. This architecture is known as action at a distance. The main problem with action at a distance is it's very difficult to debug.

The only real way to find the solution, and it was the way I went about confirming M. Deinum's diagnostic, is to read the Spring Boot source code and understand the annotations which are used to create Spring Boot code.

From the source code to Spring Boot's Flyway integration we see

@ConditionalOnClass(Flyway.class) @ConditionalOnBean(DataSource.class) 

This means "this code will get executed if Flyway is on the classpath, and if there is a DataSource bean available; otherwise it silently won't get executed".

So the answer to the question "how to debug this problem" is that there is no mechanism other than to read the source code of Spring Boot and find out how it works.

If you want to avoid this sort of problem, you have to avoid frameworks which work via "action at a distance", and that includes Spring Boot.

like image 130
Adrian Smith Avatar answered Sep 18 '22 08:09

Adrian Smith