-
Notifications
You must be signed in to change notification settings - Fork 3
/
remove_current_period.sh
executable file
·31 lines (22 loc) · 1.1 KB
/
remove_current_period.sh
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
#!/usr/bin/env bash
set -e
# ensure some environment variables are set
: "${DATABASE_URL:?DATABASE_URL not set in environment}"
: "${POSTGRES_USER:?POSTGRES_USER not set in environment}"
: "${POSTGRES_PASSWORD:?POSTGRES_PASSWORD not set in environment}"
# pull apart some of those pieces stuck together in DATABASE_URL
URL=`echo ${DATABASE_URL} | sed -E 's/^jdbc\:(.+)/\1/'` # jdbc:<url>
: "${URL:?URL not parsed}"
HOST=`echo ${DATABASE_URL} | sed -E 's/^.*\/{2}(.+):.*$/\1/'` # //<host>:
: "${HOST:?HOST not parsed}"
PORT=`echo ${DATABASE_URL} | sed -E 's/^.*\:([0-9]+)\/.*$/\1/'` # :<port>/
: "${PORT:?Port not parsed}"
DB=`echo ${DATABASE_URL} | sed -E 's/^.*\/(.+)\?*$/\1/'` # /<db>?
: "${DB:?DB not set}"
export PGHOST=$HOST
export PGPORT=$PORT
export PGDATABASE=$DB
export PGUSER=$POSTGRES_USER
export PGPASSWORD=$POSTGRES_PASSWORD
psql -q -t -c "TRUNCATE referencedata.ideal_stock_amounts RESTART IDENTITY CASCADE;"
psql -q -t -c "DELETE FROM referencedata.processing_periods WHERE startDate = (date_trunc('MONTH', NOW()))::DATE AND endDate = (date_trunc('MONTH', NOW()) + INTERVAL '1 MONTH - 1 day')::DATE;";