Skip to content

Commit c638947

Browse files
committed
devops: Fix psql \copy paths for Docker and add DOCKER_PSQL flag
- Use relative paths in sample scripts so \copy works both with local psql and Docker psql (which runs with -w /statbus) - Add DOCKER_PSQL=1 flag to force Docker psql for testing - Accept both DEBUG=1 and DEBUG=true for consistency
1 parent 1124b99 commit c638947

File tree

7 files changed

+34
-21
lines changed

7 files changed

+34
-21
lines changed

devops/manage-statbus.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# devops/manage-statbus.sh
33
set -euo pipefail # Exit on error, unbound variable, or any failure in a pipeline
44

5-
# Check for DEBUG environment variable
6-
if [ "${DEBUG:-}" = "true" ]; then
7-
set -x # Print all commands before running them if DEBUG is true
5+
# Check for DEBUG environment variable (accepts "true" or "1")
6+
if [ "${DEBUG:-}" = "true" ] || [ "${DEBUG:-}" = "1" ]; then
7+
set -x # Print all commands before running them if DEBUG is enabled
88
fi
99

1010
# Ensure Homebrew environment is set up for tools like 'shards'
@@ -328,7 +328,7 @@ case "$action" in
328328
done
329329

330330
debug_arg=""
331-
if [ "${DEBUG:-}" = "true" ]; then
331+
if [ "${DEBUG:-}" = "true" ] || [ "${DEBUG:-}" = "1" ]; then
332332
debug_arg="--debug"
333333
fi
334334

@@ -1069,7 +1069,8 @@ EOS
10691069
eval $(./devops/manage-statbus.sh postgres-variables)
10701070
# The local psql is always tried first, as it has access to files
10711071
# used for copying in data.
1072-
if $(which psql > /dev/null); then
1072+
# Set DOCKER_PSQL=1 to force using Docker psql (useful for testing).
1073+
if [ -z "${DOCKER_PSQL:-}" ] && $(which psql > /dev/null); then
10731074
psql "$@"
10741075
else
10751076
if test -t 0 && test -t 1 && test ! -p /dev/stdin && test ! -f /dev/stdin; then

samples/demo/hierarchical/import-hierarchical-demo-data.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ echo "Creating import jobs for hierarchical demo data"
3838
$WORKSPACE/devops/manage-statbus.sh psql -v USER_EMAIL="${USER_EMAIL}" -f samples/demo/hierarchical/import-hierarchical-demo-data.sql
3939

4040
echo "Loading data into import tables"
41+
# Note: Use relative paths for \copy to work both locally and in Docker
42+
# (Docker psql runs with -w /statbus, so relative paths resolve correctly)
4143

4244
# Load legal units with hierarchical identifiers
4345
# - Uganda units: census_ident (4-level)
4446
# - Morocco units: judicial_ident (2-level)
4547
echo "Loading legal units with hierarchical identifiers"
46-
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_hierarchical_demo_lu_current_upload(tax_ident,stat_ident,name,birth_date,physical_region_code,physical_country_iso_2,primary_activity_category_code,legal_form_code,sector_code,employees,turnover,data_source_code,census_ident_census,census_ident_region,census_ident_surveyor,census_ident_unit_no,judicial_ident_court,judicial_ident_unit_no) FROM '$WORKSPACE/samples/demo/hierarchical/legal_units_hierarchical_demo.csv' WITH CSV HEADER;"
48+
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_hierarchical_demo_lu_current_upload(tax_ident,stat_ident,name,birth_date,physical_region_code,physical_country_iso_2,primary_activity_category_code,legal_form_code,sector_code,employees,turnover,data_source_code,census_ident_census,census_ident_region,census_ident_surveyor,census_ident_unit_no,judicial_ident_court,judicial_ident_unit_no) FROM 'samples/demo/hierarchical/legal_units_hierarchical_demo.csv' WITH CSV HEADER;"
4749

4850
# Load formal establishments with hierarchical identifiers
4951
echo "Loading formal establishments with hierarchical identifiers"
50-
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_hierarchical_demo_es_for_lu_current_upload(tax_ident,stat_ident,name,physical_region_code,physical_country_iso_2,primary_activity_category_code,employees,turnover,legal_unit_tax_ident,data_source_code,census_ident_census,census_ident_region,census_ident_surveyor,census_ident_unit_no,judicial_ident_court,judicial_ident_unit_no) FROM '$WORKSPACE/samples/demo/hierarchical/formal_establishments_hierarchical_demo.csv' WITH CSV HEADER;"
52+
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_hierarchical_demo_es_for_lu_current_upload(tax_ident,stat_ident,name,physical_region_code,physical_country_iso_2,primary_activity_category_code,employees,turnover,legal_unit_tax_ident,data_source_code,census_ident_census,census_ident_region,census_ident_surveyor,census_ident_unit_no,judicial_ident_court,judicial_ident_unit_no) FROM 'samples/demo/hierarchical/formal_establishments_hierarchical_demo.csv' WITH CSV HEADER;"
5153

