Use environment variables for DB connection if possible

This commit is contained in:
D. Berge
2020-08-12 18:18:47 +02:00
parent 3dcb55393a
commit f2373a0454
2 changed files with 14 additions and 8 deletions

View File

@@ -19,7 +19,7 @@
# Needless to say, the schema should be compatible with the rest of the codebase. # 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 # 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. # 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. # There is an SQL script in etc/db/ to create a new database if necessary.
# Defaults to dougal. # Defaults to dougal.
@@ -36,8 +36,10 @@ PROJECT_NAME="$2"
EPSG_CODE="$3" EPSG_CODE="$3"
TEMPLATE_SQL="$PREFIX/etc/db/schema-template.sql" TEMPLATE_SQL="$PREFIX/etc/db/schema-template.sql"
DBNAME=dougal PGUSER=${PGUSER:-postgres}
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) 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)) NEXT_SURVEY_ID=$((LAST_SURVEY_ID+1))
SCHEMA_NAME="survey_$NEXT_SURVEY_ID" 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" 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 "Adding details to projects table"
echo "ID: $PROJECT_ID, Name: $PROJECT_NAME, Schema: $SCHEMA_NAME, EPSG: $EPSG_CODE" 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 pid="$PROJECT_ID" \
--variable name="$PROJECT_NAME" \ --variable name="$PROJECT_NAME" \
--variable schema="$SCHEMA_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 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 ) # 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"]]'); # 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 fi

View File

@@ -4,10 +4,14 @@
# #
# The template schema is used by bin/create_survey.sh to initialise a new set of tables for a prospect. # 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} SCHEMA_NAME=${SCHEMA_NAME:-survey_1}
EPSG_CODE=${EPSG_CODE:-23031} EPSG_CODE=${EPSG_CODE:-23031}
DEST=${DEST:-"$(dirname "$0")/../etc/db/schema-template.sql"} 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" && sed -r "s/$SCHEMA_NAME/_SURVEY__TEMPLATE_/g;s/$EPSG_CODE/_EPSG__CODE_/g" >"$DEST" &&
echo "Schema $SCHEMA_NAME dumped to $DEST" echo "Schema $SCHEMA_NAME dumped to $DEST"