aboutsummaryrefslogtreecommitdiff
path: root/scripts/resetdb.postgres.sh
blob: 0160aebf0d80bbba18f2dd4c5a75fbb4daa6621c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash

DEFAULT_USER=$(whoami)

read -p "Enter database name [metachan]: " DB_NAME
DB_NAME=${DB_NAME:-metachan}

read -p "Enter database owner username [$DEFAULT_USER]: " DB_OWNER
DB_OWNER=${DB_OWNER:-$DEFAULT_USER}

read -s -p "Enter password for $DB_OWNER (press Enter for no password): " DB_PASSWORD
echo

if [ -z "$DB_PASSWORD" ]; then
    PSQL_CMD="psql -d postgres"
    CONNECTION_INFO="Connecting as current user without password"
else
    export PGPASSWORD="$DB_PASSWORD"
    PSQL_CMD="psql -U $DB_OWNER -d postgres"
    CONNECTION_INFO="Connecting as $DB_OWNER with password"
fi

DB_EXISTS=$($PSQL_CMD -t -c "SELECT 1 FROM pg_database WHERE datname='$DB_NAME';" 2>/dev/null | xargs)

if [ "$DB_EXISTS" = "1" ]; then
    ACTION="reset"
    OPERATION="Database reset"
else
    ACTION="create"
    OPERATION="Database creation"
fi

echo
echo "=== $OPERATION Configuration ==="
echo "Database name: $DB_NAME"
echo "Owner: $DB_OWNER"
echo "Connection: $CONNECTION_INFO"
if [ "$ACTION" = "reset" ]; then
    echo "Status: Database exists and will be reset"
else
    echo "Status: Database does not exist and will be created"
fi
echo

read -p "Proceed with database $ACTION? [Y/n]: " CONFIRM
if [[ $CONFIRM =~ ^[Nn]$ ]]; then
    echo "Operation cancelled."
    exit 0
fi

if [ "$ACTION" = "reset" ]; then
    echo "Resetting database '$DB_NAME'..."
else
    echo "Creating database '$DB_NAME'..."
fi

$PSQL_CMD -c "DROP DATABASE IF EXISTS $DB_NAME;" 2>/dev/null
if $PSQL_CMD -c "CREATE DATABASE $DB_NAME OWNER $DB_OWNER;" 2>/dev/null; then
    if [ "$ACTION" = "reset" ]; then
        echo "✅ Database '$DB_NAME' successfully reset with owner '$DB_OWNER'"
    else
        echo "✅ Database '$DB_NAME' successfully created with owner '$DB_OWNER'"
    fi
    echo
    echo "Database connection DSN:"
    if [ -z "$DB_PASSWORD" ]; then
        DSN="postgresql://$DB_OWNER@localhost:5432/$DB_NAME?sslmode=disable"
    else
        DSN="postgresql://$DB_OWNER:$DB_PASSWORD@localhost:5432/$DB_NAME?sslmode=disable"
    fi
    echo "$DSN"
else
    echo "❌ Error: Failed to $ACTION database. Please check:"
    echo "  - PostgreSQL is running"
    echo "  - User '$DB_OWNER' exists and has necessary privileges"
    echo "  - No active connections to database '$DB_NAME'"
    exit 1
fi

unset PGPASSWORD