Skip to content

Instantly share code, notes, and snippets.

@Daniel-Ash
Last active December 10, 2023 22:09
Show Gist options
  • Save Daniel-Ash/0ddf135cc0d9341d9d13dadba5641c28 to your computer and use it in GitHub Desktop.
Save Daniel-Ash/0ddf135cc0d9341d9d13dadba5641c28 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Source the utility functions
source ./utils.sh
# Database connection and file path details
hostname="localhost"
port="54322"
dbname="postgres"
user="postgres"
password="postgres"
directoryPath=""
preSeedPath=""
postSeedPath=""
seedPath=""
errorLogPath=""
logPath=""
typePath=""
# Empty log files
echo "" > $logPath
echo "" > $errorLogPath
# Set password environment variable
export PGPASSWORD=$password
print_message $INFO "Starting the database reset process..."
supabase db reset 2>&1 | tee -a $logPath
execute_psql $preSeedPath "Running pre-seed script..."
execute_psql $seedPath "Running seed script..."
execute_psql $postSeedPath "Running post-seed script..."
# Running migration scripts
print_message $INFO "Running migration scripts..."
shopt -s nullglob
for file in "$directoryPath"/*.sql; do
execute_psql "$file" "Running script: $(basename "$file")..."
done
print_message $INFO "Running tests..."
supabase test db 2>&1 | tee -a $logPath
print_message $INFO "Linting database..."
supabase db lint 2>&1 | tee -a $logPath
print_message $INFO "Generating types..."
supabase gen types typescript --local > $typePath 2>&1 | tee -a $logPath
print_message $SUCCESS "Finished successfully!"
print_message $INFO "Cleaning up..."
# Cleanup
unset PGPASSWORD
print_message $INFO "All done"
#!/bin/bash
# Define color codes
INFO="\033[34m"
WARNING="\033[33m"
ERROR="\033[31m"
SUCCESS="\033[32m"
RESET="\033[0m"
# Function to print messages with color
print_message() {
COLOR="$1"
MESSAGE="$2"
echo -e "$COLOR$MESSAGE$RESET" | tee -a $logPath
}
# Function to check if file exists
check_file() {
FILE="$1"
if [ ! -f "$FILE" ]; then
print_message $ERROR "File not found: $FILE"
exit 1
fi
}
# Function to execute psql statement
execute_psql() {
SCRIPT_PATH="$1"
DESCRIPTION="$2"
print_message $INFO "$DESCRIPTION"
check_file "$SCRIPT_PATH"
psql --set ON_ERROR_STOP=on -q -h $hostname -p $port -U $user -d $dbname -f "$SCRIPT_PATH" 2>&1 | tee -a $logPath
exit_status=${PIPESTATUS[0]} # Capture the exit status of psql
if [ $exit_status -ne 0 ]; then
print_message $ERROR "Error running script: $(basename "$SCRIPT_PATH")"
cat $logPath >> $errorLogPath
exit 1
fi
}
@kiwicopple
Copy link

source ./utils.sh

If this doesn't contain anything private, would you mind sharing it too?

@Daniel-Ash
Copy link
Author

Done - hope it helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment