#!/bin/sh # 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`. # Usage: # `fetch` # 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 # `init` # apply `init.sql` # (database must already be created) # `drop` # apply `drop.sql` set -e taler_repo_url="https://git-www.taler.net/exchange.git/" # todo # update to use taler exchange new build system and better *.sql organization taler_repo_tag="v1.5.0" tmp_dir="/tmp/taler_exchange" out_dir="./_dbinit_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 --branch $taler_repo_tag $taler_repo_url $tmp_dir } build() { 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 } database_init() { createdb --host=$host --port=$port --username=$username --password $dbname } database_drop() { dropdb --host=$host --port=$port --username=$username --password $dbname } init() { psql --host=$host --port=$port --username=$username --password --dbname=$dbname --file=$init_sql } drop() { psql --host=$host --port=$port --username=$username --password --dbname=$dbname --file=$drop_sql } if [[ $# -eq 0 ]]; then echo "no argument" >&2 exit 1 fi cmd=$1 case "$cmd" in fetch) fetch "$@"; exit 0;; build) build "$@"; exit 0;; database_init) database_init "$@"; exit 0;; database_drop) database_drop "$@"; exit 0;; init) init "$@"; exit 0;; drop) drop "$@"; exit 0;; *) echo "Unknown command: $cmd" >&2; exit 1;; esac