Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: unify Bash code style #5568

Open
wants to merge 27 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
80a8957
delete unused variable
jaskfla Apr 6, 2024
0a80dbe
portable shebang for Bash scripts
jaskfla Apr 6, 2024
bd6f024
prefer `.` to `source`
jaskfla Apr 6, 2024
fa29c90
prefer `echo` without `""` for blank lines
jaskfla Apr 6, 2024
3807364
`download-env-vars`: log in to Bitwarden first
jaskfla Apr 6, 2024
92433da
simplify loop over `*.env.example` files
jaskfla Apr 6, 2024
d34a646
convenience variables for ANSI codes/sequences
jaskfla Apr 6, 2024
64b9ac7
inline `then` in `if` header
jaskfla Apr 6, 2024
2213a9a
update env var message to refer to Bitwarden
jaskfla Apr 6, 2024
8702ea2
prettify `download-env-vars` output
jaskfla Apr 6, 2024
73e1797
prettify `backendStartDev.sh` output
jaskfla Apr 6, 2024
a1db38f
prettify `dumpDatabase.sh` output
jaskfla Apr 6, 2024
77b76b2
prefer ``[[`` over `[`, and `=` over `==` in Bash
jaskfla Apr 6, 2024
1d10de0
prefer `$(…)` over backtick command substitution
jaskfla Apr 7, 2024
0be5e6f
prettify `patchMvRefereshModule.sh` output
jaskfla Apr 7, 2024
15b43de
prefer single quotes when no variables
jaskfla Apr 7, 2024
507fa83
prefer ``(( ))`` over `[[ ]]` for arithmetic
jaskfla Apr 7, 2024
1160b28
prefer array over string in `buildNonInternalDeps`
jaskfla Apr 7, 2024
0ba73f1
prefer array for `getDeployablePackages.sh`
jaskfla Apr 8, 2024
ca00437
refactor: clearer way to get current branch
jaskfla Apr 8, 2024
97c51d6
double-quote variables, avoiding `${…}`
jaskfla Apr 8, 2024
090d23c
prefer array in `mergeEnvForDB.sh`
jaskfla Apr 8, 2024
8da68e4
exit on error
jaskfla Apr 9, 2024
cca8b59
Merge branch 'dev' into shellharden
jaskfla Jun 19, 2024
65907a7
Merge branch 'dev' into shellharden
jaskfla Jun 19, 2024
65bc43b
Merge branch 'dev' into shellharden
jaskfla Jul 8, 2024
8cd0e8f
Merge branch 'dev' into shellharden
jaskfla Jul 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions packages/data-api/scripts/buildAnalyticsMaterializedView.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#!/bin/bash -e
#!/usr/bin/env bash
set -e

DIR=$(pwd "$0")
source "$DIR/../../scripts/bash/mergeEnvForDB.sh"
. "$DIR/../../scripts/bash/mergeEnvForDB.sh"

# Set default port in case it wasn't in .env
: "${DB_PORT:=5432}"

cd scripts
export PGPASSWORD=$DB_PASSWORD
if [[ "$1" == "--force" || "$1" == "-f" ]]; then
psql -p $DB_PORT -h $DB_URL -d $DB_NAME -U $DB_USER -tc "SELECT build_analytics_table(true);"
if [[ $1 = '--force' || $1 = '-f' ]]; then
psql -p "$DB_PORT" -h "$DB_URL" -d "$DB_NAME" -U "$DB_USER" -tc "SELECT build_analytics_table(true);"
else
psql -p $DB_PORT -h $DB_URL -d $DB_NAME -U $DB_USER -tc "SELECT build_analytics_table();"
psql -p "$DB_PORT" -h "$DB_URL" -d "$DB_NAME" -U "$DB_USER" -tc "SELECT build_analytics_table();"
fi
psql -p $DB_PORT -h $DB_URL -d $DB_NAME -U $DB_USER -tc "SELECT create_analytics_table_indexes();"
psql -p "$DB_PORT" -h "$DB_URL" -d "$DB_NAME" -U "$DB_USER" -tc "SELECT create_analytics_table_indexes();"
9 changes: 5 additions & 4 deletions packages/data-api/scripts/dropAnalyticsMaterializedView.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/bin/bash -e
#!/usr/bin/env bash
set -e

DIR=$(pwd "$0")
source "$DIR/../../scripts/bash/mergeEnvForDB.sh"
. "$DIR/../../scripts/bash/mergeEnvForDB.sh"

# Set default port in case it wasn't in .env
: "${DB_PORT:=5432}"

cd scripts
export PGPASSWORD=$DB_PASSWORD
psql -p $DB_PORT -h $DB_URL -d $DB_NAME -U $DB_USER -tc "SELECT drop_analytics_table();"
psql -p $DB_PORT -h $DB_URL -d $DB_NAME -U $DB_USER -tc "SELECT drop_analytics_log_tables();"
psql -p "$DB_PORT" -h "$DB_URL" -d "$DB_NAME" -U "$DB_USER" -tc "SELECT drop_analytics_table();"
psql -p "$DB_PORT" -h "$DB_URL" -d "$DB_NAME" -U "$DB_USER" -tc "SELECT drop_analytics_log_tables();"
10 changes: 6 additions & 4 deletions packages/data-api/scripts/fastRefreshAnalyticsTable.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/bin/bash -e
echo "Fast refreshing analytics table"
#!/usr/bin/env bash
set -e

echo 'Fast refreshing analytics table'

DIR=$(pwd "$0")
source "$DIR/../../scripts/bash/mergeEnvForDB.sh"
. "$DIR/../../scripts/bash/mergeEnvForDB.sh"

# Set default port in case it wasn't in .env
: "${DB_PORT:=5432}"

cd scripts
export PGPASSWORD=$DB_PASSWORD
psql -p $DB_PORT -h $DB_URL -d $DB_NAME -U $DB_USER -tc "SELECT mv\$refreshMaterializedView('analytics', 'public', true);"
psql -p "$DB_PORT" -h "$DB_URL" -d "$DB_NAME" -U "$DB_USER" -tc "SELECT mv\$refreshMaterializedView('analytics', 'public', true);"
14 changes: 8 additions & 6 deletions packages/data-api/scripts/fullRefreshAnalyticsTable.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/bin/bash -e
echo "Fully refreshing analytics table"
#!/usr/bin/env bash
set -e

echo 'Fully refreshing analytics table'

DIR=$(pwd "$0")
source "$DIR/../../scripts/bash/mergeEnvForDB.sh"
. "$DIR/../../scripts/bash/mergeEnvForDB.sh"

# Set default port in case it wasn't in .env
: "${DB_PORT:=5432}"

cd scripts
export PGPASSWORD=$DB_PASSWORD
psql -p $DB_PORT -h $DB_URL -d $DB_NAME -U $DB_USER -tc "SELECT drop_analytics_table_indexes();"
psql -p $DB_PORT -h $DB_URL -d $DB_NAME -U $DB_USER -tc "SELECT mv\$refreshMaterializedView('analytics', 'public');"
psql -p $DB_PORT -h $DB_URL -d $DB_NAME -U $DB_USER -tc "SELECT create_analytics_table_indexes();"
psql -p "$DB_PORT" -h "$DB_URL" -d "$DB_NAME" -U "$DB_USER" -tc "SELECT drop_analytics_table_indexes();"
psql -p "$DB_PORT" -h "$DB_URL" -d "$DB_NAME" -U "$DB_USER" -tc "SELECT mv\$refreshMaterializedView('analytics', 'public');"
psql -p "$DB_PORT" -h "$DB_URL" -d "$DB_NAME" -U "$DB_USER" -tc "SELECT create_analytics_table_indexes();"
10 changes: 4 additions & 6 deletions packages/data-api/scripts/installMvRefreshModule.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/bin/bash
DIR=$(dirname "$0")


DIR=$(pwd "$0")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Immediately overwritten in line 4

source "$DIR/../../scripts/bash/mergeEnvForDB.sh"
Expand All @@ -9,10 +7,10 @@ source "$DIR/../../scripts/bash/mergeEnvForDB.sh"
: "${DB_PORT:=5432}"

export PGPASSWORD=$DB_PG_PASSWORD
MV_REFRESH_EXISTS=`psql -p $DB_PORT -X -A -h $DB_URL -d $DB_NAME -U $DB_PG_USER -t -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name = '$DB_MV_USER'"`
MV_REFRESH_EXISTS=$(psql -p "$DB_PORT" -X -A -h "$DB_URL" -d "$DB_NAME" -U "$DB_PG_USER" -t -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name = '$DB_MV_USER'")

if [ "$MV_REFRESH_EXISTS" == "$DB_MV_USER" ]; then
echo "Fast Refresh module already exists, skipping installation"
if [[ $MV_REFRESH_EXISTS = "$DB_MV_USER" ]]; then
echo 'Fast Refresh module already exists, skipping installation'
else
git submodule update --init scripts/pg-mv-fast-refresh
cd scripts/pg-mv-fast-refresh/
Expand All @@ -23,4 +21,4 @@ else
fi

export PGPASSWORD=$DB_PG_PASSWORD
psql -p $DB_PORT --set=db_user="$DB_USER" --set=mv_user="$DB_MV_USER" --set=db_name="$DB_NAME" -h $DB_URL -d $DB_NAME -U $DB_PG_USER -f scripts/grantMvRefreshPermissions.sql
psql -p "$DB_PORT" --set=db_user="$DB_USER" --set=mv_user="$DB_MV_USER" --set=db_name="$DB_NAME" -h "$DB_URL" -d "$DB_NAME" -U "$DB_PG_USER" -f scripts/grantMvRefreshPermissions.sql
29 changes: 15 additions & 14 deletions packages/data-api/scripts/patchMvRefreshModule.sh
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
#!/bin/bash
#!/usr/bin/env bash

DIR=$(pwd "$0")
source "$DIR/../../scripts/bash/mergeEnvForDB.sh"
. "$DIR/../../scripts/bash/mergeEnvForDB.sh"
. "$DIR/../../scripts/bash/ansiControlSequences.sh"

COMMAND=$1

if [[ "$COMMAND" == "" ]]; then
echo "Error: missing patch command! Must be one of: up, down, create"
if [[ $COMMAND = '' ]]; then
echo "${RED}Error: missing patch command! Must be one of: up, down, create${RESET}"
exit 1
fi

if [[ "$COMMAND" == "create" ]]; then
echo "Enter patch name: "
if [[ $COMMAND = create ]]; then
echo 'Enter patch name: '
read PATCH_NAME
fi

VERSION=$2

if [[ "$VERSION" == "" ]]; then
echo "Version unspecified, defaulting to database mvrefresh version"
if [[ $VERSION = '' ]]; then
echo 'Version unspecified, defaulting to database mvrefresh version'

# Set default port in case it wasn't in .env
: "${DB_PORT:=5432}"

export PGPASSWORD=$DB_PASSWORD
VERSION_SQL_FUNC="SELECT mv\$version()"
VERSION=`psql -p $DB_PORT -X -A -h $DB_URL -d $DB_NAME -U $DB_USER -t -c "$VERSION_SQL_FUNC"`
VERSION=$(psql -p "$DB_PORT" -X -A -h "$DB_URL" -d "$DB_NAME" -U "$DB_USER" -t -c "$VERSION_SQL_FUNC")

if [[ "$VERSION" == "" ]]; then
echo "Error: failed to detect mvrefresh version from database"
if [[ $VERSION = '' ]]; then
echo "${RED}Error: failed to detect mvrefresh version from database${RESET}"
exit 1
fi

echo "Using version: $VERSION"
echo "Using version: ${BOLD}${GREEN}$VERSION${RESET}"
fi

if [[ ! -d "./scripts/patches/$VERSION" && ! "$COMMAND" == "create" ]]; then
if [[ ! -d ./scripts/patches/$VERSION && $COMMAND != create ]]; then
echo "No patches exist for version: $VERSION, skipping"
else
ts-node ./scripts/patchMvRefresh.ts $COMMAND:$VERSION $PATCH_NAME --migrations-dir "./scripts/patches" --table "patches" -v --config-file "../../babel.config.json"
ts-node ./scripts/patchMvRefresh.ts $COMMAND:$VERSION $PATCH_NAME --migrations-dir './scripts/patches' --table 'patches' -v --config-file '../../babel.config.json'
fi
5 changes: 3 additions & 2 deletions packages/data-api/scripts/refreshAnalyticsTable.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash -e
#!/usr/bin/env bash
set -e

if [[ "$1" == "--full" || "$1" == "-f" ]]; then
if [[ $1 = '--full' || $1 = '-f' ]]; then
./scripts/fullRefreshAnalyticsTable.sh
else
./scripts/fastRefreshAnalyticsTable.sh
Expand Down
13 changes: 7 additions & 6 deletions packages/data-api/scripts/uninstallMvRefreshModule.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#!/bin/bash
#!/usr/bin/env bash
set -e

DIR=$(pwd "$0")
source "$DIR/../../scripts/bash/mergeEnvForDB.sh"
. "$DIR/../../scripts/bash/mergeEnvForDB.sh"

# Set default port in case it wasn't in .env
: "${DB_PORT:=5432}"

export PGPASSWORD=$DB_PG_PASSWORD
MV_REFRESH_EXISTS=`psql -X -A -p $DB_PORT -h $DB_URL -d $DB_NAME -U $DB_PG_USER -t -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name = '$DB_MV_USER'"`
MV_REFRESH_EXISTS=$(psql -X -A -p "$DB_PORT" -h "$DB_URL" -d "$DB_NAME" -U "$DB_PG_USER" -t -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name = '$DB_MV_USER'")

if [ "$MV_REFRESH_EXISTS" != "$DB_MV_USER" ]; then
echo "Fast Refresh module does not exist, skipping uninstallation"
if [[ $MV_REFRESH_EXISTS != "$DB_MV_USER" ]]; then
echo 'Fast Refresh module does not exist, skipping uninstallation'
else
git submodule update --init scripts/pg-mv-fast-refresh
cd scripts/pg-mv-fast-refresh/
Expand All @@ -19,5 +20,5 @@ else
cd ../..
git submodule deinit scripts/pg-mv-fast-refresh
export PGPASSWORD=$DB_PG_PASSWORD
psql -p $DB_PORT -h $DB_URL -d $DB_NAME -U $DB_PG_USER -tc "REVOKE ALL PRIVILEGES on schema public FROM $DB_MV_USER; DROP ROLE IF EXISTS $DB_MV_USER;"
psql -p "$DB_PORT" -h "$DB_URL" -d "$DB_NAME" -U "$DB_PG_USER" -tc "REVOKE ALL PRIVILEGES on schema public FROM $DB_MV_USER; DROP ROLE IF EXISTS $DB_MV_USER;"
fi
17 changes: 12 additions & 5 deletions packages/data-lake-api/scripts/checkTestDataLakeExists.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
#!/bin/bash -e
#!/usr/bin/env bash
set -e

DIR=$(pwd "$0")
source "$DIR/../../scripts/bash/mergeEnvForDB.sh"
. "$DIR/../../scripts/bash/mergeEnvForDB.sh"
. "$DIR/../../scripts/bash/ansiControlSequences.sh"

# Set default port in case it wasn't in .env
: "${DATA_LAKE_DB_PORT:=5432}"

if [ "$(PGPASSWORD=$DB_PG_PASSWORD psql -p $DATA_LAKE_DB_PORT -X -A -h $DATA_LAKE_DB_URL -U $DB_PG_USER -t -c "SELECT 1 FROM pg_database WHERE datname='$DATA_LAKE_DB_NAME'" )" = '1' ]
then
if [[ "$(PGPASSWORD=$DB_PG_PASSWORD psql -p $DATA_LAKE_DB_PORT -X -A -h $DATA_LAKE_DB_URL -U $DB_PG_USER -t -c "SELECT 1 FROM pg_database WHERE datname='$DATA_LAKE_DB_NAME'" )" = '1' ]]; then
exit 0
fi

echo -e "Error: $DATA_LAKE_DB_NAME database does not exist!\n\nTo create it, please get the .env file from Bitwarden then run:\nyarn workspace @tupaia/data-lake-api setup-test-data-lake\n"
echo -e "${RED}Error: $DATA_LAKE_DB_NAME database does not exist!${RESET}"
echo 'To create it, make sure you have the environment variables from Bitwarden and run:'
echo
echo -e " ${BOLD}yarn workspace @tupaia/data-lake-api setup-test-data-lake${RESET}"
echo
echo -e "If you’re missing environment variables, see ${MAGENTA}https://beyond-essential.slab.com/posts/tupaia-monorepo-setup-v5egpdpq#hvfnz-set-environment-variables${RESET}."

exit 1
7 changes: 4 additions & 3 deletions packages/data-lake-api/scripts/setupTestDataLake.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/bin/bash -e
#!/usr/bin/env bash
set -e

DIR=$(pwd "$0")
source "$DIR/../../scripts/bash/mergeEnvForDB.sh"
. "$DIR/../../scripts/bash/mergeEnvForDB.sh"

# Set default port in case it wasn't in .env
: "${DATA_LAKE_DB_PORT:=5432}"

TUPAIA_USER_EXISTS=`PGPASSWORD=$DB_PG_PASSWORD psql -p $DATA_LAKE_DB_PORT -X -A -h $DATA_LAKE_DB_URL -U $DB_PG_USER -t -c "SELECT rolname FROM pg_catalog.pg_roles WHERE rolname = '$DATA_LAKE_DB_USER'"`

if [ -z "$TUPAIA_USER_EXISTS" ]; then
if [[ -z $TUPAIA_USER_EXISTS ]]; then
PGPASSWORD=$DB_PG_PASSWORD psql -h $DATA_LAKE_DB_URL -p $DATA_LAKE_DB_PORT -U $DB_PG_USER -c "CREATE ROLE $DATA_LAKE_DB_USER LOGIN PASSWORD '$DATA_LAKE_DB_PASSWORD'"
fi

Expand Down
20 changes: 11 additions & 9 deletions packages/database/scripts/checkTestDatabaseExists.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#!/bin/bash -e
#!/usr/bin/env bash
set -e

DIR=$(pwd "$0")
source "$DIR/../../scripts/bash/mergeEnvForDB.sh"
. "$DIR/../../scripts/bash/ansiControlSequences.sh"
. "$DIR/../../scripts/bash/mergeEnvForDB.sh"

# Set default port in case it wasn't in .env
: "${DB_PORT:=5432}"

if [ "$(PGPASSWORD=$DB_PG_PASSWORD psql -p $DB_PORT -X -A -h $DB_URL -U $DB_PG_USER -t -c "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'" )" = '1' ]
then
if [ "$(PGPASSWORD=$DB_PG_PASSWORD psql -p $DB_PORT -X -A -h $DB_URL -U $DB_PG_USER -t -c "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'" )" = '1' ]; then
exit 0
fi

echo -e "\033[31mError: $DB_NAME database does not exist!\033[m"
echo "To create it, get the .env file from Bitwarden then run:"
echo ""
echo -e " \033[1myarn workspace @tupaia/database setup-test-database\033[m"
echo ""
echo -e "${RED}Error: $DB_NAME database does not exist!${RESET}"
echo 'To create it, make sure you have the environment variables from Bitwarden and run:'
echo
echo -e " ${BOLD}yarn workspace @tupaia/database setup-test-database${RESET}"
echo
echo -e "If you’re missing environment variables, see ${MAGENTA}https://beyond-essential.slab.com/posts/tupaia-monorepo-setup-v5egpdpq#hvfnz-set-environment-variables${RESET}."

exit 1
35 changes: 18 additions & 17 deletions packages/database/scripts/dumpDatabase.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash -e
#!/usr/bin/env bash
set -e

function print_help() {
cat <<EOF
Expand All @@ -13,32 +14,33 @@ EOF

# https://stackoverflow.com/questions/12498304/using-bash-to-display-a-progress-indicator
function show_loading_spinner() {
eval $2 &
eval "$2" &
pid=$! # Process Id of the previous running command

spin='-\|/'

i=0
while kill -0 $pid 2>/dev/null
while kill -0 "$pid" 2>/dev/null
do
i=$(( (i+1) %4 ))
printf "\r$1 ${spin:$i:1}"
sleep .5
done
printf "\r$1 "
echo "" # reset prompt
echo # reset prompt
}

DIR=$(pwd "$0")
source "$DIR/../../scripts/bash/mergeEnvForDB.sh"
. "$DIR/../../scripts/bash/mergeEnvForDB.sh"
. "$DIR/../../scripts/bash/ansiControlSequences.sh"

DUMP_FILE_NAME="dump.sql"
DUMP_FILE_NAME='dump.sql'

identity_file=""
server="dev"
target_dir="."
identity_file=''
server='dev'
target_dir='.'

while [ "$1" != "" ]; do
while [[ $1 != '' ]]; do
case $1 in
-s | --server)
shift
Expand All @@ -55,7 +57,7 @@ while [ "$1" != "" ]; do
exit
;;
*)
if [ "$identity_file" == "" ]; then
if [[ $identity_file = '' ]]; then
identity_file=$1
shift
else
Expand All @@ -66,13 +68,13 @@ while [ "$1" != "" ]; do
esac
done

if [ "$identity_file" == "" ]; then
if [[ $identity_file = '' ]]; then
print_help
exit 1
fi

if [ "$DB_PG_USER" == "" ] || [ "$DB_PG_PASSWORD" == "" ]; then
echo "Missing postgres user credential env vars in @tupaia/database .env file. Check Bitwarden for variables and add them to the .env file"
if [[ $DB_PG_USER = '' || $DB_PG_PASSWORD = '' ]]; then
echo -e "${RED}Missing Postgres user credential env vars in @tupaia/database .env file.${RESET} Check Bitwarden for variables and add them to the .env file"
exit 1
fi

Expand All @@ -86,6 +88,5 @@ target_zip_path="$target_path.gz"
show_loading_spinner "Dumping database to $target_zip_path" "PGPASSWORD=$DB_PG_PASSWORD pg_dump \"host=$host user=$DB_PG_USER dbname=tupaia sslmode=require sslkey=$identity_file\" -Z1 -f $target_zip_path"
show_loading_spinner "Unzipping $target_zip_path" "gunzip -f $target_zip_path"

echo "Dump file available at $target_path"

echo "Done!"
echo "Dump file available at $target_path"
echo -e "${GREEN}Done!${RESET}"
2 changes: 1 addition & 1 deletion packages/database/scripts/migrateCreate.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

read -p "Enter migration name: " MIGRATION_NAME
read -p "Enter the scope of this migration ('schema' or 'data'): " MIGRATION_SCOPE
Expand Down
Loading