Spring Boot Application is not running Flyway migrations on startup - java

I have a Spring Boot Application with a couple Entity classes and I'm trying to implement database migrations with flyway. It appears that, on startup, Spring Boot is not running flyway at all.
Here is my application.properties
spring.datasource.url= jdbc:postgresql://localhost:5555/mfidb
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=validate
spring.flyway.enabled=true
Here are the lines in my build.gradle that have something to do with flyway
plugins {
id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id "org.flywaydb.flyway" version "6.4.1"
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.flywaydb:flyway-core'
runtime('org.postgresql:postgresql:42.2.12')
}
flyway {
url = 'jdbc:postgresql://localhost:5555/mfidb'
user = 'postgres'
password = 'postgres'
}
I am able to run my migrations by entering gradle flywayMigrate -i in the terminal.
But I want the migrations to run on startup.
Any help would be greatly appreciated, thanks in advance.
EDIT:
Here is the console output when running the application
2020-05-05 11:55:59.022 INFO 50754 --- [ main] com.ubm.mfi.MfiApplication : Starting MfiApplication on MacBook-Pro.local with PID 50754 (~/Downloads/mfi 5/build/classes/java/main started by will in ~/Downloads/mfi 5)
2020-05-05 11:55:59.024 INFO 50754 --- [ main] com.ubm.mfi.MfiApplication : No active profile set, falling back to default profiles: default
2020-05-05 11:55:59.457 INFO 50754 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-05-05 11:55:59.495 INFO 50754 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 33ms. Found 2 JPA repository interfaces.
2020-05-05 11:55:59.778 INFO 50754 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-05-05 11:55:59.783 INFO 50754 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-05-05 11:55:59.784 INFO 50754 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-05-05 11:55:59.838 INFO 50754 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-05-05 11:55:59.838 INFO 50754 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 768 ms
2020-05-05 11:55:59.917 INFO 50754 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-05-05 11:55:59.944 INFO 50754 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.12.Final
2020-05-05 11:55:59.991 INFO 50754 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-05-05 11:56:00.051 INFO 50754 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-05-05 11:56:00.114 INFO 50754 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-05-05 11:56:00.124 INFO 50754 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL10Dialect
2020-05-05 11:56:00.545 INFO 50754 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-05-05 11:56:00.548 INFO 50754 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-05-05 11:56:00.818 WARN 50754 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-05-05 11:56:00.891 INFO 50754 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-05-05 11:56:01.005 INFO 50754 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-05 11:56:01.007 INFO 50754 --- [ main] com.ubm.mfi.MfiApplication : Started MfiApplication in 2.255 seconds (JVM running for 2.983)
Here is the path to the data migrations

Add logging.level.root=debug your application.properties file to see detailed information during start application.
Also just add flyway user and password to application.properties.
spring.flyway.url = 'jdbc:postgresql://localhost:5555/mfidb'
spring.flyway.password=postgres
spring.flyway.user=postgres
if you dont need to run flyway from gradle, you can remove flyway config from build.gradle

My dependencies weren't getting refreshed when i did a gradle clean build so the flyway source was never there. I had to manually refresh my dependencies in my IDE and it started working. Sad when you spend hours on something so simple. Giga's comment to change the logging level helped me to find the issue. Thanks

Your application.properties looks perfect to me.
I am using Flyway as well, it works like a charm and executes migrations on startup of my application. The only entry in my build.gradle related to flyway is the dependency
runtime("org.flywaydb:flyway-core")
I think you should remove all other flyway related entries from build.gradle.

In my case this is what happened:
I spent almost 4 hours to figure this out, My assumption was Spring JPA starter bring the flyway core dependency along with it. I didn't add the dependency for flyway-core explicitly. So if we dont do it spring boot flyway auto configuration will not give any error but just simply ignore the migration setup.
By tracing the auto configuration I figured out this, auto configuration ignoring the flyway setup if dependency not added manually rather than throwing exception using conditional annotation like this #ConditionalOnMissingBean({Flyway.class})

