From f2373a0454de712746afc5340a6690593eec5e8e Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Wed, 12 Aug 2020 18:18:47 +0200 Subject: [PATCH] Use environment variables for DB connection if possible --- bin/create_survey.sh | 16 +++++++++------- sbin/dump_schema.sh | 6 +++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/bin/create_survey.sh b/bin/create_survey.sh index c2264a4..98c4d7a 100755 --- a/bin/create_survey.sh +++ b/bin/create_survey.sh @@ -19,7 +19,7 @@ # Needless to say, the schema should be compatible with the rest of the codebase. # Defaults to the built-in schema found under etc/db/schema-template.sql # -# * DBNAME is the name of the database in which to create the new survey. The +# * PGDATABASE is the name of the database in which to create the new survey. The # database itself is expected to have PostGIS version 3 or greater installed. # There is an SQL script in etc/db/ to create a new database if necessary. # Defaults to ‘dougal’. @@ -36,8 +36,10 @@ PROJECT_NAME="$2" EPSG_CODE="$3" TEMPLATE_SQL="$PREFIX/etc/db/schema-template.sql" -DBNAME=dougal -LAST_SURVEY_ID=$(psql -U postgres -h localhost -Atc '\dn survey_[0-9]+' "$DBNAME" |sed -rn 's/^survey_([0-9]+).*$/\1/p' |sort -n |tail -n 1) +PGUSER=${PGUSER:-postgres} +PGHOST=${PGHOST:-localhost} +PGDATABASE=${PGDATABASE:-dougal} +LAST_SURVEY_ID=$(psql -U $PGUSER -h $PGHOST -Atc '\dn survey_[0-9]+' "$PGDATABASE" |sed -rn 's/^survey_([0-9]+).*$/\1/p' |sort -n |tail -n 1) NEXT_SURVEY_ID=$((LAST_SURVEY_ID+1)) SCHEMA_NAME="survey_$NEXT_SURVEY_ID" @@ -49,16 +51,16 @@ SCHEMA_NAME="survey_$NEXT_SURVEY_ID" } echo "Creating new schema $SCHEMA_NAME with template $TEMPLATE_SQL" -sed -r "s/_SURVEY__TEMPLATE_/$SCHEMA_NAME/g;s/_EPSG__CODE_/$EPSG_CODE/g" <"$TEMPLATE_SQL" | psql -U postgres -h localhost dougal +sed -r "s/_SURVEY__TEMPLATE_/$SCHEMA_NAME/g;s/_EPSG__CODE_/$EPSG_CODE/g" <"$TEMPLATE_SQL" | psql -U $PGUSER -h $PGHOST $PGDATABASE echo "Adding details to projects table" echo "ID: $PROJECT_ID, Name: $PROJECT_NAME, Schema: $SCHEMA_NAME, EPSG: $EPSG_CODE" -psql -U postgres -h localhost \ +psql -U $PGUSER -h $PGHOST \ --variable pid="$PROJECT_ID" \ --variable name="$PROJECT_NAME" \ --variable schema="$SCHEMA_NAME" \ - dougal <<< "INSERT INTO public.projects (pid, name, schema) VALUES (LOWER(:'pid'), :'name', :'schema');" + $PGDATABASE <<< "INSERT INTO public.projects (pid, name, schema) VALUES (LOWER(:'pid'), :'name', :'schema');" if [[ -n "$CREATE_SURVEY_SQL" ]]; then @@ -66,7 +68,7 @@ if [[ -n "$CREATE_SURVEY_SQL" ]]; then # Example ( retrieved from https://epsg.io/23031-1613.sql ) # INSERT into spatial_ref_sys (srid, auth_name, auth_srid, proj4text, srtext) values ( 23031, 'EPSG', 23031, '+proj=utm +zone=31 +ellps=intl +towgs84=-90.365,-101.13,-123.384,0.333,0.077,0.894,1.994 +units=m +no_defs ', 'PROJCS["ED50 / UTM zone 31N",GEOGCS["ED50",DATUM["European_Datum_1950",SPHEROID["International 1924",6378388,297,AUTHORITY["EPSG","7022"]],TOWGS84[-90.365,-101.13,-123.384,0.333,0.077,0.894,1.994],AUTHORITY["EPSG","6230"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4230"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",3],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","23031"]]'); - psql -U postgres -h localhost dougal <<< "$CREATE_SURVEY_SQL" + psql -U $PGUSER -h $PGHOST $PGDATABASE <<< "$CREATE_SURVEY_SQL" fi diff --git a/sbin/dump_schema.sh b/sbin/dump_schema.sh index ab11690..22b13ef 100755 --- a/sbin/dump_schema.sh +++ b/sbin/dump_schema.sh @@ -4,10 +4,14 @@ # # The template schema is used by bin/create_survey.sh to initialise a new set of tables for a prospect. +PGUSER=${PGUSER:-postgres} +PGHOST=${PGHOST:-localhost} +PGDATABASE=${PGDATABASE:-dougal} + SCHEMA_NAME=${SCHEMA_NAME:-survey_1} EPSG_CODE=${EPSG_CODE:-23031} DEST=${DEST:-"$(dirname "$0")/../etc/db/schema-template.sql"} -pg_dump -U postgres --schema="$SCHEMA_NAME" --schema-only --host=localhost dougal | +pg_dump -U $PGUSER --schema="$SCHEMA_NAME" --schema-only --host=$PGHOST $PGDATABASE | sed -r "s/$SCHEMA_NAME/_SURVEY__TEMPLATE_/g;s/$EPSG_CODE/_EPSG__CODE_/g" >"$DEST" && echo "Schema $SCHEMA_NAME dumped to $DEST"