Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions local/bin/db-rebuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ USER=${DB_USER:-${MYSQL_USER:-root}}
PASSWORD=${DB_PASSWORD:-${MYSQL_PASSWORD:-}}
DATABASE=${DB_NAME:-${MYSQL_DATABASE:-database}}
PORT=${DB_PORT:-${MYSQL_TCP_PORT:-3306}}
WEBROOT=${LANDO_WEBROOT:-$(dirname $(dirname $(realpath $0)))}
WEBROOT=${WORKSPACE_FOLDER:-$(dirname $(dirname $(realpath $0)))}
DRUSH_BIN=${WEBROOT}/vendor/bin/drush
DRUSH=${DRUSH_BIN:-drush}
DRUSHTASK=${DRUSH_TASK:-deploy}
TERMINUSENV=${TERMINUS_ENV:-dev}

Expand Down Expand Up @@ -157,4 +159,4 @@ echo "Import completed with status code $?"

# update db with latest code/config changes
echo "Running drush:$DRUSHTASK to update DB with latest configs and baseline migrations."
eval "cd $WEBROOT && drush $DRUSHTASK"
eval "cd $WEBROOT && $DRUSH $DRUSHTASK"
40 changes: 35 additions & 5 deletions local/etc/uceap.d/devcontainer_reset_db.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
function devcontainer_reset_db() {
# Detect the compose project by inspecting the current container
local CURRENT_CONTAINER
local COMPOSE_PROJECT
local WORKSPACE_NAME
local CONTAINER_NAME
local VOLUME_NAME
CURRENT_CONTAINER=$(hostname)
echo "Running from container: $CURRENT_CONTAINER"
local skip_deploy=false

# Loop through all arguments passed to the function
while [[ $# -gt 0 ]]; do
case "$1" in
-sd|--skip-deploy)
skip_deploy=true
shift
;;
*)
echo "Unknown option: $1" >&2
return 1
;;
esac
done


# Get the compose project name from the current container's labels
COMPOSE_PROJECT=$(docker inspect "$CURRENT_CONTAINER" --format '{{index .Config.Labels "com.docker.compose.project"}}' 2>/dev/null)
Expand Down Expand Up @@ -43,24 +64,30 @@ function devcontainer_reset_db() {
echo "Found volume: $VOLUME_NAME"

# Stop and remove the container
docker compose -p "$COMPOSE_PROJECT" -f .devcontainer/docker-compose.yml stop mariadb
docker compose -p "$COMPOSE_PROJECT" -f .devcontainer/docker-compose.yml rm -f mariadb
docker compose -p "$COMPOSE_PROJECT" -f .devcontainer/docker-compose.yml stop mariadb || return 1
docker compose -p "$COMPOSE_PROJECT" -f .devcontainer/docker-compose.yml rm -f mariadb || return 1

# Remove the volume
echo "Removing volume: $VOLUME_NAME"
docker volume rm "$VOLUME_NAME"
docker volume rm "$VOLUME_NAME" || return 1

# Start the container with a fresh volume
docker compose -p "$COMPOSE_PROJECT" -f .devcontainer/docker-compose.yml up -d mariadb
docker compose -p "$COMPOSE_PROJECT" -f .devcontainer/docker-compose.yml up -d mariadb || return 1

echo "Waiting for database to be ready..."
sleep 5

# Clear Drupal cache since the database was reset
echo "Clearing Drupal cache..."
_cwd_workspace
drush cache-rebuild
drush cache-rebuild || return 1

# Check if skip-deploy not passed as option and run deploy
if [ "$skip_deploy" = false ]; then
echo "Re-import local Drupal changes..."
_cwd_workspace
drush deploy || return 1
fi
echo "Database reset complete!"
}

Expand All @@ -72,6 +99,7 @@ Resets the devcontainer database to its original state by recreating the mariadb

``` bash
uceap devcontainer-reset-db
uceap devcontainer-reset-db --skip-deploy
```

## Description
Expand All @@ -82,6 +110,8 @@ This command is faster than `uceap refresh-content` when you only need to reset
2. Removing the anonymous volume that contains the modified database data
3. Recreating the container from the image with baked-in seed data

Use `--skip-deploy` to skip `drush deploy` after the reset.

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.

**Note:** This command only works in devcontainers and will not work in CI or other environments.
Expand Down
Loading