cms.c2sgmbh/scripts/sync-schema.sh
Martin Porwoll 9e433315e5 feat: add automatic schema sync to deployment workflow
- Add drizzle.production.config.ts for schema synchronization
- Add scripts/sync-schema.sh for manual schema sync
- Update deploy-production.sh to run drizzle-kit push after migrations
- Document schema sync workflow in DEPLOYMENT_STRATEGY.md
- Update CLAUDE.md with schema sync commands

This prevents schema drift between DEV and PROD by automatically
syncing the database schema (especially payload_locked_documents_rels)
during deployment.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 20:17:47 +00:00

100 lines
2.5 KiB
Bash
Executable file

#!/bin/bash
# =============================================================================
# Schema Sync Script
# =============================================================================
# Synchronizes the database schema with the Payload-generated schema.
# Use this after adding new Collections/Globals to ensure the database
# has all required tables and columns.
#
# Usage:
# ./scripts/sync-schema.sh # Interactive mode
# ./scripts/sync-schema.sh --force # Non-interactive (for CI/automation)
# ./scripts/sync-schema.sh --dry-run # Show what would change
# =============================================================================
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Parse arguments
FORCE=false
DRY_RUN=false
for arg in "$@"; do
case $arg in
--force|-f)
FORCE=true
shift
;;
--dry-run|-n)
DRY_RUN=true
shift
;;
-h|--help)
head -15 "$0" | tail -12
exit 0
;;
esac
done
# Determine environment
if [ -f "/home/payload/payload-cms/.env" ]; then
source /home/payload/payload-cms/.env 2>/dev/null || true
fi
PROJECT_DIR="${PROJECT_DIR:-/home/payload/payload-cms}"
cd "$PROJECT_DIR"
echo -e "${BLUE}=============================================="
echo -e " Database Schema Sync"
echo -e "==============================================${NC}"
echo ""
# Check if schema file exists
if [ ! -f "src/payload-generated-schema.ts" ]; then
echo -e "${YELLOW}Generating schema file...${NC}"
pnpm payload generate:db-schema 2>/dev/null || true
fi
if [ ! -f "src/payload-generated-schema.ts" ]; then
echo -e "${RED}Error: Could not find or generate schema file${NC}"
exit 1
fi
# Dry run mode
if [ "$DRY_RUN" = true ]; then
echo -e "${YELLOW}[DRY RUN] Showing what would change:${NC}"
echo ""
pnpm exec drizzle-kit push --config=drizzle.production.config.ts --dry-run 2>&1 || true
exit 0
fi
# Interactive confirmation
if [ "$FORCE" != true ]; then
echo -e "${YELLOW}This will sync the database schema with the code.${NC}"
echo "Database: $DATABASE_URI"
echo ""
read -p "Continue? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Aborted."
exit 0
fi
fi
# Run schema sync
echo ""
echo -e "${BLUE}Running drizzle-kit push...${NC}"
pnpm exec drizzle-kit push --config=drizzle.production.config.ts --force
echo ""
echo -e "${GREEN}Schema sync complete!${NC}"
echo ""
echo "Next steps:"
echo " 1. Restart Payload: pm2 restart payload"
echo " 2. Test the admin panel"