Skip to content
This repository was archived by the owner on Mar 11, 2019. It is now read-only.

Commit

Permalink
Refactor DB scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Laastine committed Jul 29, 2018
1 parent 3b5e246 commit 408641e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 27 deletions.
23 changes: 13 additions & 10 deletions db/database.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,31 @@

set -eou pipefail

function print_status {
if [[ $1 -eq 0 ]]; then
printf "\n[OK]\n"
else
printf "\n[FAIL]\n"
fi
}
DBNAME="postgres"
DATADIR="lukkarimaatti-db"

if [ "$#" -eq 0 ]; then
echo "Usage: $0 [start|stop]"
echo "Usage: $0 [init|start|stop]"
elif [ "$1" = "init" ]; then
echo "Initializing database"
initdb $DATADIR

postgres -D $DATADIR > db.log 2>&1 &
echo $! > postgres.pid
sleep 3
createuser postgres
psql $DBNAME -f init.sql

elif [ "$1" = "start" ]; then
echo "Starting PostgreSQL server"
postgres -D lukkarimaatti-db
EXIT_STATUS=$?

printf 'PostgreSQL server started'
print_status EXIT_STATUS
elif [ "$1" = "stop" ]; then
echo "Stoping PostgreSQL server"
pg_ctl -D lukkarimaatti-db stop -s -m fast
EXIT_STATUS=$?

printf 'PostgreSQL server stopped'
print_status EXIT_STATUS
fi
16 changes: 0 additions & 16 deletions db/init-database.sh → db/init.sql
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
#!/usr/bin/env bash

set -eou pipefail

DBNAME="postgres"
DATADIR="lukkarimaatti-db"

initdb $DATADIR

postgres -D $DATADIR > db.log 2>&1 &
echo $! > postgres.pid
sleep 3
createuser postgres

psql $DBNAME << EOF
GRANT ALL PRIVILEGES ON database postgres to postgres;
CREATE SCHEMA IF NOT EXISTS lukkarimaatti AUTHORIZATION postgres;
GRANT ALL ON SCHEMA lukkarimaatti TO postgres;
GRANT ALL ON ALL TABLES IN SCHEMA lukkarimaatti TO postgres;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
EOF
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"watch-js": "watchify app/client.js --debug -o .generated/bundle.js -v",
"watch": "npm run watch-js & npm run watch-css & supervisor --quiet --ignore node_modules --watch app app/bootstrap.js",
"start": "node app/bootstrap.js",
"init-db": "pushd db && ./init-database.sh && popd",
"init-db": "pushd db && ./database.sh init && popd",
"start-db": "pushd db && ./database.sh start && popd",
"stop-db": "pushd db && ./database.sh stop && popd",
"lint": "eslint --cache --report-unused-disable-directives 'app/**/*.js' 'test/unit/*.js' 'test/util/*.js' 'test/specs/*.js' 'test/page/*.js' --fix",
Expand Down

0 comments on commit 408641e

Please sign in to comment.