Skip to content

Your favorite shift management application!

chore: Add demo scripts for creating and resetting database snapshots (#20)

panteLae9b4df42a6a
diff --git a/demo/README.md b/demo/README.md
new file mode 100644
index 0000000..dac4c38
--- /dev/null
+++ b/demo/README.md
@@ -0,0 +1,93 @@
+# Demo Folder
+
+⚠️ **WARNING: This folder is for DEMO purposes only!**
+
+This directory contains scripts and snapshots specifically designed for creating and managing the BetterShift demo environment. **Do NOT use these scripts on production data** as they will reset/overwrite your database.
+
+## Contents
+
+### Scripts
+
+- **`create-snapshot.sh`** - Creates a snapshot of the current database state for demo purposes
+- **`reset-demo.sh`** - Resets the database to the demo snapshot state
+
+### What This Folder Is For
+
+This folder helps maintain a consistent demo environment by:
+
+- Capturing a pre-configured database state with sample data
+- Quickly resetting to that state for demos or testing
+- Ensuring reproducible demo scenarios
+
+## Usage
+
+### Creating a Demo Snapshot
+
+```bash
+./demo/create-snapshot.sh
+```
+
+This will create a snapshot of your current database in the demo folder.
+
+### Resetting to Demo State
+
+```bash
+./demo/reset-demo.sh
+```
+
+This will **overwrite your current database** with the demo snapshot.
+
+### Automated Demo Reset with Cron
+
+For automated demo environments (e.g., public demos), you can set up a cron job to automatically reset the database at regular intervals.
+
+**Example: Reset every hour**
+
+```bash
+# Edit your crontab
+crontab -e
+
+# Add this line to reset every hour at minute 0
+0 * * * * cd /path/to/bettershift && ./demo/reset-demo.sh >> /var/log/bettershift-demo-reset.log 2>&1
+```
+
+**Example: Reset every 30 minutes**
+
+```bash
+# Add this line to reset twice per hour
+*/30 * * * * cd /path/to/bettershift && ./demo/reset-demo.sh >> /var/log/bettershift-demo-reset.log 2>&1
+```
+
+**Example: Reset daily at 2 AM**
+
+```bash
+# Add this line to reset once per day
+0 2 * * * cd /path/to/bettershift && ./demo/reset-demo.sh >> /var/log/bettershift-demo-reset.log 2>&1
+```
+
+**Important for Cron Setup:**
+
+- Replace `/path/to/bettershift` with your actual project path
+- Ensure the cron user has permission to run the script
+- The script requires `sudo` permissions for file operations
+- Check logs at `/var/log/bettershift-demo-reset.log` for errors
+- Consider using `flock` to prevent overlapping executions:
+ ```bash
+ */30 * * * * flock -n /tmp/demo-reset.lock -c "cd /path/to/bettershift && ./demo/reset-demo.sh" >> /var/log/bettershift-demo-reset.log 2>&1
+ ```
+
+## Important Notes
+
+- ⛔ **Never run these scripts on production databases**
+- 🎯 This folder is exclusively for demo/testing environments
+- 💾 The snapshot file is not tracked in git (see `.gitignore`)
+- 🔄 Always backup your data before running reset scripts
+
+## For Production Use
+
+If you need to backup or restore production data, use proper backup tools and strategies instead of these demo scripts. Consider:
+
+- Database backup solutions (SQLite `.backup` command)
+- File-based backups with proper versioning
+- Automated backup schedules
+- Secure off-site storage
diff --git a/demo/create-snapshot.sh b/demo/create-snapshot.sh
new file mode 100755
index 0000000..7594da8
--- /dev/null
+++ b/demo/create-snapshot.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+# Create initial database snapshot for demo resets
+# Run this once to capture the current database state
+
+set -e
+
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
+DB_PATH="${PROJECT_DIR}/data/sqlite.db"
+SNAPSHOT_PATH="${PROJECT_DIR}/data/demo-snapshot.db"
+
+echo "========================================="
+echo "Create Demo Snapshot"
+echo "========================================="
+
+# Check if database exists
+if [ ! -f "$DB_PATH" ]; then
+ echo "Error: Database not found at: $DB_PATH"
+ echo "Please ensure your application is running and has a database."
+ exit 1
+fi
+
+# Create snapshot
+echo "[$(date '+%Y-%m-%d %H:%M:%S')] Creating snapshot from current database..."
+sudo cp "$DB_PATH" "$SNAPSHOT_PATH"
+echo "[$(date '+%Y-%m-%d %H:%M:%S')] ✓ Snapshot created: $SNAPSHOT_PATH"
+
+echo ""
+echo "========================================="
+echo "✅ Snapshot created successfully!"
+echo "========================================="
+echo ""
+echo "You can now run ./demo/reset-demo.sh to restore to this state."
+echo ""
diff --git a/demo/reset-demo.sh b/demo/reset-demo.sh
new file mode 100755
index 0000000..c4d95cf
--- /dev/null
+++ b/demo/reset-demo.sh
@@ -0,0 +1,51 @@
+#!/bin/bash
+# Standalone demo reset script for BetterShift
+# Resets the database to the initial snapshot state
+# Works with Docker containers - handles permissions correctly
+
+set -e
+
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
+CONTAINER_NAME="${CONTAINER_NAME:-bettershift}"
+DB_PATH="${PROJECT_DIR}/data/sqlite.db"
+SNAPSHOT_PATH="${PROJECT_DIR}/data/demo-snapshot.db"
+
+echo "========================================="
+echo "BetterShift Demo Reset"
+echo "========================================="
+echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting database reset..."
+
+# Check if snapshot exists
+if [ ! -f "$SNAPSHOT_PATH" ]; then
+ echo "Error: Demo snapshot not found at: $SNAPSHOT_PATH"
+ echo "Please run: ./demo/create-snapshot.sh first"
+ exit 1
+fi
+
+# Stop container temporarily to avoid conflicts
+echo "[$(date '+%Y-%m-%d %H:%M:%S')] Stopping container..."
+if ! (cd "$PROJECT_DIR" && docker compose stop "$CONTAINER_NAME"); then
+ echo "Error: Failed to stop container"
+ exit 1
+fi
+
+# Remove current database and restore from snapshot
+sudo rm -f "$DB_PATH"
+sudo cp "$SNAPSHOT_PATH" "$DB_PATH"
+sudo chown $(stat -c '%u:%g' "$PROJECT_DIR/data") "$DB_PATH"
+echo "[$(date '+%Y-%m-%d %H:%M:%S')] ✓ Database restored from snapshot"
+
+# Start container again
+echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting container..."
+(cd "$PROJECT_DIR" && docker compose start "$CONTAINER_NAME")
+
+# Wait for container to be healthy
+echo "[$(date '+%Y-%m-%d %H:%M:%S')] Waiting for container to be ready..."
+timeout 60 sh -c "cd '$PROJECT_DIR' && until docker compose ps '$CONTAINER_NAME' | grep -q '(healthy)'; do sleep 2; done" || echo "Warning: Container may not be fully healthy"
+
+echo ""
+echo "========================================="
+echo "✅ Database reset complete!"
+echo "========================================="
+echo ""