summaryrefslogtreecommitdiff
path: root/contrib/libeufin-load-sql
blob: 963ad22e742fd171d8bb8c3eb334b000fc822a2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash

set -eu

# The only CLI argument is 'nexus' or 'sandbox',
# indicating which service will get its database prepared.

fail () {
  echo $1
  exit 1
}

usage_and_exit () {
  echo Usage: libeufin-load-sql OPTIONS
  echo
  echo By default, this command creates and/or patches the LibEuFin tables.
  echo One particular LibEuFin service could be selected via the '-s' option.
  echo Pass '-r' to delete tables and schemas.
  echo
  echo 'Supported options:'
  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."
  echo '  -d DB_CONN  -- required.  Pass DB_CONN as the postgres connection string.  Passed verbatim to Psql'
  echo '  -l LOC      -- required.  Pass LOC as the SQL files location.  Typically $prefix/share/libeufin/sql'
  echo '  -h           -- print this help'
  echo '  -r           -- drop all the tables and schema(s)'
  exit 0
}

run_sql_file () {
  # -q doesn't hide all the output, hence the
  # redirection to /dev/null.
  psql -d $DB_CONNECTION \
    -q \
    -f $1 \
    --set ON_ERROR_STOP=1 > /dev/null
}

get_patch_path () {
  echo "$PATCHES_LOCATION/$1"
}

# The real check happens (by the caller)
# by checking the returned text.
check_patch_applied () {
  psql -d $DB_CONNECTION \
  -t \
  -c "SELECT applied_by FROM _v.patches WHERE patch_name = '$1' LIMIT 1"
}

# Iterates over the .sql migration files and applies
# the new ones.
iterate_over_patches () {
  component="$1"
  cd $PATCHES_LOCATION
  for patch_filename in $(ls -1 -v $component-[0-9][0-9][0-9][0-9].sql); do
    patch_name=$(echo $patch_filename | cut -f1 -d.) # drops the final .sql
    echo Checking patch: "$patch_name"
    maybe_applied=$(check_patch_applied "$patch_name")
    if test -n "$maybe_applied"; then continue; fi
    # patch not applied, apply it.
    echo Patch $patch_name not applied, applying it.
    run_sql_file $patch_filename
  done
  cd - > /dev/null # cd to previous location.
}

if test $# -eq 0; then
  usage_and_exit
fi

while getopts ":d:l:hs:r" OPTION; do
  case "$OPTION" in 
    d)
      DB_CONNECTION="$OPTARG" # only one required.
      ;;
    l)
      PATCHES_LOCATION="$OPTARG"
      ;;
    s)
      if test "$OPTARG" != sandbox -a "$OPTARG" != nexus; then
        fail "Invalid -s value: $OPTARG.  Please pass 'sandbox' or 'nexus'."
      fi
      SERVICE="$OPTARG"
      ;;
    r)
      DROP="YES"
      ;;
    h)
      usage_and_exit
      ;;
    ?)
      fail 'Unrecognized command line option'
    ;;
  esac
done

# Checking required options.
if test -z "${PATCHES_LOCATION:-}"; then
  # This value is substituted by GNU make at installation time.
  PATCHES_LOCATION=__STATIC_PATCHES_LOCATION__
fi
if test -z "${DB_CONNECTION:-}"; then
  fail "Required option '-d' was missing."
fi

run_sql_file $(get_patch_path "versioning.sql")

if test -z "${SERVICE:-}"; then # impact both services.
  # Maybe drop.
  if test "${DROP:-}" = "YES"; then
    run_sql_file $(get_patch_path "sandbox-drop.sql")
    run_sql_file $(get_patch_path "nexus-drop.sql")
    exit 0
  fi
  iterate_over_patches sandbox
  iterate_over_patches nexus
  exit 0
fi

# Maybe drop
if test "${DROP:-}" = "YES"; then
  run_sql_file $(get_patch_path "${SERVICE}-drop.sql")
  exit 0
fi
iterate_over_patches $SERVICE # helper checks the argument sanity.