mte/tools/dbinit.sh

111 lines
2.6 KiB
Bash
Raw Permalink Normal View History

2026-05-12 02:23:46 +02:00
#!/bin/sh
2026-02-10 07:59:24 +01:00
2026-05-12 02:23:46 +02:00
# Script to initialize a taler-exchange database.
# To use as an alternative to the taler-exchange-dbinit tool.
#
# This script aggregate GNU Taler exchange's SQL files together,
# and send commands to PostgreSQL server with `psql`.
2026-02-10 07:59:24 +01:00
2026-05-12 02:23:46 +02:00
# Usage:
2026-02-10 07:59:24 +01:00
# `fetch`
2026-05-12 02:23:46 +02:00
# fetch all *.sql and *.sql.in files from GNU Taler exchange repository
# `build`
# merge *.sql and *.sql.in files to produce `init.sql` and `drop.sql`
# `database_init`
# create a `taler-exchange` database
# `database_drop`
# drop `taler-exchange` database
2026-02-10 07:59:24 +01:00
# `init`
2026-05-12 02:23:46 +02:00
# apply `init.sql`
# (database must already be created)
# `drop`
# apply `drop.sql`
2026-02-10 07:59:24 +01:00
set -e
taler_repo_url="https://git-www.taler.net/exchange.git/"
2026-05-12 02:23:46 +02:00
# todo
# update to use taler exchange new build system and better *.sql organization
taler_repo_tag="v1.5.0"
2026-02-10 07:59:24 +01:00
tmp_dir="/tmp/taler_exchange"
2026-05-12 02:23:46 +02:00
out_dir="./_dbinit_sql"
2026-02-10 07:59:24 +01:00
init_sql="${out_dir}/init.sql"
drop_sql="${out_dir}/drop.sql"
# psql parameter
host="10.0.0.1"
2026-02-10 07:59:24 +01:00
port=5432
username="mte"
dbname="taler-exchange"
fetch() {
2026-05-12 02:23:46 +02:00
git clone --depth 1 --branch $taler_repo_tag $taler_repo_url $tmp_dir
2026-02-10 07:59:24 +01:00
}
2026-05-12 02:23:46 +02:00
build() {
2026-02-10 07:59:24 +01:00
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
}
2026-05-12 02:23:46 +02:00
database_init() {
createdb --host=$host --port=$port --username=$username --password $dbname
2026-02-10 07:59:24 +01:00
}
2026-05-12 02:23:46 +02:00
database_drop() {
dropdb --host=$host --port=$port --username=$username --password $dbname
2026-02-10 07:59:24 +01:00
}
2026-05-12 02:23:46 +02:00
init() {
psql --host=$host --port=$port --username=$username --password --dbname=$dbname --file=$init_sql
2026-02-10 07:59:24 +01:00
}
2026-05-12 02:23:46 +02:00
drop() {
psql --host=$host --port=$port --username=$username --password --dbname=$dbname --file=$drop_sql
2026-02-10 07:59:24 +01:00
}
if [[ $# -eq 0 ]]; then
echo "no argument" >&2
exit 1
fi
cmd=$1
case "$cmd" in
fetch) fetch "$@"; exit 0;;
2026-05-12 02:23:46 +02:00
build) build "$@"; exit 0;;
database_init) database_init "$@"; exit 0;;
database_drop) database_drop "$@"; exit 0;;
2026-02-10 07:59:24 +01:00
init) init "$@"; exit 0;;
2026-05-12 02:23:46 +02:00
drop) drop "$@"; exit 0;;
2026-02-10 07:59:24 +01:00
*) echo "Unknown command: $cmd" >&2; exit 1;;
esac