add: schema migration scripts

This commit is contained in:
Tobias Reisinger 2019-08-26 01:02:49 +02:00
parent ea8cb566e9
commit 086102c2fb
3 changed files with 63 additions and 0 deletions

View file

@ -8,6 +8,7 @@ RUN apt-get update -yqq \
&& locale-gen en_US.UTF-8 \ && locale-gen en_US.UTF-8 \
&& cd /opt/ \ && 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 \ && 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 && sudo ln -s /opt/flyway-6.0.0/flyway /usr/local/bin
ENV LANG=en_US.UTF-8 \ ENV LANG=en_US.UTF-8 \

24
schema_migration.sh Executable file
View file

@ -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

38
sql/migration_0.sql Normal file
View file

@ -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
);