From 005ff9dac751719a3469a2ae626d28f3f1e36cd2 Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Fri, 18 Oct 2024 10:14:12 +1100 Subject: [PATCH] buscribe: Do database schema and user setup --- postgres/Dockerfile | 1 + .../buscribe.sql | 33 ------------------- postgres/setup.sh | 32 ++++++++++++++---- 3 files changed, 26 insertions(+), 40 deletions(-) rename buscribe-remainder/buscribe_data.sql => postgres/buscribe.sql (87%) diff --git a/postgres/Dockerfile b/postgres/Dockerfile index af539de..638b0db 100644 --- a/postgres/Dockerfile +++ b/postgres/Dockerfile @@ -1,6 +1,7 @@ FROM postgres:12 COPY postgres/setup.sh /docker-entrypoint-initdb.d/setup.sh COPY postgres/schema.sql / +COPY postgres/buscribe.sql / RUN chmod 0666 /docker-entrypoint-initdb.d/setup.sh COPY postgres/standby_setup.sh /standby_setup.sh LABEL org.opencontainers.image.source https://github.com/dbvideostriketeam/wubloader diff --git a/buscribe-remainder/buscribe_data.sql b/postgres/buscribe.sql similarity index 87% rename from buscribe-remainder/buscribe_data.sql rename to postgres/buscribe.sql index b69c379..a2b1778 100644 --- a/buscribe-remainder/buscribe_data.sql +++ b/postgres/buscribe.sql @@ -1,22 +1,3 @@ -BEGIN TRANSACTION; - -DROP TABLE IF EXISTS buscribe_verified_lines; -DROP TABLE IF EXISTS buscribe_line_speakers; -DROP TABLE IF EXISTS buscribe_speakers; -DROP TABLE IF EXISTS buscribe_verifiers; -DROP TABLE IF EXISTS buscribe_transcriptions; - -ROLLBACK; - -BEGIN TRANSACTION; - -TRUNCATE buscribe_verified_lines RESTART IDENTITY CASCADE; -TRUNCATE buscribe_line_speakers RESTART IDENTITY CASCADE; -TRUNCATE buscribe_speakers RESTART IDENTITY CASCADE; -TRUNCATE buscribe_verifiers RESTART IDENTITY CASCADE; -TRUNCATE buscribe_transcriptions RESTART IDENTITY CASCADE; - -ROLLBACK; CREATE TABLE buscribe_transcriptions ( @@ -43,19 +24,12 @@ CREATE TABLE buscribe_speakers CREATE TABLE buscribe_verifiers ( --- id SERIAL PRIMARY KEY, email TEXT PRIMARY KEY, name TEXT NOT NULL ); --- For testing --- INSERT INTO buscribe_verifiers(email, name) --- VALUES ('placeholder@example.com', 'Place Holder'), --- ('aguy@example.com', 'Arnold Guyana'); - CREATE TABLE buscribe_line_speakers ( --- id BIGSERIAL PRIMARY KEY, line BIGINT NOT NULL REFERENCES buscribe_transcriptions, speaker BIGINT NOT NULL REFERENCES buscribe_speakers, verifier text NOT NULL REFERENCES buscribe_verifiers, @@ -71,7 +45,6 @@ CREATE TABLE buscribe_line_inferred_speakers CREATE TABLE buscribe_verified_lines ( --- id BIGSERIAL PRIMARY KEY, line BIGINT NOT NULL REFERENCES buscribe_transcriptions, verified_line TEXT NOT NULL, verifier text REFERENCES buscribe_verifiers, @@ -82,10 +55,6 @@ CREATE TABLE buscribe_verified_lines CREATE INDEX buscribe_verified_lines_idx ON buscribe_verified_lines USING GIN (setweight(to_tsvector('english', verified_line), 'C')); -BEGIN; - -DROP VIEW buscribe_all_transcriptions; - CREATE VIEW buscribe_all_transcriptions AS SELECT buscribe_transcriptions.id, start_time, @@ -127,8 +96,6 @@ FROM buscribe_transcriptions GROUP BY line ) AS speakers ON id = speakers.line; -ROLLBACK; - CREATE VIEW buscribe_all_transcriptions2 AS SELECT buscribe_transcriptions.id, start_time, diff --git a/postgres/setup.sh b/postgres/setup.sh index abc34cf..ad45cc7 100644 --- a/postgres/setup.sh +++ b/postgres/setup.sh @@ -2,12 +2,19 @@ set -e +sql() { + local user + user="$1" + shift + psql -v ON_ERROR_STOP=1 -U "$user" "$@" +} + # only allow the $WUBLOADER_USER to connect remotely rather than all users sed -i "/host all all all/d" "$PGDATA/pg_hba.conf" echo "host all $WUBLOADER_USER all md5" >> "$PGDATA/pg_hba.conf" echo "Creating $WUBLOADER_USER" -psql -v ON_ERROR_STOP=1 -U $POSTGRES_USER <<-EOSQL +sql "$POSTGRES_USER" <<-EOSQL CREATE USER $WUBLOADER_USER LOGIN PASSWORD '$WUBLOADER_PASSWORD'; @@ -18,7 +25,7 @@ if [ -n "$REPLICATION_USER" ]; then echo "Creating $REPLICATION_USER" # allow the $REPLICATION user to replicate remotely echo "host replication $REPLICATION_USER all md5" >> "$PGDATA/pg_hba.conf" - psql -v ON_ERROR_STOP=1 -U $POSTGRES_USER <<-EOSQL + sql "$POSTGRES_USER" <<-EOSQL CREATE USER $REPLICATION_USER LOGIN REPLICATION PASSWORD '$REPLICATION_PASSWORD'; @@ -35,27 +42,27 @@ if [ -n "$REPLICATION_USER" ]; then fi echo "Applying schema for $POSTGRES_DB" -psql -v ON_ERROR_STOP=1 -U $WUBLOADER_USER -d $POSTGRES_DB < /schema.sql +sql "$WUBLOADER_USER" -d "$POSTGRES_DB" < /schema.sql if [ -a /mnt/wubloader/nodes.csv ]; then echo "Loading nodes from nodes.csv" - psql -v ON_ERROR_STOP=1 -U $POSTGRES_USER -d $POSTGRES_DB <<-EOF + sql "$POSTGRES_USER" -d "$POSTGRES_DB" <<-EOF COPY nodes FROM '/mnt/wubloader/nodes.csv' DELIMITER ',' CSV HEADER; EOF fi if [ -a /mnt/wubloader/editors.csv ]; then echo "Loading editors from editors.csv" - psql -v ON_ERROR_STOP=1 -U $POSTGRES_USER -d $POSTGRES_DB <<-EOF + sql "$POSTGRES_USER" -d "$POSTGRES_DB" <<-EOF COPY editors FROM '/mnt/wubloader/editors.csv' DELIMITER ',' CSV HEADER; EOF fi -if [ -n "READONLY_USER" ]; then +if [ -n "$READONLY_USER" ]; then echo "Creating $READONLY_USER" # allow $READONLY_USER to connect remotely echo "host all $READONLY_USER all md5" >> "$PGDATA/pg_hba.conf" - psql -v ON_ERROR_STOP=1 -U $POSTGRES_USER -d $POSTGRES_DB <<-EOSQL + sql "$POSTGRES_USER" -d "$POSTGRES_DB" <<-EOSQL CREATE USER $READONLY_USER WITH CONNECTION LIMIT 50 LOGIN PASSWORD '$READONLY_PASSWORD'; GRANT CONNECT ON DATABASE $POSTGRES_DB TO $READONLY_USER; @@ -64,3 +71,14 @@ if [ -n "READONLY_USER" ]; then EOSQL fi + +if [ -n "$BUSCRIBE_USER" ]; then + echo "Creating $BUSCRIBE_USER" + echo "host all $BUSCRIBE_USER all md5" >> "$PGDATA/pg_hba.conf" + sql "$POSTGRES_USER" <<-EOSQL + CREATE USER $BUSCRIBE_USER LOGIN PASSWORD '$BUSCRIBE_PASSWORD'; + EOSQL + + echo "Applying schema for $BUSCRIBE_DB" + sql "$BUSCRIBE_USER" -d "$BUSCRIBE_DB" < /buscribe.sql +fi