mte/tools/dbinit.sh

114 lines
2.8 KiB
Bash
Raw Normal View History

2026-02-10 07:59:24 +01:00
#!/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() {
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 - < "$file_in" \
2>/dev/null \
| sed -e "s/--.*//" \
| awk 'NF' - \
> "$file_out"
done
# order is important
files=(
"versioning.sql"
"exchange-0001.sql"
# "0001-*.sql"
"exchange-0002.sql"
"0002-*.sql"
"exchange-0003.sql"
"0003-*.sql"
"exchange-0004.sql"
"0004-*.sql"
"exchange-0005.sql"
# "0005-*.sql"
"procedures.sql"
"exchange_do_*.sql"
"exchange_statistics_*.sql"
"exchange_trigger_*.sql"
)
mkdir -p "$out_dir"
echo "" > $out_init_sql
for patern in ${files[@]}; do
# '*'-expension
cat ${source_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
#