From 086102c2fb2e523969b93177351014e0dc66cdd7 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Mon, 26 Aug 2019 01:02:49 +0200 Subject: [PATCH] add: schema migration scripts --- Dockerfile | 1 + schema_migration.sh | 24 ++++++++++++++++++++++++ sql/migration_0.sql | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100755 schema_migration.sh create mode 100644 sql/migration_0.sql diff --git a/Dockerfile b/Dockerfile index 2a5824f..043c319 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,7 @@ RUN apt-get update -yqq \ && locale-gen en_US.UTF-8 \ && cd /opt/ \ && wget -qO- https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/6.0.0/flyway-commandline-6.0.0-linux-x64.tar.gz | tar xvz \ + && sudo chmod +x /opt/flyway-6.0.0/flyway \ && sudo ln -s /opt/flyway-6.0.0/flyway /usr/local/bin ENV LANG=en_US.UTF-8 \ diff --git a/schema_migration.sh b/schema_migration.sh new file mode 100755 index 0000000..2393927 --- /dev/null +++ b/schema_migration.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +sqlite_return=$(sqlite3 core.sqlite "SELECT version_num FROM meta LIMIT 1;" 2>/dev/null); + +if ! [[ $sqlite_return =~ ^-?[0-9]+$ ]] +then + echo "No version number found. Initiating database." + sqlite_return=0; +fi; + +version_num=$sqlite_return; + +while [ -f sql/migration_$version_num.sql ] +do + sqlite3 core.sqlite < sql/migration_$version_num.sql; + ((version_num++)); +done; + +if [ $sqlite_return -eq 0 ] +then + sqlite3 core.sqlite "INSERT INTO meta (version_num) VALUES ($version_num); "; +else + sqlite3 core.sqlite "UPDATE meta SET version_num=$version_num;"; +fi \ No newline at end of file diff --git a/sql/migration_0.sql b/sql/migration_0.sql new file mode 100644 index 0000000..b81d189 --- /dev/null +++ b/sql/migration_0.sql @@ -0,0 +1,38 @@ +create table meta +( + version_num int not null +); + +create table controllers +( + id VARCHAR(33) not null + primary key + unique, + name VARCHAR(128), + ip VARCHAR(16), + port INTEGER, + relay_count INTEGER, + active BOOLEAN not null +); + +create table relays +( + id INTEGER not null + primary key + unique, + name VARCHAR(128), + number INTEGER not null, + controller_id VARCHAR(33) not null + references controllers (id), + active_schedule_id VARCHAR(33) + references schedules +); + +create table schedules +( + id VARCHAR(33) not null + primary key + unique, + name VARCHAR(128), + periods BLOB +);