-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb-manage.sh
More file actions
executable file
·211 lines (182 loc) · 5.17 KB
/
db-manage.sh
File metadata and controls
executable file
·211 lines (182 loc) · 5.17 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
# Database management script for AGI Memory System
# Usage: ./db-manage.sh [command]
set -e # Exit on any error
# Load environment variables if .env exists
if [ -f .env ]; then
export $(cat .env | grep -v '^#' | xargs)
fi
# Default values
POSTGRES_USER=${POSTGRES_USER:-postgres}
POSTGRES_DB=${POSTGRES_DB:-memory_db}
POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-password}
show_help() {
echo "AGI Memory Database Management Script"
echo ""
echo "Usage: ./db-manage.sh [command]"
echo ""
echo "Commands:"
echo " start Start the database container"
echo " stop Stop the database container"
echo " restart Restart the database container"
echo " reset Delete and recreate the database (DESTRUCTIVE)"
echo " status Show database container status"
echo " logs Show database logs"
echo " wait Wait for database to be ready"
echo " shell Open psql shell to database"
echo " backup Create a backup of the database"
echo " test Run database tests"
echo " help Show this help message"
echo ""
echo "Examples:"
echo " ./db-manage.sh reset # Delete and recreate database"
echo " ./db-manage.sh start # Start database and wait for ready"
echo " ./db-manage.sh shell # Open database shell"
}
wait_for_db() {
echo "Waiting for database to be ready..."
# Wait for container to be running
until docker compose ps db | grep -q "Up"; do
echo "Container is not running yet, waiting..."
sleep 2
done
# Wait for PostgreSQL to be ready
until docker compose exec db pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB" > /dev/null 2>&1; do
echo "Database is not ready yet, waiting..."
sleep 2
done
echo "Database is ready!"
# Test connection
if docker compose exec db psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 1;" > /dev/null 2>&1; then
echo "Database connection verified!"
return 0
else
echo "Database connection test failed"
return 1
fi
}
start_db() {
echo "Starting database..."
docker compose up -d db
wait_for_db
echo "Database is fully operational!"
}
stop_db() {
echo "Stopping database..."
docker compose stop db
echo "Database stopped."
}
restart_db() {
echo "Restarting database..."
docker compose restart db
wait_for_db
echo "Database restarted and ready!"
}
reset_db() {
echo "⚠️ WARNING: This will DELETE ALL DATA in the database!"
echo "Are you sure you want to continue? (y/N)"
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
echo "Resetting database..."
# Stop and remove containers and volumes
docker compose down -v
# Remove any orphaned containers
docker compose rm -f
# Start fresh
echo "Starting fresh database..."
docker compose up -d db
# Wait for it to be ready
wait_for_db
echo "✅ Database has been reset and reinitialized!"
echo "Schema has been automatically loaded from schema.sql"
else
echo "Database reset cancelled."
fi
}
show_status() {
echo "Database container status:"
docker compose ps db
echo ""
if docker compose ps db | grep -q "Up"; then
echo "Database health check:"
if docker compose exec db pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB" > /dev/null 2>&1; then
echo "✅ Database is healthy and accepting connections"
else
echo "❌ Database container is up but not accepting connections"
fi
else
echo "❌ Database container is not running"
fi
}
show_logs() {
echo "Database logs (last 50 lines):"
docker compose logs --tail=50 db
}
open_shell() {
echo "Opening database shell..."
echo "Type \\q to exit"
docker compose exec db psql -U "$POSTGRES_USER" -d "$POSTGRES_DB"
}
backup_db() {
timestamp=$(date +%Y%m%d_%H%M%S)
backup_file="backup_${timestamp}.sql"
echo "Creating backup: $backup_file"
docker compose exec db pg_dump -U "$POSTGRES_USER" -d "$POSTGRES_DB" > "$backup_file"
if [ $? -eq 0 ]; then
echo "✅ Backup created successfully: $backup_file"
else
echo "❌ Backup failed"
return 1
fi
}
run_tests() {
echo "Running database tests..."
if [ -f "test.py" ]; then
python test.py
else
echo "No test.py file found"
return 1
fi
}
# Main command handling
case "${1:-help}" in
start)
start_db
;;
stop)
stop_db
;;
restart)
restart_db
;;
reset)
reset_db
;;
status)
show_status
;;
logs)
show_logs
;;
wait)
wait_for_db
;;
shell)
open_shell
;;
backup)
backup_db
;;
test)
run_tests
;;
help|--help|-h)
show_help
;;
*)
echo "Unknown command: $1"
echo ""
show_help
exit 1
;;
esac