5254
echo "Checking import job states"
5355
$WORKSPACE/devops/manage-statbus.sh psql -c "SELECT slug, state FROM public.import_job WHERE slug LIKE 'import_hierarchical_demo_%' ORDER BY slug;"

samples/demo/import-demo-data-with-source-dates.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,18 @@ $WORKSPACE/devops/manage-statbus.sh psql -v USER_EMAIL="${USER_EMAIL}" -f sample
3434

3535
echo "Loading data into import tables"
3636
# Load legal units data
37+
# Note: Use relative paths for \copy to work both locally and in Docker
38+
# (Docker psql runs with -w /statbus, so relative paths resolve correctly)
3739
echo "Loading legal units with source dates demo data"
38-
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_demo_lu_wsd_upload(tax_ident,stat_ident,name,valid_from,physical_address_part1,valid_to,postal_address_part1,postal_address_part2,physical_address_part2,physical_postcode,postal_postcode,physical_address_part3,physical_postplace,postal_address_part3,postal_postplace,phone_number,landline,mobile_number,fax_number,web_address,email_address,secondary_activity_category_code,physical_latitude,physical_longitude,physical_altitude,birth_date,physical_region_code,postal_country_iso_2,physical_country_iso_2,primary_activity_category_code,legal_form_code,sector_code,employees,turnover,data_source_code,status_code,unit_size_code) FROM '$WORKSPACE/app/public/demo/legal_units_with_source_dates_demo.csv' WITH CSV HEADER;"
40+
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_demo_lu_wsd_upload(tax_ident,stat_ident,name,valid_from,physical_address_part1,valid_to,postal_address_part1,postal_address_part2,physical_address_part2,physical_postcode,postal_postcode,physical_address_part3,physical_postplace,postal_address_part3,postal_postplace,phone_number,landline,mobile_number,fax_number,web_address,email_address,secondary_activity_category_code,physical_latitude,physical_longitude,physical_altitude,birth_date,physical_region_code,postal_country_iso_2,physical_country_iso_2,primary_activity_category_code,legal_form_code,sector_code,employees,turnover,data_source_code,status_code,unit_size_code) FROM 'app/public/demo/legal_units_with_source_dates_demo.csv' WITH CSV HEADER;"
3941

4042
# Load formal establishments data
4143
echo "Loading formal establishments with source dates demo data"
42-
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_demo_es_for_lu_wsd_upload(tax_ident,stat_ident,name,physical_region_code,valid_from,valid_to,postal_country_iso_2,physical_country_iso_2,primary_activity_category_code,secondary_activity_category_code,employees,turnover,legal_unit_tax_ident,data_source_code,physical_address_part1,physical_address_part2,physical_address_part3,postal_address_part1,postal_address_part2,postal_address_part3,phone_number,mobile_number,landline,fax_number,web_address,email_address,physical_latitude,physical_longitude,physical_altitude,birth_date,unit_size_code,status_code) FROM '$WORKSPACE/app/public/demo/formal_establishments_units_with_source_dates_demo.csv' WITH CSV HEADER;"
44+
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_demo_es_for_lu_wsd_upload(tax_ident,stat_ident,name,physical_region_code,valid_from,valid_to,postal_country_iso_2,physical_country_iso_2,primary_activity_category_code,secondary_activity_category_code,employees,turnover,legal_unit_tax_ident,data_source_code,physical_address_part1,physical_address_part2,physical_address_part3,postal_address_part1,postal_address_part2,postal_address_part3,phone_number,mobile_number,landline,fax_number,web_address,email_address,physical_latitude,physical_longitude,physical_altitude,birth_date,unit_size_code,status_code) FROM 'app/public/demo/formal_establishments_units_with_source_dates_demo.csv' WITH CSV HEADER;"
4345

