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

12,471

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.

Share:
12,471

Related videos on Youtube

Adrian Smith
Author by

Adrian Smith

Software developer & software architect living in Vienna, Austria. From London, UK originally.

Updated on July 13, 2022

Comments

  • Adrian Smith
    Adrian Smith almost 2 years

    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?

    • M. Deinum
      M. Deinum about 7 years
      flyway will by default use the normal datasource specified by spring.datasource properties generally you don't need an additional one. If you really want to use separate URL and user for flyway you need flyway.url with flyway.username and flyway.password and not embedded in the URL. Next to that make sure you have a database driver else no JDBC is detected and nothing will be run (without JDBC it won't work).
    • M. Deinum
      M. Deinum about 7 years
      As I stated in my first comment if will only work if you are using a datasource already. If you aren't it doesn't do anything. The flyway configuration depends on the availability of a datasource. If that isn't availing flyway will be disabled.
  • Stefan Falk
    Stefan Falk about 6 years
    Unfortunately the link "Spring Boot's Flyway integration" is broken. I think I'm facing the same issue with flyway not getting run by spring.
  • Adrian Smith
    Adrian Smith almost 6 years
    Thanks for the headsup @StefanFalk. I've corrected it now.