Fix DB issue 2s

This commit is contained in:
Urtzi Alfaro
2025-09-30 21:58:10 +02:00
parent 147893015e
commit 7cc4b957a5
77 changed files with 4385 additions and 1211 deletions

View File

@@ -1,231 +0,0 @@
#!/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 "$@"

View File

@@ -1,235 +0,0 @@
#!/bin/bash
# Development Workflow Script for Bakery-IA
#
# This script provides common development workflows with database management
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
show_usage() {
echo "Development Workflow Script for Bakery-IA"
echo ""
echo "Usage: $0 [COMMAND] [OPTIONS]"
echo ""
echo "Commands:"
echo " start Start development environment"
echo " reset Reset database(s) and restart"
echo " clean Clean start (drop all data)"
echo " migrate Run migrations only"
echo " logs Show service logs"
echo " status Show deployment status"
echo ""
echo "Options:"
echo " --service NAME Target specific service (default: all)"
echo " --profile NAME Use specific Skaffold profile (minimal, full, dev)"
echo " --clean-slate Force recreate all tables"
echo " --help Show this help"
echo ""
echo "Examples:"
echo " $0 start --profile minimal # Start with minimal services"
echo " $0 reset --service auth # Reset auth service only"
echo " $0 clean --profile dev # Clean start with dev profile"
}
start_development() {
local profile="${1:-dev}"
local clean_slate="${2:-false}"
echo -e "${BLUE}Starting development environment with profile: $profile${NC}"
if [[ "$clean_slate" == "true" ]]; then
echo -e "${YELLOW}Enabling clean slate mode...${NC}"
kubectl create configmap development-config --dry-run=client -o yaml \
--from-literal=DB_FORCE_RECREATE=true \
--from-literal=DEVELOPMENT_MODE=true \
--from-literal=DEBUG_LOGGING=true | \
kubectl apply -f -
fi
# Start with Skaffold
echo -e "${BLUE}Starting Skaffold with profile: $profile${NC}"
skaffold dev --profile="$profile" --port-forward
}
reset_service_and_restart() {
local service="$1"
local profile="${2:-dev}"
echo -e "${BLUE}Resetting service: $service${NC}"
# Reset the database
./scripts/dev-reset-database.sh --service "$service" --yes
# Restart the deployment
kubectl rollout restart deployment "${service}-service" -n bakery-ia 2>/dev/null || \
kubectl rollout restart deployment "$service" -n bakery-ia 2>/dev/null || true
echo -e "${GREEN}Service $service reset and restarted${NC}"
}
clean_start() {
local profile="${1:-dev}"
echo -e "${YELLOW}Performing clean start...${NC}"
# Stop existing Skaffold process
pkill -f "skaffold" || true
# Clean up all deployments
kubectl delete jobs -l app.kubernetes.io/component=migration -n bakery-ia 2>/dev/null || true
# Wait a moment
sleep 2
# Start with clean slate
start_development "$profile" "true"
}
run_migrations() {
local service="$1"
if [[ -n "$service" ]]; then
echo -e "${BLUE}Running migration for service: $service${NC}"
kubectl delete job "${service}-migration" -n bakery-ia 2>/dev/null || true
kubectl apply -f "infrastructure/kubernetes/base/migrations/${service}-migration-job.yaml"
kubectl wait --for=condition=complete job/"${service}-migration" -n bakery-ia --timeout=300s
else
echo -e "${BLUE}Running migrations for all services${NC}"
kubectl delete jobs -l app.kubernetes.io/component=migration -n bakery-ia 2>/dev/null || true
kubectl apply -f infrastructure/kubernetes/base/migrations/
# Wait for all migration jobs
for job in $(kubectl get jobs -l app.kubernetes.io/component=migration -n bakery-ia -o name); do
kubectl wait --for=condition=complete "$job" -n bakery-ia --timeout=300s
done
fi
echo -e "${GREEN}Migrations completed${NC}"
}
show_logs() {
local service="$1"
if [[ -n "$service" ]]; then
echo -e "${BLUE}Showing logs for service: $service${NC}"
kubectl logs -l app.kubernetes.io/name="${service}" -n bakery-ia --tail=100 -f
else
echo -e "${BLUE}Available services for logs:${NC}"
kubectl get deployments -n bakery-ia -o custom-columns="NAME:.metadata.name"
fi
}
show_status() {
echo -e "${BLUE}Deployment Status:${NC}"
echo ""
echo -e "${YELLOW}Pods:${NC}"
kubectl get pods -n bakery-ia
echo ""
echo -e "${YELLOW}Services:${NC}"
kubectl get services -n bakery-ia
echo ""
echo -e "${YELLOW}Jobs:${NC}"
kubectl get jobs -n bakery-ia
echo ""
echo -e "${YELLOW}ConfigMaps:${NC}"
kubectl get configmaps -n bakery-ia
}
main() {
local command=""
local service=""
local profile="dev"
local clean_slate="false"
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
start|reset|clean|migrate|logs|status)
command="$1"
shift
;;
--service)
service="$2"
shift 2
;;
--profile)
profile="$2"
shift 2
;;
--clean-slate)
clean_slate="true"
shift
;;
--help)
show_usage
exit 0
;;
*)
if [[ -z "$command" ]]; then
command="$1"
fi
shift
;;
esac
done
if [[ -z "$command" ]]; then
show_usage
exit 1
fi
case "$command" in
"start")
start_development "$profile" "$clean_slate"
;;
"reset")
if [[ -n "$service" ]]; then
reset_service_and_restart "$service" "$profile"
else
echo -e "${RED}Error: --service required for reset command${NC}"
exit 1
fi
;;
"clean")
clean_start "$profile"
;;
"migrate")
run_migrations "$service"
;;
"logs")
show_logs "$service"
;;
"status")
show_status
;;
*)
echo -e "${RED}Error: Unknown command: $command${NC}"
show_usage
exit 1
;;
esac
}
# Check if kubectl and skaffold are available
if ! command -v kubectl &> /dev/null; then
echo -e "${RED}Error: kubectl is not installed or not in PATH${NC}"
exit 1
fi
if ! command -v skaffold &> /dev/null; then
echo -e "${RED}Error: skaffold is not installed or not in PATH${NC}"
exit 1
fi
# Run main function
main "$@"