How can I make my flyway default DB pointing to different db and my actual Spring boot Application to some other DB - spring-boot

I am trying to integrate flyway to my existing Spring boot app. My requirement is, the default table created by flyway needs to be created on different DB schema and my applications actual table migration must happen on different schema. Reason being, no addition table must be created on the applications DB schema. Currently we are using Mysql

We had a Spring Boot app with the flyway configuration in application.properties file for different environments:
flyway.enabled=true
flyway.url=jdbc:postgresql://localhost:5432/finance
flyway.schemas=user
flyway.password=password
flyway.user=postgres
flyway.baseline-on-migrate=true
We had the application-dev.properties, application-local.properties and application-aws.properties with the configuration specific to the environment

Flyway provides a property flyway.schemas. When the flyway.schemas property is set (multi-schema mode), the schema history table is placed in the first schema of the list.
https://flywaydb.org/documentation/commandline/migrate.html
ex:flyway.schemas=schema1,schema2,schema3

Related

Spring Boot h2 initial data not creating table first

I am trying to create a small Spring Boot setup with a h2 database.
Now I have a weird problem, which I can't solve.
If I don't create an data.sql for initial data, the app starts fine and creates my entity tables.
If I create an data.sql for initial data and keep the existing table from previous step, everything works fine.
If I create an data.sql for initial data and remove my existing h2 file, I get the error that it can't import the data, because the table is missing.
How do I tell Spring to create my tables before importing the initial data?
This is covered in the release notes for Spring Boot 2.5:
By default, data.sql scripts are now run before Hibernate is initialized. This aligns the behavior of basic script-based initialization with that of Flyway and Liquibase. If you want to use data.sql to populate a schema created by Hibernate, set spring.jpa.defer-datasource-initialization to true. While mixing database initialization technologies is not recommended, this will also allow you to use a schema.sql script to build upon a Hibernate-created schema before it’s populated via data.sql.

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

Load different schema.sql and data.sql for different datasources in spring boot

I have two datasources in my Spring Boot application. I want to initialize database at startup for local profile for both datasource with two different schema.sql and data.sql files. How to do it?
According to the section 85.3 Initialize a Database of the documentation:
Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts). It loads SQL from the standard root classpath locations: schema.sql and data.sql, respectively. In addition, Spring Boot processes the schema-${platform}.sql and data-${platform}.sql files (if present), where platform is the value of spring.datasource.platform. This allows you to switch to database-specific scripts if necessary. For example, you might choose to set it to the vendor name of the database (hsqldb, h2, oracle, mysql, postgresql, and so on).

spring boot hsqldb details

I have created two spring boot microservices for experimenting purposes, both use hsqldb. Is there a way to see the hsqldb details like what is the name of the db that spring boot created, username, password, what tables are there in each one and to query those tables.
You can use the built-in SQL functions and INFORMATION_SCHEMA views in HSQLDB to check these properties. See the Guide http://hsqldb.org/doc/2.0/guide/
But the quick and easy way is to use a file: database connection and check the *.script file of the database, which contains SQL statements that set the properties and create the tables.

Resources