Skip to content

Commit

Permalink
[devops] Make DB_PUBLIC_LOCALHOST_PORT consistent
Browse files Browse the repository at this point in the history
It should only be the port.
In passing also
* Cleanup getting DB_PUBLIC_LOCALHOST_PORT in cli for import.
* Bugfix running psql from manage-statbus.sh when a local psql
  is not available.
  • Loading branch information
jhf committed May 5, 2024
1 parent 4e9c937 commit 191acef
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cli/src/statbus.cr
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class StatBus

private def import_common(import_file_name, sql_field_required_list, sql_field_optional_list, upload_view_name)
# Find .env and load required secrets
Dir.cd("../supabase_docker") do
Dir.cd("..") do
ini_data = File.read(".env")
vars = INI.parse ini_data
# The variables are all in the global scope, as an ".env" file is not really an ini file,
Expand Down
23 changes: 7 additions & 16 deletions devops/manage-statbus.sh
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ EOS
SUPABASE_BIND_ADDRESS="127.0.0.1:$(( 3000+$DEPLOYMENT_SLOT_PORT_OFFSET*10+1 ))"
./devops/dotenv --file .env set SUPABASE_BIND_ADDRESS=$SUPABASE_BIND_ADDRESS

DB_PUBLIC_LOCALHOST_PORT="127.0.0.1:$(( 3000+$DEPLOYMENT_SLOT_PORT_OFFSET*10+2 ))"
DB_PUBLIC_LOCALHOST_PORT="$(( 3000+$DEPLOYMENT_SLOT_PORT_OFFSET*10+2 ))"
./devops/dotenv --file .env set DB_PUBLIC_LOCALHOST_PORT=$DB_PUBLIC_LOCALHOST_PORT

echo "Setting Supabase Container Configuration"
Expand Down Expand Up @@ -277,11 +277,8 @@ EOF

;;
'postgres-variables' )
DB_PUBLIC_LOCALHOST_PORT=$(./devops/dotenv --file .env get DB_PUBLIC_LOCALHOST_PORT)
# Extract the host part (before the colon) and set it to PGHOST
PGHOST=$(echo $DB_PUBLIC_LOCALHOST_PORT | cut -d':' -f1)
# Extract the port part (after the colon) and set it to PGPORT
PGPORT=$(echo $DB_PUBLIC_LOCALHOST_PORT | cut -d':' -f2)
PGHOST=127.0.0.1
PGPORT=$(./devops/dotenv --file .env get DB_PUBLIC_LOCALHOST_PORT)
PGDATABASE=$(./devops/dotenv --file .env get POSTGRES_DB)
PGUSER=postgres
PGPASSWORD=$(./devops/dotenv --file .env get POSTGRES_PASSWORD)
Expand All @@ -290,15 +287,8 @@ export PGHOST=$PGHOST PGPORT=$PGPORT PGDATABASE=$PGDATABASE PGUSER=$PGUSER PGPAS
EOS
;;
'psql' )
DB_PUBLIC_LOCALHOST_PORT=$(./devops/dotenv --file .env get DB_PUBLIC_LOCALHOST_PORT)
# Extract the host part (before the colon) and set it to PGHOST
export PGHOST=$(echo $DB_PUBLIC_LOCALHOST_PORT | cut -d':' -f1)
# Extract the port part (after the colon) and set it to PGPORT
export PGPORT=$(echo $DB_PUBLIC_LOCALHOST_PORT | cut -d':' -f2)
export PGDATABASE=$(./devops/dotenv --file .env get POSTGRES_DB)
export PGUSER=postgres
export PGPASSWORD=$(./devops/dotenv --file .env get POSTGRES_PASSWORD)
if $(which psql > /dev/null); then
eval $(./devops/manage-statbus.sh postgres-variables)
if $(which psql > /dev/null && false); then
psql "$@"
else
# When using scripted input, such as "< some.sql" then interactive TTY is required.
Expand All @@ -308,7 +298,8 @@ EOS
# as required for an interactive psql promp
args="-ti"
fi
docker exec $args $(docker ps | awk '/statbus-db/{print $1}') psql -U $PGUSER $PGDATABASE "$@"
COMPOSE_INSTANCE_NAME=$(./devops/dotenv --file .env get COMPOSE_INSTANCE_NAME)
docker exec $args -e PGPASSWORD=$PGPASSWORD $(docker ps | awk "/$COMPOSE_INSTANCE_NAME-db/{print \$1}") psql -U $PGUSER $PGDATABASE "$@"
fi
;;
'generate-types' )
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.supabase_docker.customise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ services:
# Prevent the database from using too much memory.
shm_size: 1g
ports:
- ${DB_PUBLIC_LOCALHOST_PORT}:5432
- 127.0.0.1:${DB_PUBLIC_LOCALHOST_PORT}:5432
8 changes: 1 addition & 7 deletions samples/norway/brreg-samples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ WORKSPACE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd ../.. && pwd )"

pushd $WORKSPACE

source supabase_docker/.env
export PGHOST=localhost
export PGPORT=$DB_PUBLIC_LOCALHOST_PORT
export PGDATABASE=$POSTGRES_DB
export PGUSER=postgres
export PGPASSWORD="$POSTGRES_PASSWORD"
echo "Setting up Statbus for Norway"
psql < samples/norway/setup.sql
./devops/manage-statbus.sh psql < samples/norway/setup.sql

pushd cli
echo "Loading legal_units"
Expand Down
8 changes: 1 addition & 7 deletions samples/norway/history/load.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@ WORKSPACE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd ../../.. && pwd )"

pushd $WORKSPACE

source supabase_docker/.env
export PGHOST=localhost
export PGPORT=$DB_PUBLIC_LOCALHOST_PORT
export PGDATABASE=$POSTGRES_DB
export PGUSER=postgres
export PGPASSWORD="$POSTGRES_PASSWORD"
echo "Setting up Statbus for Norway"
psql < samples/norway/setup.sql
./devops/manage-statbus.sh psql < samples/norway/setup.sql


echo "Adding tags for insert into right part of history"
Expand Down

0 comments on commit 191acef

Please sign in to comment.