4446
# Load informal establishments data
4547
echo "Loading informal establishments with source dates demo data"
46-
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_demo_es_without_lu_wsd_upload(tax_ident,stat_ident,name,physical_region_code,valid_from,valid_to,physical_country_iso_2,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,employees,turnover,data_source_code,physical_address_part1,physical_address_part2,physical_address_part3,postal_address_part1,postal_address_part2,postal_address_part3,phone_number,mobile_number,landline,fax_number,web_address,email_address,unit_size_code,status_code,physical_latitude,physical_longitude,physical_altitude) FROM '$WORKSPACE/app/public/demo/informal_establishments_units_with_source_dates_demo.csv' WITH CSV HEADER;"
48+
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_demo_es_without_lu_wsd_upload(tax_ident,stat_ident,name,physical_region_code,valid_from,valid_to,physical_country_iso_2,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,employees,turnover,data_source_code,physical_address_part1,physical_address_part2,physical_address_part3,postal_address_part1,postal_address_part2,postal_address_part3,phone_number,mobile_number,landline,fax_number,web_address,email_address,unit_size_code,status_code,physical_latitude,physical_longitude,physical_altitude) FROM 'app/public/demo/informal_establishments_units_with_source_dates_demo.csv' WITH CSV HEADER;"
4749

4850
echo "Checking import job states"
4951
$WORKSPACE/devops/manage-statbus.sh psql -c "SELECT slug, state FROM public.import_job WHERE slug LIKE 'import_demo_%_wsd' ORDER BY slug;"

samples/demo/import-demo-data.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,19 @@ echo "Creating import jobs for demo data"
3333
$WORKSPACE/devops/manage-statbus.sh psql -v USER_EMAIL="${USER_EMAIL}" -f samples/demo/import-demo-data.sql
3434

3535
echo "Loading data into import tables"
36+
# Note: Use relative paths for \copy to work both locally and in Docker
37+
# (Docker psql runs with -w /statbus, so relative paths resolve correctly)
3638
# Load legal units data
3739
echo "Loading legal units demo data"
38-
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_demo_lu_current_upload(tax_ident,stat_ident,name,birth_date,physical_region_code,physical_country_iso_2,primary_activity_category_code,legal_form_code,sector_code,employees,turnover,data_source_code) FROM '$WORKSPACE/app/public/demo/legal_units_demo.csv' WITH CSV HEADER;"
40+
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_demo_lu_current_upload(tax_ident,stat_ident,name,birth_date,physical_region_code,physical_country_iso_2,primary_activity_category_code,legal_form_code,sector_code,employees,turnover,data_source_code) FROM 'app/public/demo/legal_units_demo.csv' WITH CSV HEADER;"
3941

4042
# Load formal establishments data
4143
echo "Loading formal establishments demo data"
42-
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_demo_es_for_lu_current_upload(tax_ident,stat_ident,name,physical_region_code,physical_country_iso_2,primary_activity_category_code,employees,turnover,legal_unit_tax_ident,data_source_code) FROM '$WORKSPACE/app/public/demo/formal_establishments_units_demo.csv' WITH CSV HEADER;"
44+
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_demo_es_for_lu_current_upload(tax_ident,stat_ident,name,physical_region_code,physical_country_iso_2,primary_activity_category_code,employees,turnover,legal_unit_tax_ident,data_source_code) FROM 'app/public/demo/formal_establishments_units_demo.csv' WITH CSV HEADER;"
4345

4446
# Load informal establishments data
4547
echo "Loading informal establishments demo data"
46-
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_demo_es_without_lu_current_upload(tax_ident,stat_ident,name,physical_region_code,physical_country_iso_2,primary_activity_category_code,employees,turnover,data_source_code) FROM '$WORKSPACE/app/public/demo/informal_establishments_units_demo.csv' WITH CSV HEADER;"
48+
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_demo_es_without_lu_current_upload(tax_ident,stat_ident,name,physical_region_code,physical_country_iso_2,primary_activity_category_code,employees,turnover,data_source_code) FROM 'app/public/demo/informal_establishments_units_demo.csv' WITH CSV HEADER;"
4749

4850
echo "Checking import job states"
4951
$WORKSPACE/devops/manage-statbus.sh psql -c "SELECT slug, state FROM public.import_job WHERE slug LIKE 'import_demo_%_current' ORDER BY slug;"

samples/norway/brreg/brreg-import-selection.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ FROM def
7171
ON CONFLICT (slug) DO NOTHING;"
7272

7373
echo "Loading data into import tables"
74+
# Note: Use relative paths for \copy to work both locally and in Docker
75+
# (Docker psql runs with -w /statbus, so relative paths resolve correctly)
7476
# Load hovedenhet (legal units) data
7577
echo "Loading hovedenhet selection data"
76-
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_hovedenhet_${YEAR}_selection_upload FROM '$WORKSPACE/samples/norway/legal_unit/enheter-selection.csv' WITH CSV HEADER;"
78+
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_hovedenhet_${YEAR}_selection_upload FROM 'samples/norway/legal_unit/enheter-selection.csv' WITH CSV HEADER;"
7779

