Skip to content

Commit fa1cb89

Browse files
kurowskiclaude
andcommitted
Add devcontainer-reset-db command
This command provides a fast way to reset the database in devcontainers by recreating the mariadb container and removing its data volume. This is much faster than downloading and importing a SQL dump from Pantheon when you only need to reset to the original seeded state. Useful after running e2e tests that modify the database. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent db88888 commit fa1cb89

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
function devcontainer_reset_db() {
2+
# Determine the compose project name from the workspace folder
3+
# Use WORKSPACE_FOLDER env var if set by devcontainer, otherwise derive from pwd
4+
if [ -n "$WORKSPACE_FOLDER" ]; then
5+
WORKSPACE_NAME=$(basename "$WORKSPACE_FOLDER")
6+
else
7+
WORKSPACE_NAME=$(basename "$(pwd)")
8+
fi
9+
COMPOSE_PROJECT="${COMPOSE_PROJECT_NAME:-${WORKSPACE_NAME}_devcontainer}"
10+
11+
echo "Using compose project: $COMPOSE_PROJECT"
12+
13+
# Get the container name
14+
CONTAINER_NAME="${COMPOSE_PROJECT}-mariadb-1"
15+
16+
# Verify the container exists
17+
if ! docker inspect "$CONTAINER_NAME" &>/dev/null; then
18+
echo "Error: Container $CONTAINER_NAME not found"
19+
echo "Available mariadb containers:"
20+
docker ps -a --filter "name=mariadb" --format "{{.Names}}"
21+
return 1
22+
fi
23+
24+
# Get the volume name from the mariadb container before stopping it
25+
VOLUME_NAME=$(docker inspect "$CONTAINER_NAME" --format '{{range .Mounts}}{{if eq .Destination "/var/lib/mysql"}}{{.Name}}{{end}}{{end}}')
26+
27+
if [ -z "$VOLUME_NAME" ]; then
28+
echo "Error: Could not find mariadb volume"
29+
return 1
30+
fi
31+
32+
echo "Found container: $CONTAINER_NAME"
33+
echo "Found volume: $VOLUME_NAME"
34+
35+
# Stop and remove the container
36+
docker compose -p "$COMPOSE_PROJECT" -f .devcontainer/docker-compose.yml stop mariadb
37+
docker compose -p "$COMPOSE_PROJECT" -f .devcontainer/docker-compose.yml rm -f mariadb
38+
39+
# Remove the volume
40+
echo "Removing volume: $VOLUME_NAME"
41+
docker volume rm "$VOLUME_NAME"
42+
43+
# Start the container with a fresh volume
44+
docker compose -p "$COMPOSE_PROJECT" -f .devcontainer/docker-compose.yml up -d mariadb
45+
46+
echo "Waiting for database to be ready..."
47+
sleep 5
48+
49+
echo "Database reset complete!"
50+
}
51+
52+
_devcontainer_reset_db_desc='resets devcontainer database to original state'
53+
_devcontainer_reset_db_help='
54+
Resets the devcontainer database to its original state by recreating the mariadb container and removing its data volume.
55+
56+
# Usage
57+
58+
``` bash
59+
uceap devcontainer-reset-db
60+
```
61+
62+
## Description
63+
64+
This command is faster than `uceap refresh-content` when you only need to reset the database to its original seeded state. It works by:
65+
66+
1. Stopping and removing the mariadb container
67+
2. Removing the anonymous volume that contains the modified database data
68+
3. Recreating the container from the image with baked-in seed data
69+
70+
This is useful after running e2e tests that modify the database (e.g., submitting an application). The database container image has seed data pre-baked, so resetting to a clean state is much faster than downloading and importing a SQL dump from Pantheon.
71+
72+
**Note:** This command only works in devcontainers and will not work in CI or other environments.
73+
'

0 commit comments

Comments
 (0)