128 lines
3.3 KiB
Bash
Executable File
128 lines
3.3 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# ============================================
|
|
# Cleanup Orphaned Mission Calendars Script
|
|
# ============================================
|
|
# This script helps clean up calendars from deleted missions
|
|
#
|
|
# Usage:
|
|
# ./scripts/cleanup-orphaned-mission-calendars.sh [--dry-run] [--execute]
|
|
#
|
|
# Options:
|
|
# --dry-run : Show what would be deleted (default)
|
|
# --execute : Actually delete the orphaned calendars
|
|
|
|
set -e
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
DRY_RUN=true
|
|
EXECUTE=false
|
|
|
|
# Parse arguments
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
--execute)
|
|
EXECUTE=true
|
|
DRY_RUN=false
|
|
shift
|
|
;;
|
|
--dry-run)
|
|
DRY_RUN=true
|
|
EXECUTE=false
|
|
shift
|
|
;;
|
|
*)
|
|
echo "Unknown option: $1"
|
|
echo "Usage: $0 [--dry-run] [--execute]"
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
echo -e "${YELLOW}========================================${NC}"
|
|
echo -e "${YELLOW}Cleanup Orphaned Mission Calendars${NC}"
|
|
echo -e "${YELLOW}========================================${NC}"
|
|
echo ""
|
|
|
|
# Check if docker-compose is available
|
|
if ! command -v docker-compose &> /dev/null; then
|
|
echo -e "${RED}Error: docker-compose not found${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
# Check if .env.production exists
|
|
if [ ! -f ".env.production" ]; then
|
|
echo -e "${RED}Error: .env.production file not found${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
if [ "$DRY_RUN" = true ]; then
|
|
echo -e "${YELLOW}Mode: DRY RUN (no changes will be made)${NC}"
|
|
echo ""
|
|
echo "Reviewing orphaned calendars..."
|
|
echo ""
|
|
|
|
docker-compose -f docker-compose.prod.yml --env-file .env.production exec -T db psql -U neah_user -d calendar_db <<EOF
|
|
SELECT
|
|
c.id,
|
|
c.name,
|
|
c."missionId",
|
|
u.email as user_email,
|
|
COUNT(e.id) as event_count
|
|
FROM "Calendar" c
|
|
LEFT JOIN "User" u ON c."userId" = u.id
|
|
LEFT JOIN "Event" e ON e."calendarId" = c.id
|
|
WHERE c.name LIKE 'Mission: %'
|
|
AND c."missionId" IS NULL
|
|
GROUP BY c.id, c.name, c."missionId", c."userId", u.email
|
|
ORDER BY event_count DESC, c.name;
|
|
EOF
|
|
|
|
echo ""
|
|
echo -e "${YELLOW}To actually delete these calendars, run:${NC}"
|
|
echo -e "${GREEN} $0 --execute${NC}"
|
|
|
|
elif [ "$EXECUTE" = true ]; then
|
|
echo -e "${RED}Mode: EXECUTE (calendars will be deleted!)${NC}"
|
|
echo ""
|
|
read -p "Are you sure you want to delete orphaned mission calendars? (yes/no): " confirm
|
|
|
|
if [ "$confirm" != "yes" ]; then
|
|
echo "Cancelled."
|
|
exit 0
|
|
fi
|
|
|
|
echo ""
|
|
echo "Deleting orphaned calendars..."
|
|
|
|
docker-compose -f docker-compose.prod.yml --env-file .env.production exec -T db psql -U neah_user -d calendar_db <<EOF
|
|
-- Delete events first (they will be cascade deleted anyway, but this is explicit)
|
|
DELETE FROM "Event"
|
|
WHERE "calendarId" IN (
|
|
SELECT id FROM "Calendar"
|
|
WHERE name LIKE 'Mission: %'
|
|
AND "missionId" IS NULL
|
|
);
|
|
|
|
-- Delete orphaned calendars
|
|
DELETE FROM "Calendar"
|
|
WHERE name LIKE 'Mission: %'
|
|
AND "missionId" IS NULL;
|
|
|
|
-- Show remaining orphaned calendars (should be 0)
|
|
SELECT
|
|
COUNT(*) as remaining_orphaned_calendars
|
|
FROM "Calendar"
|
|
WHERE name LIKE 'Mission: %'
|
|
AND "missionId" IS NULL;
|
|
EOF
|
|
|
|
echo ""
|
|
echo -e "${GREEN}Cleanup completed!${NC}"
|
|
fi
|