7880
# Load underenhet (establishments) data
7981
echo "Loading underenhet selection data"
80-
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_underenhet_${YEAR}_selection_upload FROM '$WORKSPACE/samples/norway/establishment/underenheter-selection.csv' WITH CSV HEADER;"
82+
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_underenhet_${YEAR}_selection_upload FROM 'samples/norway/establishment/underenheter-selection.csv' WITH CSV HEADER;"
8183

8284
echo "Checking import job states"
8385
$WORKSPACE/devops/manage-statbus.sh psql -c "SELECT slug, state FROM public.import_job WHERE slug LIKE '%selection' ORDER BY slug;"

samples/norway/history/load.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ echo "Adding import definitions for BRREG units"
3535
$WORKSPACE/devops/manage-statbus.sh psql < samples/norway/brreg/create-import-definition-hovedenhet-2024.sql
3636
$WORKSPACE/devops/manage-statbus.sh psql < samples/norway/brreg/create-import-definition-underenhet-2024.sql
3737

38-
YEARS=$(ls $WORKSPACE/samples/norway/history/*-enheter.csv | sed -E 's/.*\/([0-9]{4})-enheter\.csv/\1/' | sort -u)
38+
# Note: Use relative paths for \copy to work both locally and in Docker
39+
# (Docker psql runs with -w /statbus, so relative paths resolve correctly)
40+
YEARS=$(ls samples/norway/history/*-enheter.csv | sed -E 's/.*\/([0-9]{4})-enheter\.csv/\1/' | sort -u)
3941

4042
echo "Creating import jobs for each year"
4143
for YEAR in $YEARS; do
@@ -76,11 +78,11 @@ for YEAR in $YEARS; do
7678

7779
# Load hovedenhet (legal units) data
7880
echo "Loading hovedenhet data for $YEAR"
79-
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_hovedenhet_${YEAR}_history_upload FROM '$WORKSPACE/samples/norway/history/${YEAR}-enheter.csv' WITH CSV HEADER;"
81+
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_hovedenhet_${YEAR}_history_upload FROM 'samples/norway/history/${YEAR}-enheter.csv' WITH CSV HEADER;"
8082

8183
# Load underenhet (establishments) data
8284
echo "Loading underenhet data for $YEAR"
83-
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_underenhet_${YEAR}_history_upload FROM '$WORKSPACE/samples/norway/history/${YEAR}-underenheter.csv' WITH CSV HEADER;"
85+
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_underenhet_${YEAR}_history_upload FROM 'samples/norway/history/${YEAR}-underenheter.csv' WITH CSV HEADER;"
8486
done
8587

8688
echo "Checking import job states"

samples/norway/small-history/load.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ echo "Adding import definitions for BRREG units"
3535
$WORKSPACE/devops/manage-statbus.sh psql < samples/norway/brreg/create-import-definition-hovedenhet-2024.sql
3636
$WORKSPACE/devops/manage-statbus.sh psql < samples/norway/brreg/create-import-definition-underenhet-2024.sql
3737

38-
YEARS=$(ls $WORKSPACE/samples/norway/small-history/*-enheter.csv | sed -E 's/.*\/([0-9]{4})-enheter\.csv/\1/' | sort -u)
38+
# Note: Use relative paths for \copy to work both locally and in Docker
39+
# (Docker psql runs with -w /statbus, so relative paths resolve correctly)
40+
YEARS=$(ls samples/norway/small-history/*-enheter.csv | sed -E 's/.*\/([0-9]{4})-enheter\.csv/\1/' | sort -u)
3941

4042
echo "Creating import jobs for each year"
4143
for YEAR in $YEARS; do
@@ -76,11 +78,11 @@ for YEAR in $YEARS; do
7678

7779
# Load hovedenhet (legal units) data
7880
echo "Loading hovedenhet data for $YEAR"
79-
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_hovedenhet_${YEAR}_small_history_upload FROM '$WORKSPACE/samples/norway/small-history/${YEAR}-enheter.csv' WITH CSV HEADER;"
81+
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_hovedenhet_${YEAR}_small_history_upload FROM 'samples/norway/small-history/${YEAR}-enheter.csv' WITH CSV HEADER;"
8082

8183
# Load underenhet (establishments) data
8284
echo "Loading underenhet data for $YEAR"
83-
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_underenhet_${YEAR}_small_history_upload FROM '$WORKSPACE/samples/norway/small-history/${YEAR}-underenheter.csv' WITH CSV HEADER;"
85+
$WORKSPACE/devops/manage-statbus.sh psql -c "\copy public.import_underenhet_${YEAR}_small_history_upload FROM 'samples/norway/small-history/${YEAR}-underenheter.csv' WITH CSV HEADER;"
8486
done
8587

8688
#echo "Running worker processing to process import jobs"

0 commit comments

Comments
 (0)