Files
bakery-ia/scripts/dev-reset-database.sh
2025-09-30 08:12:45 +02:00

231 lines
7.2 KiB
Bash
Executable File

#!/bin/bash
# Development Database Reset Script
#
# This script helps developers reset their databases to a clean slate.
# It can reset individual services or all services at once.
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
# Configuration
NAMESPACE="bakery-ia"
SERVICES=("alert-processor" "auth" "external" "forecasting" "inventory" "notification" "orders" "pos" "production" "recipes" "sales" "suppliers" "tenant" "training")
print_banner() {
echo -e "${BLUE}"
echo "╔═══════════════════════════════════════════════════════════════╗"
echo "║ Bakery-IA Development Database Reset ║"
echo "║ ║"
echo "║ This script will reset database(s) to a clean slate ║"
echo "║ WARNING: This will delete all existing data! ║"
echo "╚═══════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
}
show_usage() {
echo "Usage: $0 [OPTIONS] [SERVICE]"
echo ""
echo "Options:"
echo " -a, --all Reset all services"
echo " -s, --service NAME Reset specific service"
echo " -l, --list List available services"
echo " -y, --yes Skip confirmation prompts"
echo " -h, --help Show this help"
echo ""
echo "Examples:"
echo " $0 --service auth # Reset only auth service"
echo " $0 --all # Reset all services"
echo " $0 auth # Reset auth service (short form)"
}
list_services() {
echo -e "${YELLOW}Available services:${NC}"
for service in "${SERVICES[@]}"; do
echo " - $service"
done
}
confirm_action() {
local service="$1"
local message="${2:-Are you sure you want to reset}"
if [[ "$SKIP_CONFIRM" == "true" ]]; then
return 0
fi
echo -e "${YELLOW}$message the database for service: ${RED}$service${YELLOW}?${NC}"
echo -e "${RED}This will delete ALL existing data!${NC}"
read -p "Type 'yes' to continue: " confirmation
if [[ "$confirmation" != "yes" ]]; then
echo -e "${YELLOW}Operation cancelled.${NC}"
return 1
fi
return 0
}
enable_force_recreate() {
echo -e "${BLUE}Enabling force recreate mode...${NC}"
# Update the development config
kubectl patch configmap development-config -n "$NAMESPACE" \
--patch='{"data":{"DB_FORCE_RECREATE":"true"}}' 2>/dev/null || \
kubectl create configmap development-config -n "$NAMESPACE" \
--from-literal=DB_FORCE_RECREATE=true \
--from-literal=DEVELOPMENT_MODE=true \
--from-literal=DEBUG_LOGGING=true || true
}
disable_force_recreate() {
echo -e "${BLUE}Disabling force recreate mode...${NC}"
kubectl patch configmap development-config -n "$NAMESPACE" \
--patch='{"data":{"DB_FORCE_RECREATE":"false"}}' 2>/dev/null || true
}
reset_service() {
local service="$1"
echo -e "${BLUE}Resetting database for service: $service${NC}"
# Delete existing migration job if it exists
kubectl delete job "${service}-migration" -n "$NAMESPACE" 2>/dev/null || true
# Wait a moment for cleanup
sleep 2
# Create new migration job
echo -e "${YELLOW}Creating migration job for $service...${NC}"
kubectl apply -f "infrastructure/kubernetes/base/migrations/${service}-migration-job.yaml"
# Wait for job to complete
echo -e "${YELLOW}Waiting for migration to complete...${NC}"
kubectl wait --for=condition=complete job/"${service}-migration" -n "$NAMESPACE" --timeout=300s
# Check job status
if kubectl get job "${service}-migration" -n "$NAMESPACE" -o jsonpath='{.status.succeeded}' | grep -q "1"; then
echo -e "${GREEN}✓ Database reset completed successfully for $service${NC}"
else
echo -e "${RED}✗ Database reset failed for $service${NC}"
echo "Check logs with: kubectl logs -l job-name=${service}-migration -n $NAMESPACE"
return 1
fi
}
reset_all_services() {
echo -e "${BLUE}Resetting databases for all services...${NC}"
local failed_services=()
for service in "${SERVICES[@]}"; do
echo -e "\n${BLUE}Processing $service...${NC}"
if ! reset_service "$service"; then
failed_services+=("$service")
fi
done
if [[ ${#failed_services[@]} -eq 0 ]]; then
echo -e "\n${GREEN}✓ All services reset successfully!${NC}"
else
echo -e "\n${RED}✗ Some services failed to reset:${NC}"
for service in "${failed_services[@]}"; do
echo -e " ${RED}- $service${NC}"
done
return 1
fi
}
cleanup_migration_jobs() {
echo -e "${BLUE}Cleaning up migration jobs...${NC}"
kubectl delete jobs -l app.kubernetes.io/component=migration -n "$NAMESPACE" 2>/dev/null || true
}
main() {
local action=""
local target_service=""
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-a|--all)
action="all"
shift
;;
-s|--service)
action="service"
target_service="$2"
shift 2
;;
-l|--list)
list_services
exit 0
;;
-y|--yes)
SKIP_CONFIRM="true"
shift
;;
-h|--help)
show_usage
exit 0
;;
*)
if [[ -z "$action" && -z "$target_service" ]]; then
action="service"
target_service="$1"
fi
shift
;;
esac
done
print_banner
# Validate arguments
if [[ -z "$action" ]]; then
echo -e "${RED}Error: No action specified${NC}"
show_usage
exit 1
fi
if [[ "$action" == "service" && -z "$target_service" ]]; then
echo -e "${RED}Error: Service name required${NC}"
show_usage
exit 1
fi
if [[ "$action" == "service" ]]; then
# Validate service name
if [[ ! " ${SERVICES[*]} " =~ " ${target_service} " ]]; then
echo -e "${RED}Error: Invalid service name: $target_service${NC}"
list_services
exit 1
fi
fi
# Execute action
case "$action" in
"all")
if confirm_action "ALL SERVICES" "Are you sure you want to reset ALL databases? This will affect"; then
enable_force_recreate
trap disable_force_recreate EXIT
reset_all_services
fi
;;
"service")
if confirm_action "$target_service"; then
enable_force_recreate
trap disable_force_recreate EXIT
reset_service "$target_service"
fi
;;
esac
}
# Run main function
main "$@"