diff --git a/dbinit_sql/init.sql b/dbinit_sql/init.sql new file mode 100644 index 00000000..e69de29b diff --git a/process b/process new file mode 100644 index 00000000..e69de29b diff --git a/tools/dbinit.sh b/tools/dbinit.sh new file mode 100755 index 00000000..5b76f685 --- /dev/null +++ b/tools/dbinit.sh @@ -0,0 +1,100 @@ +#!/bin/bash + +# TODO +# - PostgreSQL configuration file/env variable +# (host, port, username, password, ..) + +# usage: +# `make create_database` +# create a `taler-exchange` database +# `make fetch_source` +# fetch all *.sql and *.sql.in files from GNU Taler exchange repository (latest commit) +# file `exchangedb.version` contains hash of latest commit that changed +# something in exchange/src/exchangedb +# `make 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="./dbinit_sql" +out_init_sql="${out_dir}/init.sql" +out_init_drop="${out_dir}/drop.sql" + +dbname="taler-exchange" + +fetch() { + git clone --depth 1 $taler_repo_url $tmp_dir + #cp /tmp/exchange/src/exchangedb/*.sql $out_dir/ + #cp /tmp/exchange/src/exchangedb/*.sql.in $out_dir/ +} + +process() { + sql_in_files=( + "procedures.sql" + "exchange-0002.sql" + "exchange-0003.sql" + "exchange-0004.sql" + ) + for file in "$sql_in_files"; do + file="$tmp_dir/src/exchangedb/${file}.in" + # 2>/dev/null \ + gcc -E -P -undef $file \ + | sed -e "s/--.*//" \ + | awk 'NF' - \ + > "${out_dir}/${file}" + done + # order of files is important + sql_files=( + "versioning.sql" + # "exchange-0001.sql" + # "exchange-0002.sql" + # "exchange-0003.sql" + # "exchange-0004.sql" + # "exchange-0005.sql" + "procedures.sql" ) + mkdir -p $out_dir + for file in "$sql_files"; do + cat "${out_dir}/${file}" > $out_init_sql + done +} + +if [[ $# -eq 0 ]]; then + echo "no arguments" >&2 + exit 1 +fi + +cmd=$1 +case "$cmd" in + fetch) fetch "$@"; exit 0;; + process) process "$@"; exit 0;; + *) echo "Unknown command: $cmd" >&2; exit 1;; +esac + +#clean: +# rm -f procedures.sql +# rm -f exchange-0002.sql +# rm -f exchange-0003.sql +# rm -f exchange-0004.sql +# rm $(sql_INIT) +# +## "NOTICE: function xxx() does not exist, skipping" are normal and expected +#init: gen_init +# psql --host=localhost --port=5432 --username=mte --password --dbname=$(dbname) --file=$(sql_INIT) +# +#drop: +# psql --host=localhost --port=5432 --username=mte --password --dbname=$(dbname) --file=$(sql_DROP) +# +#create_database: +# createdb --host=localhost --port=5432 --username=mte --password $(dbname) +# +#drop_database: +# dropdb --host=localhost --port=5432 --username=mte --password $(dbname) +# +#reset_database: drop_database create_database +#