forked from keystonejs/keystone
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request keystonejs#1404 from keystonejs/molomby/knex-reset…
…-db-script Adding a helper script to build/rebuild the KS test Postgres DB
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Create or recreate and setup the Keystone test Postgres DB from scratch | ||
# Will distroy any existing data, etc. | ||
# When Keystone starts the knex adapter will recreate the structure | ||
|
||
KS_DB_NAME="ks5_dev" | ||
KS_SCHEMA_NAME="keystone" | ||
KS_USER_NAME="keystone5" | ||
KS_USER_PASS="k3yst0n3" | ||
|
||
# On MacOS the super use is the current username, no password | ||
SUPER_USER="${USER}" | ||
|
||
# Our queries | ||
DROP_CONNECTIONS_SQL=" | ||
SELECT pg_terminate_backend(pg_stat_activity.pid) | ||
FROM pg_stat_activity | ||
WHERE pg_stat_activity.datname = '${KS_DB_NAME}' | ||
AND pid <> pg_backend_pid(); | ||
" | ||
DROP_DB_SQL=" | ||
DROP DATABASE IF EXISTS \"${KS_DB_NAME}\"; | ||
" | ||
CREATE_DB_SQL=" | ||
CREATE DATABASE \"${KS_DB_NAME}\"; | ||
" | ||
RECREATE_ROLE_SQL=" | ||
DROP USER IF EXISTS \"${KS_USER_NAME}\"; | ||
CREATE USER \"${KS_USER_NAME}\" PASSWORD '${KS_USER_PASS}'; | ||
" | ||
SETUP_SCHEMA=" | ||
CREATE SCHEMA \"${KS_SCHEMA_NAME}\"; | ||
GRANT ALL ON SCHEMA \"${KS_SCHEMA_NAME}\" TO \"${KS_USER_NAME}\"; | ||
" | ||
# Needed for `gen_random_uuid()` function, used by UUIDs | ||
SETUP_UUIDS=" | ||
CREATE EXTENSION IF NOT EXISTS \"pgcrypto\"; | ||
" | ||
|
||
# Run outside the KS DB (in template1) | ||
psql template1 -U "${SUPER_USER}" -c "${DROP_CONNECTIONS_SQL}" | ||
psql template1 -U "${SUPER_USER}" -c "${DROP_DB_SQL}" | ||
psql template1 -U "${SUPER_USER}" -c "${CREATE_DB_SQL}" | ||
psql template1 -U "${SUPER_USER}" -c "${RECREATE_ROLE_SQL}" | ||
|
||
# Run in the new DB | ||
psql "${KS_DB_NAME}" -U "${SUPER_USER}" -c "${SETUP_SCHEMA}" | ||
psql "${KS_DB_NAME}" -U "${SUPER_USER}" -c "${SETUP_UUIDS}" |