13

I just started right now a new project in Intellij using Spring Boot ver 2.1.3 and Flyway 5.2.4 with Java 11.

After try to start my project i got :

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'flyway' threw exception; nested exception is java.lang.IllegalStateException: Cannot find migrations location in: [classpath:db/migration] (please add migrations or check your Flyway configuration)

I have the following folders:

enter image description here

As you can see i have "db/migration" but without any migration, i just started right now. Debugging the class FlywayAutoConfiguration i got the following:

enter image description here

So, i tried to return all files in "classpath:", see:

enter image description here

Note that i just have "application.properties" file.

1

8 Answers 8

20

It is not that much useful or accurate answer.

But This issue make you frustrated so that i give this solution.

Note: Strange but it's true, Sometime it's not allow copy paste because your folder created db.migration and it expact db->migration(It's not same in this scenario). So whenever you start from scratch. Go to the resource folder -> Create DB folder -> Create migration folder -> Create database file with Version_SubVersion__Name(As defined below).

Normally this happens in following cases,

  1. Path is not proper try using set locations param value.
  2. db.migrate folder not contain any file.
  3. Check name of file : V1_1__(short_desc)
  4. Try to run using, mvn compile flyway:migrate

In my case i already place sql file over there but still it gives same error, Basically i place this sql file using copy paste from somewhere.

When i try to add one new file on same place using IDE (Intellij : Right click on migration folder -> new -> Flyway migration -> versioned migration), then it ask me(warning) about some delicate allowance(normally we mention in database configuration i also place there still), and it start working.

3
  • 1
    thanks, the folder structure was the issue for me. 🙏🏼 Commented Jul 15, 2020 at 12:39
  • 2
    I had a folder called db.migrations and not two nested folder db and inside migrations! A simple mv db.migrations db; mkdir db/migrations did the trick. Inside IntelliJ, the before and after looks identical! Commented Apr 27, 2021 at 16:04
  • I'd love to say "thank you, this fixed my issue I've never had before", but sadly it's the second time I'm running into this and you'd guess I would have learned by now. Thank you, IntelliJ for making this job a blast today ... Thank you, this fixed my issue :-D
    – Ben
    Commented Jul 8, 2021 at 13:46
13

Flyway requires at least one script, disable it until you need it by using the following command in application.properties file

spring.flyway.enabled=false

9

I believe that Flyway requires at least one migration script to initialize. Try adding a simple sql creation script into your migration folder and give it another try. Alternatively you can disable the flyway dependency until you need it.

4
  • thanks . how to disable it? i tried adding in application.yml flyway: baseline-on-migrate: false but its still trying to load Commented Oct 25, 2019 at 17:02
  • Just try removing the flyway dependency (e.g. removing the maven pom dependency)
    – Guy
    Commented Oct 29, 2019 at 12:01
  • the dependency is coming from skipper dependencies .if i exclude its giving another error - ava.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.skipper.server.db.migration.SkipperFlywayConfigurationCustomizer] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2] Commented Oct 30, 2019 at 20:21
  • this worked fine .Added below in application.yml spring: flyway: enabled: false baseline-on-migrate: false Commented Oct 30, 2019 at 20:55
4

I had the same issue. When I created the directory, I simply typed db.migration - the same way one would do with package names. InteliJ will display both db.migration and db/migration directories as db.migration, so while it may look correct in IntelliJ, flyway requires the latter.

2

I had a similar error, and solved it as follows: I added these commands

spring.flyway.baselineOnMigrate=true
spring.flyway.check-location=true
spring.flyway.locations=classpath:db/migration
spring.flyway.schemas=public
spring.flyway.enabled=true

to application.properties

2

If you have tried everything above and it still does not apply the migrations.

Be sure you have followed the steps in the previous answers

  • A db.migration folder exists in your resources folder
  • The sql scripts are named with respect to the correct naming convention, such as V1__init.sql
  • The following config exists in your application.properties/application.yml

    spring.flyway.locations=classpath:db/migration

Do a clean build -> mvn clean

Then restart your spring boot app, this worked for me.

If you are working from the cli, you can type

flyway clean

you may need to add inline config params, as needed.

1

Even when having your migration files in the db/migration folder, flyway won't detect it.

Then you will have to fix this by explicitly setting the locations in your application.properties (or appliocation.yml) by adding:

spring.flyway.locations=classpath:db/migration

N.B: Also you need to have at least one script to initialize flyway, you can even put an empty one. But you need to have at least one script

1

As @Guy suggested in his answer, I created an SQL file in the migration folder and left it empty. Named v1_0__mock_flyway.sql in the migration folder classpath:db/migration. Error solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.