Flyway: Prevent migration on empty database? - spring-boot

I'm working on a spring boot application that uses Camunda
I've just bumped our Camunda version by a couple of minor versions, and I'm running incremental SQL scripts with Flyway to update the DB to support the new camunda version (v1.02--7.8-7.9.sql and v1.03--7.9>7.10.sql)
This migration works fine on existing databases that are currently on Camunda 7.8, but it fails when I try to run the application on a clean database.
One of the scripts tries to add a column:
ALTER TABLE ACT_ID_USER
ADD LOCK_EXP_TIME_ timestamp;
But, if the application is running on a clean database, it will complain that the column is already present - presumably because the database has just been initialised with 9.10, so running a 9.8 migration after the fact doesn't work
I'm wondering if there's a way for Flyway to not run those migrations, if the database is empty on startup?
Or, is my problem the way I'm using Flyway? Should flyway be getting executed before Camunda initialises the Database?

Related

using Flyway for MongoDb in Spring

I use Flyway in Spring project that has 2 databases: mysql and mongodb. I want to keep order of migrations to both data sources and be able to move data from one to other one (use mysql+mongo in one migration).
Is it ok to use Flyway java-based-migrations for mongodb migrations?
IMO it's completely fine to run MongoDB migrations as a flyway java migration, you only need to be sure that the migration is idempotent and prepare for possible failures, unlike SQL migrations Flyway won't perform a rollback if the transaction fails in the middle.
Another alternative would be Mongobee, it's like Flyway but for MongoDB. But it also relies on java based migrations.

Programmatically recreate H2 database schema in SpringBoot application (not while unit testing)?

I have a SpringBoot application with in memory H2 database and Spring Data JPA.
I need to configure a #Scheduled job that drops and recreates the schema and loads it with fresh data from a file.
How can I programmatically recreate the schema in my application?
You can use database version control tool like eg Liquibase to create and maintain database schema definition as well as initial data. Than, you will be able to easily invoke database migration including drop of whole schema during applicaiton runtime. IT has some integration with Spring Boot already.
Keep in mind, that you will have to lock database access in order to execute migration - DDL is not transactional, so database will be of no use anyway during the migration process and you app can yeld many errors during that time.
If locking is not an option - you should be able to create another instance or at least separate schema in running instance, run migration against it and if everything is done, "switch" peristence context to use brand new schema (and probably remove the old one)

Automatically baselining a database with Flyway in a Spring Boot project

I'm trying to add Flyway to a Spring Boot project. In accordance with the instructions, I've created my initial DDL script and committed it to src/main/resources/db/migration/V1__base_version.sql.
If I run the baseline command, this will create the flyway_schema_history table and set the baseline version therein to 1.
While this works fine for my local database, I would like this to happen automatically on the other developers' local environments, UAT environment, etc.
I tried adding the following property to the Spring Boot config
spring:
flyway:
baseline-on-migrate: true
I expected this to do the same thing as the baseline command when the Spring Boot app starts up if the flyway_schema_history table does not exist, i.e. create this table and insert a row specifying the current schema version, but it didn't.
Is there a way to automatically baseline the database when the app starts up?

Spring Boot: Executing the Newer version of SQL file each time we rebuild the application

I have a spring-boot application with PostgreSQL. Some of the tables are created using models and other tables have to be created prior to the start of the application or while starting the application. That can be done by running an SQL file while startup.
But DB tend to change over time, we may have to alter some of the tables, add some new tables without disturbing the existing data in the tables, etc.
Is there a way to add new SQL files, and run only the SQL files which was not run in spring-boot application each time when we rebuild and rerun? And don't run any of the SQL files while start-up if everything were already executed?
For your scenario liquibase is the best solution. You can merge liquibase on your spring boot application.
Ex: https://javadeveloperzone.com/spring-boot/spring-boot-liquibase-example/
You can use flyway. It allows you to have versioned sql scripts:
flywaydb.org
flywaydb spring boot plugin
Examples:
Spring Boot Database Migrations with Flyway
Incrementally changing your db with java and flyway

Flyway with spring boot overwrites whole DB every time I switch run mode between WAR and IDE run

I'm facing very weird issue while integrating flyway DB migration with spring boot application.
When I run the application from executable WAR using command line, it creates new DB at the start-up of application.
Now, If I switch the application run mode to IDE (i.e. run from STS), it again fires all the script from my db/migration folder. I can see the installed_on column time changes every-time I switch between these 2 run modes. I have tried enabling baselineOnMigrate property, but didn't get any effect of it.
Do you think its something related to spring boot embedded tomcat ? because at both run it creates individual tomcat which is embedded.
Please find my spring boot application.properties below:
mssql.dbname=issueDB
mssql.password=password
mssql.dbserver=localhost
mssql.port=1501
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://${mssql.dbserver}:${mssql.port};databaseName=${mssql.dbname}
spring.datasource.username=user
spring.datasource.password=${mssql.password}
spring.flyway.baselineOnMigrate=true
spring.flyway.locations=classpath:db/migration/testissue
spring.flyway.out-of-order=true
spring.flyway.baseline-version=1.3
spring.flyway.placeholder-prefix=$
spring.flyway.placeholder-suffix=$
spring.flyway.mixed=true
spring.flyway.cleanOnValidationError=true
I suppose, it could be caused by this property spring.flyway.cleanOnValidationError=true. According to the docs:
Whether to automatically call clean or not when a validation error occurs.
This is exclusively intended as a convenience for development. Even tough we strongly recommend not to change migration scripts once they have been checked into SCM and run, this provides a way of dealing with this case in a smooth manner. The database will be wiped clean automatically, ensuring that the next migration will bring you back to the state checked into SCM.
May be that you got some validation problems if you are running your application in different ways on the same database and flyway just clean your database and overwrite it with the current scripts state.

Resources