Home > Back-end >  Cannot migrate sql script with Flyway for Postgres (Java Spring)
Cannot migrate sql script with Flyway for Postgres (Java Spring)

Time:08-24

I am currently building my application with Spring. And recently stumbled through flyway for db migrations and decided to also implement it into the project.

I have all of my .sql scripts placed under resources/db/migration The following is how I set up the build.gradle:

plugins {
    id 'org.springframework.boot' version '2.6.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id "org.flywaydb.flyway" version "9.1.6"
}

group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.postgresql:postgresql'
    compileOnly 'org.projectlombok:lombok'
    compileOnly 'org.flywaydb:flyway-core'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

flyway {
    url = 'jdbc:postgresql://localhost:5432/test'
    user = 'postgres'
    password = 'postgres'
    driver = 'org.postgresql.Driver'
    schemas = ['public']
    locations = ['filesystem:src/main/resources/db/migration/']
}

tasks.named('test') {
    useJUnitPlatform()
}

When I run gradle flywayMigrate -i I kept getting this error:

    > Task :flywayMigrate FAILED
    Caching disabled for task ':flywayMigrate' because:
      Build cache is disabled
    Task ':flywayMigrate' is not up-to-date because:
      Task has not declared any outputs despite executing actions.
Resolving global dependency management for project 'myapp.main.module'
Excluding []
Excluding []
Excluding []
Excluding []
Database: jdbc:postgresql://localhost:5432/test (PostgreSQL 14.4)
Successfully validated 3 migrations (execution time 00:00.010s)
:flywayMigrate (Thread[Execution worker Thread 3,5,main]) completed. Took 0.396 secs.

What is it that I still doing it incorrectly?

CodePudding user response:

I managed to solve it myself. Apparently I have to define the schema history table first. This can be done easily first time by adding baselineOnMigrate to true inside build.gradle:

flyway {
    url = 'jdbc:postgresql://localhost:5432/test'
    user = 'postgres'
    password = 'postgres'
    driver = 'org.postgresql.Driver'
    baselineOnMigrate = true
    locations = ['filesystem:src/main/resources/db/migration/']
}

I also removed schemas as it is not necessarily required in this case.

  • Related