initial devcontainer setup
This commit is contained in:
commit
b9b7aa6a08
14 changed files with 411 additions and 0 deletions
79
.devcontainer/restore_db.sh
Executable file
79
.devcontainer/restore_db.sh
Executable file
|
@ -0,0 +1,79 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
safe_mode=false
|
||||
|
||||
# Parse command line arguments
|
||||
while getopts ":s" opt; do
|
||||
case $opt in
|
||||
s)
|
||||
safe_mode=true
|
||||
;;
|
||||
\?)
|
||||
echo "Invalid option: -$OPTARG" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Check if safe mode is enabled
|
||||
if [ "$safe_mode" = true ]; then
|
||||
# Count objects in public schema
|
||||
object_count=$(psql -d ${PGDATABASE} -t -c "SELECT COUNT(*) FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'public';" | tr -d ' ')
|
||||
|
||||
if [ "$object_count" -gt 0 ]; then
|
||||
echo "Safe mode: Public schema is not empty. Aborting import."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
# Shift the options so that $1 is the input path
|
||||
shift $((OPTIND-1))
|
||||
|
||||
# If we were't given any arguments, make the user pick one!
|
||||
if [ $# -eq 0 ]; then
|
||||
INPUT_PATH=$(find $PROJECT_ROOT/.snapshots -type f -name "*.sql" | sort -r | fzf --no-sort --header="Select database to restore")
|
||||
else
|
||||
INPUT_PATH="$1"
|
||||
fi
|
||||
|
||||
# Determine if input is directory or file
|
||||
if [ -d "$INPUT_PATH" ]; then
|
||||
# Find latest SQL file by name sorting (assuming timestamped filenames)
|
||||
LATEST_FILE=$(ls "$INPUT_PATH"/*.sql 2>/dev/null | sort -r | head -n 1)
|
||||
|
||||
if [ -z "$LATEST_FILE" ]; then
|
||||
echo "No .sql files found in directory"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
DUMP_FILE="$LATEST_FILE"
|
||||
elif [ -f "$INPUT_PATH" ]; then
|
||||
DUMP_FILE="$INPUT_PATH"
|
||||
else
|
||||
echo "Invalid path: $INPUT_PATH"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Validate the file exists
|
||||
if [ ! -f "$DUMP_FILE" ]; then
|
||||
echo "SQL file not found: $DUMP_FILE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Using dump file: $DUMP_FILE"
|
||||
|
||||
echo " - Clearing existing objects"
|
||||
# Clean existing objects using schema drop
|
||||
psql -d ${PGDATABASE} -v ON_ERROR_STOP=1 <<-EOSQL > /dev/null 2>&1
|
||||
DROP SCHEMA PUBLIC CASCADE;
|
||||
CREATE SCHEMA PUBLIC;
|
||||
EOSQL
|
||||
|
||||
echo " - Importing database"
|
||||
# Import new dump
|
||||
psql -d ${PGDATABASE} -v ON_ERROR_STOP=1 -f "$DUMP_FILE" > /dev/null 2>&1
|
||||
|
||||
echo "Database imported successfully from: $DUMP_FILE"
|
Loading…
Add table
Add a link
Reference in a new issue