# What database to read table specifications from.
HOST=scholar-reader.c5tvjmptvzlz.us-west-2.rds.amazonaws.com
DATABASE=scholar-reader
USERNAME=api
PORT=5432

# Edit this variable if you want to generate the 'CREATE TABLE' commands from a different
# schema already in the database (e.g., 'public' or 'dev'.)
SCHEMA=dev

TEST_SCHEMA_NAME=test
CREATE_TABLES_FILE=create_test_tables.sql


cat > $CREATE_TABLES_FILE <<- EOF
--
-- This SQL file is meant to be used to initialize tables in a test database prior to unit tests.
-- The commands in this file were generated by running ./make_create_tables_script.sh.
-- Whenever the tables change, these commands should be updated by running that script again.
--

EOF

echo "Running script. Be prepared to enter the password for the user 'api' for the Postgres database."

pg_dump \
  --db $DATABASE \
  --host $HOST \
  --username $USERNAME \
  --port $PORT \
  --schema-only \
  --no-owner \
  --no-privileges \
  --schema=$SCHEMA >> $CREATE_TABLES_FILE

# Due to limitations in the 'pg_dump' command, all tables are output by the pg_dump command
# with their fully-qualified names (including schema). See https://stackoverflow.com/q/4191653/2096369.
# Because the tests need to know an exact schema name, the name of the dumped schema is
# renamed to a name the tests expect.
echo "ALTER SCHEMA $SCHEMA RENAME TO $TEST_SCHEMA_NAME;" >> $CREATE_TABLES_FILE