In my case it, was a missing character in the filename of the flyway init script.
Bad-Filename: V1_init.sql
Good-Filename: V1__init.sql (double '_')
I was missing an underscore in the filename, it needs 2 instead of 1.
Using spring-boot 2.6.7, my application.properties
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=validate # or 'none'
Resource-Folder
src/main/resources
- db/migration
- V1__init.sql
Example of V1__init.sql
create sequence hibernate_sequence start with 1 increment by 1;
CREATE TABLE person
(
id INT NOT NULL,
name VARCHAR(255),
age INT,
CONSTRAINT pk_person PRIMARY KEY (id)
);
Described correctly in the spring docs, but I recognised too late

Check if your script already run.
The trick is, once Flyway has succeeded in running the update script, it has created the table flyway_schema_history, that recorded that I have already run the create script successfully once.
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.
drop the flyway_schema_history table or delete a record corresponding to previous script run and roll again!

After checking the SQL manually, here's what worked for me (using Spring Boot + Cloud):
important part in my build.gradle:
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.flywaydb:flyway-core'
implementation 'org.hibernate:hibernate-core'
implementation 'org.hibernate.validator:hibernate-validator:7.0.1.Final'
no gradle plugins. And in application.yml / application-local.yml:
spring:
flyway:
baseline-on-migrate: true
jpa:
database: POSTGRESQL
show-sql: true
hibernate:
ddl-auto: validate
open-in-view: true
datasource:
platform: postgres
url: jdbc:postgresql://localhost:5432/your_db
username: your_db_user
password: your_db_passwd
driverClassName: org.postgresql.Driver
Note that you do not need the flyway specific config for DB user/url. Spring Boot detects Flyway (or Liquibase if you were using that...) on the classpath. It then uses the primary datasource. See: https://docs.spring.io/spring-boot/docs/2.5.6/reference/html/howto.html#howto.data-initialization . Then the last trick is to make your first script V2__init.sql . This is critical as the baseline-on-migrate: true bit creates the flyway_schema_history table and its action is the first thing, so version 1, or V1_ ie the first row... so your script won't go if it is V1__init.sql or whatever. It will think it has already run. It needs to be the next one, V2__init.sql or whatever.

I'm using maven, and solved this issue by running mvn clean install

spring-boot-starter-data-jpa and mysql-connector-java dependencies are required for flyway to run. You can check that in debug output by enabling logging.level.root=DEBUG in your application properties.
Just add following dependencies to fix the issue:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>

In my case I had the db/migration incorrectly spelled :)

I found my way here after days of trying different permutations of configurations. For me when trying to upgrade all the way from 6.2.1 to 8.5.11 I simply needed to add flyway.migrate() to the before each in my integration tests.

Tried all of the listed answers, and nothing helped.
Version incompatibility was the cause!
Spring Boot 2.7.0 goes with Flyway 8.5.13

In my case, I had not named the migration correctly.
I had this name v1_0_7__addEws.sql but it should start with capital V
,ie, V1_0_7__addEws.sql

Related

Spring Boot works on Other Local Computers but not mine IntelliJ, Bean Factory not working

