liqibase multiple schema update with single user credentials - spring-boot

I have requirements as below:
Use liquibase DB manager to update DB objects of multiple schemas using single elevated DB credentials
Every schema has different Datasoure objects
Every Schema should maintain their own liquibase tables (DATABASECHANGELOG and DATABASECHANGELOGLOCK)
Can anyone help with this?
I am using:
Springboot
HikariCP as connection pool
I tried setting
spring.liquibase.liquibase-schema
and
spring.liquibase.default-schema
configuration but not getting a way to set schema for each datasource.

Related

Spring Session Creation

spring.session.store-type=jdbc
# Database schema initialization mode.
spring.session.jdbc.initialize-schema=always
# Path to the SQL file to use to initialize the database schema.
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-mysql.sql
# Name of the database table used to store sessions.
spring.session.jdbc.table-name=SPRING_SESSION
spring.main.allow-bean-definition-overriding=true
The above is my configuration in application.properties. When the application runs, Spring creates a session and persists it automatically in the Spring_session and spring_session_attributes table. My question is, can I tell Spring to do not persist the session details into the Database, and instead I create and save it to the same tables using JPA?
Thank you.

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)

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.

Database table creation and updation with springboot, spring data

For the database schema management with spring data/hibernate, setting spring.jpa.hibernate.ddl-auto option doesn't look like a cleaner approach.
Bcoz
1) We are forced to put the credentials of a user that has permission to create and delete in the application.properties.
2) In production, relaying on spring.jpa.hibernate.ddl-auto option could lead to dataloss, if not managed carefully.
So what is the best way out there to handle the database schema management automatically in a spring boot/spring data app?
Any pointers would help.
Thanks
If you want to track each change state of database then you can use flyway
See this link how to maintain database versioning in spring-boot
In production, you should ideally set spring.jpa.hibernate.ddl-auto property as none so that no schema changes are allowed.
For the database schema management with spring data/hibernate, I would suggest you go for Liquibase, it basically is an open source database-independent library for tracking, managing and applying database schema changes.
Every change to Schema is added as a changeset using property file in Liquibase , this is for the new changes.
In order to migrate the existing database structure into Liquibase, it provides you with commands to automatically generate Changesets by reading the current database.
So using this you can generate database schema, add constraints, load data.
More info at : https://www.liquibase.org/ , Why and when Liquibase?

HSQL Unit Test -- How to Create Multiple In-Memory Schemas?

I would like to use hsql within my DAO unit tests for a web application. The web app is written against mysql and uses three different schemas within the same mysql database. Some schemas has FK relationships with data in the other schemas. If I'm to unit test, I must be able to execute against a database that can hold multiple schemas.
I know that HSQL supports multiple schemas, but I don't know how to configure hsql to have multiple schemas set up for an in-memory database. I read that I can define multiple schemas in the server.properties file, but the file needs to be in the location of where the java class was called -- the junit.jar location? If so, that would be hard to support in my Java Maven application. How can I:
Run an in-memory hsql database to start up with three databases?
Where would I place the server.properties file in my Maven app?
Could I point hsql to use a server.properties file in a location other than where the junit jar is (that's a showstopper for me)?
Is it possible to configure multiple schemas for an in-memory database just via a tricked out jdbc url?
I wish I could untangle the schemas from each other, but that's not possible at this time.
Thanks for your help!
HSQLDB supports multiple schemas in the same database. Foreign keys can reference tables from different schemas. The following apply to the very latest HSQLDB 2.2.6 snapshot available from http://hsqldb.org
Before running your tests, execute CREATE SCHEMA schemaname for each schema.
Doesn't matter where, you can specify the absolute path on the command line arguments when running. See the HSQLD Guide and JavaDoc on server.
Yes.
No. You use the SQL statement to create the schemas.
Note you have two options for running HSQLDB, one is as a server, the other is as an embedded database. In the case of server, it must be started before the test run. In both cases, you need to connect to the database and create the schemas before your tests.
It is possible to create different db by setting db name. By default, it creates the db name as testdb, but in case we want to create multiple db, then set the name explicitly.
new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL).setName("DB_NAME")
.addScript("DDL.SQL")
.addScript("DML.SQL")
.build();
If you run the below line mutiple time, you can see the databases:
DatabaseManagerSwing.main(new String[] { "--url", "jdbc:hsqldb:mem:" + dbnaes, "--user", "sa", "--password", "" });

Resources