libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

libeufin-load-sql (3346B)


      1 #!/bin/bash
      2 
      3 # NOTE: THIS FILE CONSIDERS _ONLY_ THE OBSOLETE NEXUS
      4 # SQL FILES.  THIS FILE WILL BE DISCARDED AS SOON AS NEXUS
      5 # WILL GET ITS SQL REFACTORED.
      6 
      7 set -eu
      8 
      9 fail () {
     10   echo $1
     11   exit 1
     12 }
     13 
     14 usage_and_exit () {
     15   echo Usage: libeufin-load-sql OPTIONS
     16   echo
     17   echo By default, this command creates and/or patches the LibEuFin tables.
     18   echo One particular LibEuFin service could be selected via the '-s' option.
     19   echo Pass '-r' to delete tables and schemas.
     20   echo
     21   echo 'Supported options:'
     22   echo "  -s SERVICE  -- specify 'sandbox' or 'nexus', according to which set of tables are to be setup or dropped.  If missing both sets will be setup or dropped on the same database."
     23   echo '  -d DB_CONN  -- required.  Pass DB_CONN as the postgres connection string.  Passed verbatim to Psql'
     24   echo '  -l LOC      -- required.  Pass LOC as the SQL files location.  Typically $prefix/share/libeufin/sql'
     25   echo '  -h           -- print this help'
     26   echo '  -r           -- drop all the tables and schema(s)'
     27   exit 0
     28 }
     29 
     30 run_sql_file () {
     31   # -q doesn't hide all the output, hence the
     32   # redirection to /dev/null.
     33   psql -d $DB_CONNECTION \
     34     -q \
     35     -f $1 \
     36     --set ON_ERROR_STOP=1 > /dev/null
     37 }
     38 
     39 get_patch_path () {
     40   echo "$PATCHES_LOCATION/$1"
     41 }
     42 
     43 # The real check happens (by the caller)
     44 # by checking the returned text.
     45 check_patch_applied () {
     46   psql -d $DB_CONNECTION \
     47   -t \
     48   -c "SELECT applied_by FROM _v.patches WHERE patch_name = '$1' LIMIT 1"
     49 }
     50 
     51 # Iterates over the .sql migration files and applies
     52 # the new ones.
     53 iterate_over_patches () {
     54   component="$1"
     55   cd $PATCHES_LOCATION
     56   for patch_filename in $(ls -1 -v $component-[0-9][0-9][0-9][0-9].sql); do
     57     patch_name=$(echo $patch_filename | cut -f1 -d.) # drops the final .sql
     58     echo Checking patch: "$patch_name"
     59     maybe_applied=$(check_patch_applied "$patch_name")
     60     if test -n "$maybe_applied"; then continue; fi
     61     # patch not applied, apply it.
     62     echo Patch $patch_name not applied, applying it.
     63     run_sql_file $patch_filename
     64   done
     65   cd - > /dev/null # cd to previous location.
     66 }
     67 
     68 if test $# -eq 0; then
     69   usage_and_exit
     70 fi
     71 
     72 while getopts ":d:l:hs:r" OPTION; do
     73   case "$OPTION" in 
     74     d)
     75       DB_CONNECTION="$OPTARG" # only one required.
     76       ;;
     77     l)
     78       PATCHES_LOCATION="$OPTARG"
     79       ;;
     80     s)
     81       if test "$OPTARG" != sandbox -a "$OPTARG" != nexus; then
     82         fail "Invalid -s value: $OPTARG.  Please pass 'sandbox' or 'nexus'."
     83       fi
     84       SERVICE="$OPTARG"
     85       ;;
     86     r)
     87       DROP="YES"
     88       ;;
     89     h)
     90       usage_and_exit
     91       ;;
     92     ?)
     93       fail 'Unrecognized command line option'
     94     ;;
     95   esac
     96 done
     97 
     98 # Checking required options.
     99 if test -z "${PATCHES_LOCATION:-}"; then
    100   # This value is substituted by GNU make at installation time.
    101   PATCHES_LOCATION=__STATIC_PATCHES_LOCATION__
    102 fi
    103 if test -z "${DB_CONNECTION:-}"; then
    104   fail "Required option '-d' was missing."
    105 fi
    106 
    107 run_sql_file $(get_patch_path "versioning.sql")
    108 
    109 if test -z "${SERVICE:-}"; then # impact both services.
    110   # Maybe drop.
    111   if test "${DROP:-}" = "YES"; then
    112     run_sql_file $(get_patch_path "nexus-drop.sql")
    113     exit 0
    114   fi
    115   iterate_over_patches nexus
    116   exit 0
    117 fi
    118 
    119 # Maybe drop
    120 if test "${DROP:-}" = "YES"; then
    121   run_sql_file $(get_patch_path "${SERVICE}-drop.sql")
    122   exit 0
    123 fi
    124 iterate_over_patches $SERVICE # helper checks the argument sanity.