Skip to content

Commit

Permalink
Database updates to support deploy to RDS. (#26)
Browse files Browse the repository at this point in the history
* Updated db setup scripts to use env vars for host, user and db name.

* Updated README with new env var names.

* Added env var to specify database port.
  • Loading branch information
nihonjinrxs authored and mrcnc committed Sep 23, 2017
1 parent 774c747 commit f02836b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ If you are loading the data into a remote database, use environment variables
to tell the script where to load:

```
DB_USER=nola311 DB_NAME=nola311 DB_HOST=c2rp0kujqp.us-east-1.rds.amazonaws.com ./setup.sh
NOLA311_DB_USER=nola311 NOLA311_DB_NAME=nola311 NOLA311_DB_HOST=c2rp0kujqp.us-east-1.rds.amazonaws.com NOLA311_DB_PORT=5432 ./setup.sh
```

## some sample queries
Expand All @@ -63,7 +63,7 @@ select council_district, count(*) as num_calls from nola311.calls group by counc
-- how many pothole calls have been opened and closed this year?
select ticket_status, count(*) as total from nola311.calls where issue_type = 'Pothole/Roadway Surface Repair' and ticket_created_date_time >= '2017-01-01'::date group by ticket_status;

--- dont forget to checkout the views
--- dont forget to checkout the views

select * from open_tickets_stats where issue_type = 'Catch Basin Maintenance' and year_created = '2017';
```
23 changes: 17 additions & 6 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@
#!/usr/bin/bash
#!/usr/local/bin/bash

echo ""
echo "Creating nola311 user and database"
echo ""
createuser nola311
createdb nola311 -O nola311
NOLA311_DB_USER=${NOLA311_DB_USER:-postgres}
NOLA311_DB_HOST=${NOLA311_DB_HOST:-localhost}
NOLA311_DB_PORT=${NOLA311_DB_PORT:-5432}
NOLA311_DB_NAME=${NOLA311_DB_NAME:-nola311}

if [ $NOLA311_DB_HOST == "localhost" ]; then
echo ""
echo "Creating nola311 user and database"
echo ""
createuser nola311
createdb nola311 -O nola311
else
echo ""
echo "Skipping creation of user and database: not on localhost"
echo ""
fi

echo ""
echo "Downloading the source data"
echo ""
./setup/download_source_data.sh

echo ""
echo "Setup tables and load data into database"
echo "Setup tables and load data into database"
echo ""
./setup/load_data.sh

Expand Down
19 changes: 10 additions & 9 deletions setup/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
#!/usr/bin/bash
#!/usr/local/bin/bash

DB_USER=${DB_USER:-postgres}
DB_HOST=${DB_HOST:-localhost}
DB_NAME=${DB_NAME:-nola311}
NOLA311_DB_USER=${NOLA311_DB_USER:-postgres}
NOLA311_DB_HOST=${NOLA311_DB_HOST:-localhost}
NOLA311_DB_PORT=${NOLA311_DB_PORT:-5432}
NOLA311_DB_NAME=${NOLA311_DB_NAME:-nola311}

# sanitize the tables
psql -U $DB_USER -d $DB_NAME -h $DB_HOST -f setup/sanitize_call_data.sql
psql -U $DB_USER -d $DB_NAME -h $DB_HOST -f setup/sanitize_neighborhood_data.sql
psql -U $NOLA311_DB_USER -d $NOLA311_DB_NAME -h $NOLA311_DB_HOST -p $NOLA311_DB_PORT -f setup/sanitize_call_data.sql
psql -U $NOLA311_DB_USER -d $NOLA311_DB_NAME -h $NOLA311_DB_HOST -p $NOLA311_DB_PORT -f setup/sanitize_neighborhood_data.sql

# create views
psql -U $DB_USER -d $DB_NAME -h $DB_HOST -f views/open_tickets_stats.sql -q
psql -U $DB_USER -d $DB_NAME -h $DB_HOST -f views/closed_tickets_stats.sql -q
psql -U $DB_USER -d $DB_NAME -h $DB_HOST -f views/call_records_for_review.sql -q
psql -U $DB_USER -d $DB_NAME -h $DB_HOST -f views/call_records_with_call_for_details.sql -q
psql -U $NOLA311_DB_USER -d $NOLA311_DB_NAME -h $NOLA311_DB_HOST -p $NOLA311_DB_PORT -f views/open_tickets_stats.sql -q
psql -U $NOLA311_DB_USER -d $NOLA311_DB_NAME -h $NOLA311_DB_HOST -p $NOLA311_DB_PORT -f views/closed_tickets_stats.sql -q
psql -U $NOLA311_DB_USER -d $NOLA311_DB_NAME -h $NOLA311_DB_HOST -p $NOLA311_DB_PORT -f views/call_records_for_review.sql -q
psql -U $NOLA311_DB_USER -d $NOLA311_DB_NAME -h $NOLA311_DB_HOST -p $NOLA311_DB_PORT -f views/call_records_with_call_for_details.sql -q
15 changes: 8 additions & 7 deletions setup/load_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@
#!/usr/local/bin/bash
set -e

DB_USER=${DB_USER:-postgres}
DB_HOST=${DB_HOST:-localhost}
DB_NAME=${DB_NAME:-nola311}
NOLA311_DB_USER=${NOLA311_DB_USER:-postgres}
NOLA311_DB_HOST=${NOLA311_DB_HOST:-localhost}
NOLA311_DB_PORT=${NOLA311_DB_PORT:-5432}
NOLA311_DB_NAME=${NOLA311_DB_NAME:-nola311}
data_dir=$(pwd)/data
neighborhood_areas_file=$data_dir/neighborhood_areas.geo.json
neighborhood_areas_file=$data_dir/neighborhood_areas.geo.json
call_data_file=$data_dir/nola311_raw.csv

echo ""
echo "Setting up schema to load data into $DB_NAME on $DB_HOST"
echo "Setting up schema to load data into $NOLA311_DB_NAME on $NOLA311_DB_HOST:$NOLA311_DB_PORT"
echo ""
psql -U $DB_USER -d $DB_NAME -h $DB_HOST -f setup/setup_schema.sql
psql -U $NOLA311_DB_USER -d $NOLA311_DB_NAME -h $NOLA311_DB_HOST -p $NOLA311_DB_PORT -f setup/setup_schema.sql

echo ""
echo "Loading data from $call_data_file"
echo "This may take a minute..."
echo ""
psql -U $DB_USER -d $DB_NAME -h $DB_HOST \
psql -U $NOLA311_DB_USER -d $NOLA311_DB_NAME -h $NOLA311_DB_HOST -p $NOLA311_DB_PORT \
-c "\copy nola311.calls_tmp(ticket_id,issue_type,ticket_created_date_time,ticket_closed_date_time,ticket_status,issue_description,street_address,neighborhood_district,council_district,city,state,zip_code,location,geom,latitude,longitude) from '$call_data_file' with csv header NULL as '';"

echo ""
echo "Loading data from $neighborhood_areas_file"
echo ""
psql -U $DB_USER -d $DB_NAME -h $DB_HOST \
psql -U $NOLA311_DB_USER -d $NOLA311_DB_NAME -h $NOLA311_DB_HOST -p $NOLA311_DB_PORT \
-c "\copy nola311.neighborhood_areas_tmp(json_data) from '$neighborhood_areas_file' csv quote e'\x01' delimiter e'\x02';"

0 comments on commit f02836b

Please sign in to comment.