I am using Spring Boot with IntelliJ Java 8. For some reason the Bean Factory is working on all other teammates computers from Git, except my local computer. I am receiving the following errors. It's like it cannot initialize the Beans. I cleared, invalidated caches, and restarted Intellij. Still no luck. Does anyone know what the issue can be?
2021-09-02 12:26:49.213 INFO 2430 --- [ main] c.k.s.s.c.capture.server.Application : Starting Application using Java 1.8.0_73 on KMAC-15SJGH5 with PID 2430 (/Users/joe/src/charge-capture/service/service-server/target/classes started by joe in /Users/joe/src/charge-capture)
2021-09-02 12:26:49.217 INFO 2430 --- [ main] c.k.s.s.c.capture.server.Application : No active profile set, falling back to default profiles: default
2021-09-02 12:26:51.689 INFO 2430 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-09-02 12:26:51.690 INFO 2430 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-09-02 12:26:51.721 INFO 2430 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 16 ms. Found 0 JPA repository interfaces.
2021-09-02 12:26:51.742 INFO 2430 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-09-02 12:26:51.744 INFO 2430 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2021-09-02 12:26:51.768 INFO 2430 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 8 ms. Found 0 Redis repository interfaces.
2021-09-02 12:26:52.197 INFO 2430 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=43442f9b-fc6e-3ab1-be0d-b16685f5327e
2021-09-02 12:26:52.554 INFO 2430 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'com.abcd.library.springboot.autoconfigure.config.RedisCacheCon
This program does not have errors on other computers, and so its not a source code problem, more of an environmental issue with my local.
Active profiles does not seem like the issue, as it has a select mechanism here
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Application.class);
String[] profiles = SpringProfilesUtil.readActiveProfiles(Application.class);
if(profiles != null) {
springApplication.setAdditionalProfiles(profiles);
}
springApplication.run(args);
}
I did not locate Maven Window even though the Plugin was installed.
Right Click on the root pom.xml file and select "Add as Maven Project" from the context menu. After this, application should run without Bean errors.
Not sure why it fixed the issue, however could be a small issue with IntelliJ. Either way, it works.

spring boot Tomcat

I have created one simple spring boot application and have added 2 dependencies using spring initializers, H2 and JPA.
I have added H2 configuration in application.properties file and then added hoteldata.sql file in resource folder. When i am running the project, spring boot log doesn't show Tomcat is started.
I am expecting below log to be displayed while starting the spring boot application,
main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
2020-12-06 18:16:26.350 INFO [userms,,,] 9144 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-12-06 18:16:26.351 INFO [userms,,,] 9144 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-12-06 18:16:27.085 INFO [userms,,,] 9144 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
but it is not appearing..
When i am hitting the endpoint, http://localhost:8081/h2-console nothing is happening..
Did i miss anything at configuration level ?
Finally, I have solved this problem. Along with H2 and JPA dependency, we need to add 'spring boot starter web' dependency as well .It helps to start inbuilt Tomcat in spring boot application.

Cant run spring boot application?

I'm working on an assignment with a team. And my team decided to use spring boot for the application. However for some reason whenever I run the application I get a different message on my terminal whereas my teammates have a different message on their terminal. Instead of "LiveReload server is running on port 35729" They get "tomcat initialized with port(s) 8081 (http)".
I am not sure what exactly to do since all of us are using the same project and I have built it as they told me to do so.
However it is still not working for me. Can anybody tell me how to fix it so I could get it to run. I have never used maven or spring boot before.
This is the message I am getting on my terminal
2020-06-13 23:05:35.538 INFO 34396 --- [ restartedMain] com.rest.WebApplication : Starting WebApplication on DESKTOP-1M4A445 with PID 34396 (Directory\project\target\classes started by USER in Directory/project)
2020-06-13 23:05:35.543 INFO 34396 --- [ restartedMain] com.rest.WebApplication : No active profile set, falling back to default profiles: default
2020-06-13 23:05:35.609 INFO 34396 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-06-13 23:05:36.520 INFO 34396 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2020-06-13 23:05:36.550 INFO 34396 --- [ restartedMain] com.rest.WebApplication : Started WebApplication in 1.452 seconds (JVM running for 2.035)
Process finished with exit code 0
Thank you so much.
It seems you have spring-boot-devtools dependency in the classpath. Because of this you see LiveReload server is running on port 35729 statement written to log.
From your logs, it seems tomcat is not started at all. Verify your properties files and make sure web environment is not turned off
For spring-boot 1.x, look for spring.main.web-environment=false property
For spring-boot 2.x, look for spring.main.web-application-type=none property

Postman is not fetching data from spring application

