-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest-db.sh
More file actions
executable file
Β·189 lines (163 loc) Β· 5.23 KB
/
Copy pathtest-db.sh
File metadata and controls
executable file
Β·189 lines (163 loc) Β· 5.23 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
#!/bin/bash
# Test Database Management Script
# Manages the Docker PostgreSQL database for testing
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
print_color() {
printf "${1}${2}${NC}\n"
}
# Docker compose file for tests
DOCKER_COMPOSE_FILE="docker-compose.test.yml"
CONTAINER_NAME="meutch-test-db"
TEST_DB_NAME="meutch_test"
ensure_test_db_exists() {
DB_EXISTS=$(docker exec $CONTAINER_NAME psql -U test_user -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='${TEST_DB_NAME}'" 2>/dev/null | tr -d ' ')
if [ "$DB_EXISTS" != "1" ]; then
print_color $BLUE "π Creating test database: ${TEST_DB_NAME}"
docker exec $CONTAINER_NAME psql -U test_user -d postgres -c "CREATE DATABASE ${TEST_DB_NAME}" > /dev/null
fi
}
# Function to check if Docker is running
check_docker() {
if ! docker info > /dev/null 2>&1; then
print_color $RED "β Docker is not running. Please start Docker and try again."
exit 1
fi
}
# Function to start the test database
start_db() {
print_color $BLUE "π Starting test database..."
check_docker
# Start the Docker database
docker compose -f $DOCKER_COMPOSE_FILE up -d
# Wait for database to be ready
print_color $BLUE "β³ Waiting for database to be ready..."
timeout=30
while [ $timeout -gt 0 ]; do
if docker compose -f $DOCKER_COMPOSE_FILE exec -T test-postgres pg_isready -U test_user -d postgres > /dev/null 2>&1; then
print_color $GREEN "β
Test database is ready!"
break
fi
sleep 1
((timeout--))
done
if [ $timeout -eq 0 ]; then
print_color $RED "β Timeout waiting for database to be ready"
exit 1
fi
ensure_test_db_exists
print_color $GREEN "π Test database started successfully!"
print_color $BLUE "Database URL (tests): postgresql://test_user:test_password@localhost:5433/${TEST_DB_NAME}"
print_color $BLUE "Database URL (dev): postgresql://test_user:test_password@localhost:5433/meutch_dev"
}
# Function to stop the test database
stop_db() {
print_color $BLUE "π Stopping test database..."
docker compose -f $DOCKER_COMPOSE_FILE down
print_color $GREEN "β
Test database stopped!"
}
# Function to restart the test database
restart_db() {
print_color $BLUE "π Restarting test database..."
stop_db
start_db
}
# Function to reset the test database (clean slate)
reset_db() {
print_color $BLUE "ποΈ Resetting test database..."
docker compose -f $DOCKER_COMPOSE_FILE down -v
start_db
print_color $GREEN "β
Test database reset complete!"
}
# Function to show database status
status_db() {
print_color $BLUE "π Test database status:"
if docker ps --filter "name=$CONTAINER_NAME" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep -q $CONTAINER_NAME; then
docker ps --filter "name=$CONTAINER_NAME" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
print_color $GREEN "β
Test database is running"
else
print_color $YELLOW "β οΈ Test database is not running"
fi
}
# Function to connect to database with psql
connect_db() {
print_color $BLUE "π Connecting to test database..."
docker compose -f $DOCKER_COMPOSE_FILE exec test-postgres psql -U test_user -d $TEST_DB_NAME
}
# Function to run database migrations
migrate_db() {
print_color $BLUE "π Running database migrations..."
# Set the test database URL
export FLASK_APP=app.py
export DATABASE_URL="postgresql://test_user:test_password@localhost:5433/$TEST_DB_NAME"
# Run migrations
flask db upgrade
print_color $GREEN "β
Database migrations completed!"
}
# Function to show logs
logs_db() {
print_color $BLUE "π Test database logs:"
docker compose -f $DOCKER_COMPOSE_FILE logs -f test-postgres
}
# Function to show usage
usage() {
echo "Usage: $0 {start|stop|restart|reset|status|connect|migrate|logs|help}"
echo ""
echo "Commands:"
echo " start Start the test database"
echo " stop Stop the test database"
echo " restart Restart the test database"
echo " reset Reset the test database (removes all data)"
echo " status Show database status"
echo " connect Connect to database with psql"
echo " migrate Run database migrations"
echo " logs Show database logs"
echo " help Show this help message"
echo ""
echo "Examples:"
echo " $0 start # Start the test database"
echo " $0 reset # Reset database to clean state"
echo " $0 migrate # Run database migrations"
echo " $0 connect # Connect with psql"
}
# Main command handling
case "$1" in
start)
start_db
;;
stop)
stop_db
;;
restart)
restart_db
;;
reset)
reset_db
;;
status)
status_db
;;
connect)
connect_db
;;
migrate)
migrate_db
;;
logs)
logs_db
;;
help|--help|-h)
usage
;;
*)
print_color $RED "β Unknown command: $1"
echo ""
usage
exit 1
;;
esac