mte/tools/dbinit.sh

100 lines
2.3 KiB
Bash
Executable file

#!/bin/bash
# TODO
# use specific commit
# usage:
# `create_database`
# create a `taler-exchange` database
# `fetch`
# fetch all *.sql and *.sql.in files from GNU Taler exchange repository (latest commit)
# `init`
# initialize taler-exchange database (create tables, ...)
# `taler-exchange` database must already be created
#
set -e
taler_repo_url="https://git-www.taler.net/exchange.git/"
tmp_dir="/tmp/taler_exchange"
out_dir="./_taler_exchange_sql"
init_sql="${out_dir}/init.sql"
drop_sql="${out_dir}/drop.sql"
# psql parameter
host="10.0.0.1"
port=5432
username="mte"
dbname="taler-exchange"
fetch() {
git clone --depth 1 $taler_repo_url $tmp_dir
}
process() {
source_dir="${tmp_dir}/src/exchangedb"
sql_in_files=(
"procedures.sql"
"exchange-0002.sql"
"exchange-0003.sql"
"exchange-0004.sql"
)
for file in "${sql_in_files[@]}"; do
file_in="${source_dir}/${file}.in"
file_out="${source_dir}/${file}"
gcc -E -P -undef -I "$source_dir" - < "$file_in" \
2>/dev/null \
> "$file_out"
done
# order is important
files=(
"versioning.sql"
"exchange-0001.sql"
"exchange-0002.sql"
"exchange-0003.sql"
"exchange-0004.sql"
"exchange-0005.sql"
"procedures.sql"
)
mkdir -p "$out_dir"
cat "${source_dir}/drop.sql" > "$drop_sql"
echo "" > "$init_sql"
for file in "${files[@]}"; do
cat "${source_dir}/${file}" >> "$init_sql"
done
}
# ? "NOTICE: function xxx() does not exist, skipping"
init() {
psql --host=$host --port=$port --username=$username --password --dbname=$dbname --file=$init_sql
}
drop_schema() {
psql --host=$host --port=$port --username=$username --password --dbname=$dbname --file=$drop_sql
}
create_database() {
createdb --host=$host --port=$port --username=$username --password $dbname
}
drop_database() {
dropdb --host=$host --port=$port --username=$username --password $dbname
}
if [[ $# -eq 0 ]]; then
echo "no argument" >&2
exit 1
fi
cmd=$1
case "$cmd" in
fetch) fetch "$@"; exit 0;;
process) process "$@"; exit 0;;
init) init "$@"; exit 0;;
drop_schema) drop_schema "$@"; exit 0;;
create_database) create_database "$@"; exit 0;;
drop_database) drop_database "$@"; exit 0;;
*) echo "Unknown command: $cmd" >&2; exit 1;;
esac