From a0a322f766a13dba5e475804b365a34476405b5b Mon Sep 17 00:00:00 2001 From: Chenglim Ear Date: Mon, 15 Jul 2024 20:49:52 -0700 Subject: [PATCH] make data_warning empty instead of null --- Makefile | 2 +- bin/make-null-empty | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 bin/make-null-empty diff --git a/Makefile b/Makefile index a9de936b6..c788916be 100644 --- a/Makefile +++ b/Makefile @@ -96,7 +96,7 @@ do-import-spreadsheets: echo 'ALTER TABLE "referendums" ADD COLUMN id SERIAL PRIMARY KEY;' | psql $(DATABASE_NAME) ./bin/remove-whitespace $(DATABASE_NAME) referendums Short_Title ./bin/remove-whitespace $(DATABASE_NAME) referendums Summary - ./bin/remove-whitespace $(DATABASE_NAME) referendums data_warning + ./bin/make-null-empty $(DATABASE_NAME) referendums data_warning echo 'DROP TABLE IF EXISTS name_to_number CASCADE;' | psql $(DATABASE_NAME) ./bin/create-table $(DATABASE_NAME) $(CSV_PATH) name_to_number diff --git a/bin/make-null-empty b/bin/make-null-empty new file mode 100644 index 000000000..11c4b0b4b --- /dev/null +++ b/bin/make-null-empty @@ -0,0 +1,20 @@ +#!/bin/bash +# Usage: ./bin/make-null-empty [database name] [table name] [column name] +# ./bin/make-null-empty disclosure-backend candidates Candidate +set -euo pipefail + +if [ $# -ne 3 ]; then + echo 'Usage: ./bin/make-null-empty [database name] [table name] [column name]' + exit 1 +fi + +database_name=$1 +table_name=$2 +column_name=$3 + +# trim leading and trailing white spaces +cat <<-QUERY | psql ${database_name} + \\set ON_ERROR_STOP on + UPDATE "$table_name" t SET "$column_name" = '' WHERE "$column_name" IS NULL; + +QUERY