How to create H2 in-memory database using H2 embedded database - java

I'm currently writing some test cases using testng. I have a requirement to repeat same test set in H2 embedded db and in H2 in-memory db. What i want is to copy all tables and data from H2 embedded db to H2 in-memory db upon transition from one db type to another.
Is it possible ? thanks in advance.

you could use the script tool from here
http://h2database.com/javadoc/org/h2/tools/Script.html
to export the tables and data and then use runscript as descibed here
http://h2database.com/html/grammar.html#runscript
to import it in the other db

Related

How to re-run hbm2ddl in between unit tests for H2 database?

In production, we use SQL scripts (Postgres) to generate / update the schema. In my unit tests, I use configProperties.put("hibernate.hbm2ddl.auto", "create-drop"); in order to create the schema for my H2 database.
In order to isolate the unit tests better, I want to drop and recreate the database in between tests. I can wipe the H2 database with entityManager.createNativeQuery("DROP ALL OBJECTS").executeUpdate();, but how do I then get hibernate to re-create the schema?
Or is there an easy way to simply shut down H2 and create a new instance? I can create multiple instances by using "jdbc:h2:mem:"+UUID.randomUUID()+";MODE=DB2;" as my connection string, but I'm not sure how to shut down / destroy / clean up previous instances.

Unit tests with SQL

I am doing a software project, working with database and Java code and in the unit tests, we have to test the selects....I mean, the expectable is that the table that results from the selects, have to correspond with the output in the intellij.....
in a few words, how do I compare a table originated from SQL with output(system.out.println())
You can use an embedded database for that (for example H2).
The workflow would be as follows:
Define a schema for your tables and any initial values.
Start the embedded database at the beginning of your tests and populate it with the defined schema.
Connect to the embedded database from your test code, as you would connect to any database.
Run your SQL against the embedded database.
Do asserts on the results fetched from the database.
Stop the embedded database.
You can find a H2 tutorial here: https://www.tutorialspoint.com/h2_database/h2_database_jdbc_connection.htm

Spring boot load H2 db file from classpath

I want to package prepopulated h2 db-files with a Spring boot application. The database is only going to be used to read values.
How am I able to load the H2 files that are available on the classpath? I am very uncertain how to setup the spring.datasource.url to get this to work.
I would suggest you export the schema and data into SQL scripts and use the in-memory H2 DB in combination with Spring Boot's built-in initialization feature: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-spring-jdbc
Granted, it may result in a slower startup if you have a relatively big dataset but if that were the case you probably wouldn't have put it in the JAR in the first place :)

H2 RUNSCRIPT equivalent in HSQL

In H2, it is easy to setup in-mem db for unit testing via RUNSCRIPT command as part of connection url itself (No hibernate & Spring required) to set it up.
h2 sample
jdbc:h2:mem:sample;INIT=RUNSCRIPT FROM 'classpath:scripts/create.sql'\\;RUNSCRIPT FROM 'classpath:scripts/create_2.sql'
I am trying to understand if there is a way to have a similar setup for hsqldb too? No success till now. If it is in documentation, point me to the specific link please.
Constraints:
HSQL should be in-memory only.
No Spring and Hibernate should be used.
Thanks
HSQLDB supports a memory database that is read from file, with no change automatically written to file.
Create the memory database with tables and required initial data, then save it with the SRCIPT 'filename' command.
Then open it as a file readonly database:
jdbc:hsqldb:file:filename;files_readonly=true

How do I mimic Hibernate hbm2ddl "create" behaviour in Liquibase?

I've worked with liquibase 1.9.5 for a while now and got it to replace hibernate hbm2ddl strategy of creating tables and loading fixtures in it. Since it's a maven project and since I use hsqldb (using file create=true), I simply create the db in the target folder so that I have a fresh database anytime I test the application. Works fine till that I realize:
1 I will need the database to be recreated when doing integration test using mysql database now
2 I will definitely need the same solution for a non maven project.
So basically how do I drop and create the database when using liquibase as opposed to hbm2ddl?
The easiest way is to add a separate database call before liquibase update that runs the sql
DROP DATABASE X;
CREATE DATABASE X
Liquibase does have a dropAll command which can be used to drop everything in a schema, but it is slower than drop/create database on mysql and may miss some database objects.

Categories