-
Notifications
You must be signed in to change notification settings - Fork 10
/
deploy-local.sh
executable file
·58 lines (42 loc) · 1.8 KB
/
deploy-local.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
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
#!/usr/bin/env bash
# This script sets up a PostgreSQL database in a temporary directory, loads the
# schema described in the app.sql.md file and runs a PostgREST instance on top
# of it.
set -e # abort on error
# Create a new environment if the example environment is not yet set.
if [[ -z "$EXAMPLEAPP_BASEDIR" ]]; then
echo "Loading a new environment from 'deploy-local.env'..."
source "$(deploy/mkenv-local.sh)"
else
echo "Using the existing environment in $EXAMPLEAPP_BASEDIR..."
fi
mkdir -p "$EXAMPLEAPP_BASEDIR"
cleanup () {
pg_ctl stop -m i
rm -rf "$EXAMPLEAPP_BASEDIR"
kill 0
}
trap cleanup exit
# Filter the SQL code blocks from the Markdown file
sed -f deploy/md2sql.sed <app.sql.md >"$EXAMPLEAPP_BASEDIR/app.sql"
# Initialize our database cluster in the PGDATA directory. As we'd like our
# environment to be as reproducible as possible, we make it independent from
# the locale, encoding and timezone of the host.
TZ=UTC initdb --no-locale --encoding=UTF8 -U "$PGUSER" > /dev/null
# Create the socket directory.
mkdir -p "$PGHOST"
# Start the database server, listening on a socket instead of a port.
# -F disables file syncing for a bit of extra performance in testing, don't use
# this in production. We use pg_ctl here as it waits for its actions to
# complete by default.
pg_ctl start -o "-F -c listen_addresses=\"\" -k $PGHOST" > /dev/null
# Load the application schema.
psql -P pager=off -f "$EXAMPLEAPP_BASEDIR/app.sql"
# Stop the database server and wait for it to shut down.
pg_ctl stop > /dev/null
echo "Running PostgreSQL and PostgREST on $EXAMPLEAPP_URI"
echo "Press Ctrl-c to exit and clean up the temp directory $EXAMPLEAPP_BASEDIR"
# Start a non-daemonized instance of the database server.
postgres -F -c listen_addresses="" -k "$PGHOST" &
postgrest deploy/postgrest.conf &
wait