Skip to content

Commit

Permalink
Database script updates to support containerization
Browse files Browse the repository at this point in the history
This commit modifies several scripts to make it easier to setup the database as part of a containerized cf.gov deploy.

It removes a portion of the refresh-data.sh script that attempts to manually rename the existing 'cfpb' schema in our database dumps, to a schema named for a different Postgres user. We no longer use the PGUSER environment variable as of 59b109b, so this code doesn't work right now.

In the future, if we need to import a 'cfpb' schema into a database where the username is something different (for example in an RDS instance where we might have less control over the username) we might have to revisit this code.

This change also consolidates the various 'DB setup' steps we do after loading a dump -- running migrations, creating the cache table, and running our 'initial' data script, reducing some duplication.
  • Loading branch information
chosak committed Dec 6, 2024
1 parent 5b2f3d7 commit 45a39ef
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 15 deletions.
2 changes: 0 additions & 2 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ if [ ! -z "$RUN_MIGRATIONS" ]; then
if [ -n "$CFGOV_PROD_DB_LOCATION" ] || [ -n "$DB_DUMP_FILE" ] || [ -n "$DB_DUMP_URL" ]; then
echo "Running refresh-data.sh... $DB_DUMP_FILE"
./refresh-data.sh "$DB_DUMP_FILE"
echo "Create the cache table..."
./cfgov/manage.py createcachetable

# refresh-data.sh runs migrations and rebuilds index,
# unset vars to prevent further action
Expand Down
2 changes: 1 addition & 1 deletion initial-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ set -e
# Import Data
import_data(){
echo 'Running Django migrations...'
./cfgov/manage.py migrate
./cfgov/manage.py migrate --noinput
echo 'Creating any necessary Django database cache tables...'
./cfgov/manage.py createcachetable
echo 'Running initial_data script to configure Wagtail admin...'
Expand Down
18 changes: 6 additions & 12 deletions refresh-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,14 @@ check_data() {
refresh_data() {
echo 'Importing refresh db'
gunzip < "$refresh_dump_name" | cfgov/manage.py dbshell > /dev/null
SCHEMA="$(gunzip -c $refresh_dump_name | grep -m 1 'CREATE SCHEMA' | sed 's/CREATE SCHEMA \(.*\);$/\1/')"
PGUSER="${PGUSER:-cfpb}"
if [ "${PGUSER}" != "${SCHEMA}" ]; then
echo "Adjusting schema name to match username..."
echo "DROP SCHEMA IF EXISTS \"${PGUSER}\" CASCADE; \
ALTER SCHEMA \"${SCHEMA}\" RENAME TO \"${PGUSER}\"" | psql > /dev/null 2>&1
fi
echo 'Running any necessary migrations'
./cfgov/manage.py migrate --noinput --fake-initial
echo 'Setting up initial data'
./cfgov/manage.py runscript initial_data
}

initial_data() {
./initial-data.sh
}

update_index() {
source ./index.sh
source ./index.sh
}

get_data() {
Expand Down Expand Up @@ -108,6 +101,7 @@ done
get_data
check_data
refresh_data
initial_data

if [[ $noindex -ne 1 ]]; then
update_index
Expand Down

0 comments on commit 45a39ef

Please sign in to comment.