When I run maven flyway:migrate
, I get the error
Failed to execute goal org.flywaydb:flyway-maven-plugin:6.5.5:migrate (default-cli) on project myProject: org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!
I have my Spring Boot settings in my application.yml file, but I guess the error means it doesn't detect the database config. This documention says, "Spring Boot will then automatically autowire Flyway with its DataSource and invoke it on startup." If I add the configuration to my pom.xml in the flyway plugin section, it connects to the database successfully, but I want it to use my application.yml config. Not the pom.xml. So what am I doing wrong?
Link to repo with issue: https://github.com/jack-cole/BrokenSpringBoot
application.yml
spring:
datasource:
driverClassName: org.postgresql.Driver
url: "jdbc:postgresql://localhost:5433/myDB"
username: postgres
password: test123
Dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jooq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.16</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>7.0.0</version>
</dependency>
Plugins:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>6.5.5</version>
</plugin>
You quoted a part of spring boot docs, but you launched your migration not by spring boot, but as a maven task.
Flyway maven plugin is not aware of spring boot configuration, it only takes the following sources into account: Overriding order
On my PC, I used the environment variables approach - I have the same environment variables defined for build plugin and for spring boot.
Run With maven:
Failed to execute goal org.flywaydb:flyway-maven-plugin:6.5.5:migrate (default-cli) on project myProject: org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!
You can configure url, user and password in flyway-maven-plugin configuration see First Steps Maven
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>7.0.0</version>
<configuration>
<url>jdbc:postgresql://localhost:5433/myDB</url>
<user>postgres</user>
<password>test123</password>
</configuration>
</plugin>
or with environment variables:
mvn flyway:migrate -Dflyway.url=jdbc:postgresql://localhost:5433/myDB -Dflyway.user=postgres -Dflyway.password=test123
More approaches in https://www.baeldung.com/database-migrations-with-flyway
Run with spring-boot:
Spring Boot auto configure and trigger Flyway at the application startup when you include the Flyway core library into the project. See usage of
@ConditionalOnClass(Flyway.class)
in FlywayAutoConfiguration :@Configuration(proxyBeanMethods = false) @ConditionalOnClass(Flyway.class) @Conditional(FlywayDataSourceCondition.class) @ConditionalOnProperty(prefix = "spring.flyway", name = "enabled", matchIfMissing = true) @AutoConfigureAfter({ DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) @Import({ FlywayEntityManagerFactoryDependsOnPostProcessor.class, FlywayJdbcOperationsDependsOnPostProcessor.class, FlywayNamedParameterJdbcOperationsDependencyConfiguration.class }) public class FlywayAutoConfiguration { ... }
Use mvn spring-boot:run
or java -jar app.jar
to run the application
NB : Check also that migration scripts are in db/migration
otherwise provide the locations with spring.flyway.locations
property
Resources:
https://flywaydb.org/documentation/configuration/parameters/
https://flywaydb.org/documentation/getstarted/firststeps/maven/
https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-execute-flyway-database-migrations-on-startup
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With