I am new to spring.
My spring application is up and running. but when i am using postman to fetch the data. it is throwing 404 not found error. I checked URL but I am not getting where I am going wrong.
can anyone help me.
here is my API class
#RestController
#RequestMapping(value="/nicetry")
public class NiceAPI {
#Autowired
private NiceService niceService;
#GetMapping(value = "/student")
public ResponseEntity<List<Student>> getDetails(){
List<Student> studentList= niceService.getDetails();
ResponseEntity<List<Student>> response = new ResponseEntity<List<Student>>
(studentList,HttpStatus.OK);
return response;
}
}
Here is the Postman Screen image
Here is the application console
2020-04-01 23:50:24.207 WARN 8400 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view
rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-04-01 23:50:24.704 INFO 8400 --- [ main]
o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService
'applicationTaskExecutor'
2020-04-01 23:50:25.458 INFO 8400 --- [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 5556
(http) with context path ''
2020-04-01 23:50:25.495 INFO 8400 --- [ main]
com.saif.NiceTry.NiceTryApplication : Started NiceTryApplication in
44.685 seconds (JVM running for 55.867)
2020-04-01 23:52:02.858 INFO 8400 --- [nio-5556-exec-2] o.a.c.c.C.[Tomcat].
[localhost].[/] : Initializing Spring DispatcherServlet
'dispatcherServlet'
2020-04-01 23:52:02.858 INFO 8400 --- [nio-5556-exec-2]
o.s.web.servlet.DispatcherServlet : Initializing Servlet
'dispatcherServlet'
2020-04-01 23:52:03.276 INFO 8400 --- [nio-5556-exec-2]
o.s.web.servlet.DispatcherServlet : Completed initialization in 417 ms
Help Me
With regards to the above postman image, you are using 5556 port. By default, Spring Boot uses the 8080 port number. You can however change the port that you are running in the properties file. In Spring Boot, properties are kept in the application.properties file under the classpath. The application.properties file is located in the src/main/resources directory.
[https://www.tutorialspoint.com/spring_boot/spring_boot_application_properties.htm][1]
[1]: Spring Boot

Hibernate takes so much time to load on application run

Hibernate is getting stuck when i run my application using ddl-auto 'update' or 'validate'
I have a local Oracle DataBase with a user (TEST) running on port 1521.
On the Spring application I have this on my application yml config file:
spring:
jpa:
show-sql: true
devtools:
add-properties: true
datasource:
jdbcUrl: 'jdbc:oracle:thin:#localhost:1521:test'
username: 'TEST'
password: 'Testing123'
driverClassName: 'oracle.jdbc.OracleDriver'
hibernate:
ddl-auto: "update"
dialect: 'org.hibernate.dialect.Oracle10gDialect'
When I run the application it gets stuck on these lines:
2019-07-19 13:30:39.043 INFO 74584 [ restartedMain] o.h.Version : HHH000412: Hibernate Core {5.3.10.Final}
2019-07-19 13:30:39.045 INFO 74584 [ restartedMain] o.h.c.Environment : HHH000206: hibernate.properties not found
2019-07-19 13:30:39.235 INFO 74584 [ restartedMain] o.h.a.c.r.j.JavaReflectionManager : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-07-19 13:30:39.475 INFO 74584 [ restartedMain] c.z.h.HikariDataSource : Test-H2DBPool - Starting...
2019-07-19 13:30:40.750 INFO 74584 [ restartedMain] c.z.h.HikariDataSource : Test-H2DBPool - Start completed.
2019-07-19 13:30:40.769 INFO 74584 [ restartedMain] o.h.d.Dialect : HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
If I change the hibernate ddl-auto to 'create-drop' it works perfect and it doesn't get stuck.
I tried to debug the application and I've tried to get some logs, but nothing is showing and I don't know what I could do. With 'create-drop' i get the SQL logs without problem.
Edit:
After waiting 40 minutes with the application running it has been executed and running well.
If I use the user SYS as SYSDBA when I run the application, Hibernate loads in 40 seconds more or less. Why with another user it takes so much time to load?

Categories