From ab1c64525d64d62e64654cc93365e9479b537879 Mon Sep 17 00:00:00 2001 From: swrup Date: Tue, 10 Feb 2026 07:59:24 +0100 Subject: [PATCH] add dbinit.sh --- .gitignore | 2 + tools/dbinit.sh | 100 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100755 tools/dbinit.sh diff --git a/.gitignore b/.gitignore index dbd4866f..0ffd3a74 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ _build data/secmod/* !data/secmod/.gitkeep + +taler_exchange_sql diff --git a/tools/dbinit.sh b/tools/dbinit.sh new file mode 100755 index 00000000..ae85d04a --- /dev/null +++ b/tools/dbinit.sh @@ -0,0 +1,100 @@ +#!/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="localhost" +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