25

I can't seem to let flyway know where to look for my migrations. My file structure is the default generated from spring initializr. My migrations are in: ./demo/src/main/kotlin/db/migration My migrations are java based

My application.properties file looks like this:

spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.url=jdbc:postgresql://${JDBC_DATABASE_URL}/jpaTestDatabase
spring.datasource.username=${JDBC_DATABASE_USERNAME}
spring.datasource.password=${JDBC_DATABASE_PASSWORD}

spring.flyway.baseline-on-migrate=true
spring.flyway.locations=classpath:demo/src/main/kotlin/db/migration

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=validate
spring.session.store-type=none

I tried several classpaths:

/demo/src/main/kotlin/db/migration
demo/src/main/kotlin/db/migration
/src/main/kotlin/db/migration
src/main/kotlin/db/migration

None of the above seem to work.

How can I let flyway know where the migrations are?

PaulB
  • 1,554
  • 2
  • 16
  • 34
  • 1
    Migrations in `src/main/kotlin/db/migration` should be found by the default location of `classpath:db/migration` as the code in `src/main/kotlin/db/migration` will be compiled into the `db/migration` directory on the classpath. Can you provide an example of one of the Kotlin-based migrations you've written. If you've subclassed `org.flywaydb.core.api.migration.BaseJavaMigration`, does the name of your migration meet its requirements? – Andy Wilkinson Nov 06 '18 at 14:49
  • 2
    Can confirm that classpath:db/migration does indeed work – PaulB Nov 06 '18 at 14:52

9 Answers9

43

In my case, I got that error message because I created the folders via copy-paste in the IDE (and not manually, as one usually does).

I actually had (which didn't work):

src/main/resources/db.migration/

instead of the correct (which worked):

src/main/resources/db/migration/

The db.migration version obviously does not work, but it is hard to spot on the IDE.

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
39

I had a different problem, my migration file name was V1_Base_version.sql instead of V1__Base_version.sql. Flyway requires double underscore __ in name prefix.

Oleksandr.Bezhan
  • 2,028
  • 3
  • 22
  • 31
21

By default Flyway will look for migrations on the classpath under db/migration, which on a Maven project means src/main/resources/db/migration.

Ensure that you have directory like this.

Refer flyway-db-migration-folder

Alien
  • 15,141
  • 6
  • 37
  • 57
  • 1
    Thank you so much!! – Chasen Bettinger Sep 18 '20 at 18:46
  • This image made me understand the problem I had: `mvn flyway:migrate -Dflyway.locations=filesystem:src/main/resources/db/migration/common,src/main/resources/db/migration/systemtest ...`. **The prefix `filesystem` need to be added for each location** and not only the first one (or it will use the `classpath` prefix). Hope it can save some time to others :) – Chavjoh Nov 29 '22 at 17:25
  • 1
    This does not work anymore. You need the path from the repository-root. Which means try ```locations: filesystem:my-other-folder```. If you are using Intellij, just copy the path from repository root and it will work straight away. Tested this on flyway 6.4.4 and spring-boot 2.2.6.RELEASE with java version 11 – Pranjal Gore Mar 27 '23 at 12:19
1

In my case, the trick was: once Flyway has succeeded in running the update script, it creates the table flyway_schema_history, that recorded that I have already run the create script successfully once.

I was playing back and forward with the implementation, changing spring.jpa.hibernate.ddl-auto to validate and then create-drop, and then back.

So, when I tried to run the script second time, that was rejected but since the originally created table (in my case called 'bike') was deleted when the app shut down when run in spring.jpa.hibernate.ddl-auto=create-drop mode previous time, I was getting the

SchemaManagementException: Schema-validation: missing table [bike] Exception.

Solution is: drop the flyway_schema_history table or remove the records corresponding to the previous run and roll again!

Nestor Milyaev
  • 5,845
  • 2
  • 35
  • 51
1

Also make sure that your folder path is correct.

Mine was: src/main/resources/db.migration/...
instead of: src/main/resources/db/migration/...

I got the same error as migration files weren't retrieved.

PHA
  • 11
  • 2
0

you must agregate or uncomment the line flyway.locations with

flyway.localtions=filesystem:.

into flyway.conf configuration file.

0

in my case i had - (dash character) in the name of migration like V20220508-1900__init.sql. removing - fixed the problem

0
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-r2dbc</artifactId>
        </dependency>
    <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
            <version>9.16.1</version>
        </dependency>
database:
  url: postgresql://localhost:5433/db_name
spring:
  flyway:
    baseline-description: true
    baseline-on-migrate: true
    create-schemas: true
    default-schema: public
    enabled: true
    locations: classpath:db/migration
    password: ${spring.r2dbc.password}
    schemas: public
    url: jdbc:${database.url}
    user: ${spring.r2dbc.username}
    validate-on-migrate: true
  r2dbc:
    password: postgres
    url: r2dbc:postgresql://localhost:5433/db_name
    username: postgres
  • V2__create_car.sql (example)

Start numbering with V2, the script with V1 version is ignored. I don't understand why, but that's exactly how it works for me.

skyho
  • 1,438
  • 2
  • 20
  • 47
0

I am on Mac M1 chip, and things are a little different. After numerous attempts, I found two methods to solve this issue.

Option 1, replace flyway.localtions=filesystem: relative path with flyway.localtions=filesystem: absolute path

Option 2, upgrade gradle from 6.8.1 to 7.3

Hao Feng
  • 61
  • 1
  • 3