This is a quick tip for usage of Liquibase with HSQL. You may use the former tool for relational database schema migrations and the latter as in-memory database on the developers’ machines. Today, with feature branches and project customisation, i.e. changes that are specific for a single customer, you may have to shutdown the database, restart and reapply the migrations on a regular basis. Of course, this is a task that can be automated!
Typical situation: you are working on the project’s main development branch, but you now wish to review a colleague’s new feature, which has been developed in a feature branch. You switch to the feature branch, start the server, open the application and BAM, an exception occurs due a failed schema validation. The reason: your colleague made some schema changes in the feature branch that you forgot to apply.
Improving your Liquibase and HSQL workflow is rather simple: restart the database whenever the schema changes and apply the migrations! This is how you can do it in bash (sorry Windows users).
1#!/bin/bash
2set -e
3
4HSQL_PID=""
5PROJECT=""
6
7if [[ $1 = "customisation" ]]; then
8 echo "Using customer specific changelogs..."
9 PROJECT="$PROJECT_REPO/customisation"
10else
11 echo "Using default changelogs..."
12 PROJECT="$PROJECT_REPO/database"
13fi
14
15control_c() {
16 kill $HSQL_PID
17 exit
18}
19trap control_c SIGHUP SIGINT SIGTERM 0
20
21start() {
22 # Start HSQL
23 java -cp "$M2_REPO/org/hsqldb/hsqldb/2.2.9/hsqldb-2.2.9.jar" \
24 org.hsqldb.Server \
25 -database.0 mem:myDB \
26 -dbname.0 myDB &
27 HSQL_PID=$!
28
29 # Schema update
30 mvn -f "$PROJECT/pom.xml" liquibase:update -Plocal --no-snapshot-updates
31}
32
33start
34
35while true; do
36 if hash inotifywait 2>/dev/null; then
37 WATCH_DIR="$PROJECT/src/changelogs"
38 echo "Watching $WATCH_DIR for changes..."
39
40 # Wait for changelog changes
41 inotifywait --recursive $WATCH_DIR \
42 --event modify \
43 --event move \
44 --event create \
45 --event delete
46
47 # Just a precaution: wait until the VCS is done changing the branch
48 sleep 1
49 else
50 echo "[Press any key to discard the schema and apply a new one...]"
51 read
52 fi
53
54 # kill HSQL and wait until it is actually shut down
55 kill $HSQL_PID
56 while kill -0 "$HSQL_PID"; do
57 sleep 0.1
58 done
59
60 start
61done
You need to make sure that you have two environment variables setup. There should be a variable PROJECT_REPO which points to the root directory of your project’s repository and M2_REPO which points to the Maven repository. You may also need to tweak a few paths, e.g. the version of HSQL that you are using as well as the Maven project and profile names. At last, you should install the inotify-tools (unfortunately not available on OS X). inotifywait (part of the inotify-tools) is used to watch the changelog directory for changes. The project’s Wiki has a section on installation .
Once setup, usage is pretty easy: start the script and let it do the rest. When the inotify-tools are not installed, you need to switch to your terminal and hit “the any key” in order to discard the current and apply a new schema.
More articles
fromBen Ripkens
Your job at codecentric?
Jobs
Agile Developer und Consultant (w/d/m)
Alle Standorte
More articles in this subject area
Discover exciting further topics and let the codecentric world inspire you.
Gemeinsam bessere Projekte umsetzen.
Wir helfen deinem Unternehmen.
Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.
Hilf uns, noch besser zu werden.
Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.
Blog author
Ben Ripkens
Do you still have questions? Just send me a message.
Do you still have questions? Just send me a message.