major-domo-database/scripts/sync_from_prod.sh
Cal Corum 54a1a407d0 CLAUDE: Season batting stats table and selective update system
Major database enhancement implementing fast-querying season batting stats:

Database Schema:
- Created seasonbattingstats table with composite primary key (player_id, season)
- All batting stats (counting + calculated): pa, ab, avg, obp, slg, ops, woba, etc.
- Proper foreign key constraints and performance indexes
- Production-ready SQL creation script included

Selective Update System:
- update_season_batting_stats() function with PostgreSQL upsert logic
- Triggers on game PATCH operations to update affected player stats
- Recalculates complete season stats from stratplay data
- Efficient updates of only players who participated in modified games

API Enhancements:
- Enhanced SeasonBattingStats.get_top_hitters() with full filtering support
- New /api/v3/views/season-stats/batting/refresh endpoint for season rebuilds
- Updated views endpoint to use centralized get_top_hitters() method
- Support for team, player, min PA, and pagination filtering

Infrastructure:
- Production database sync Docker service with SSH automation
- Comprehensive error handling and logging throughout
- Fixed Peewee model to match actual table structure (no auto-id)
- Updated CLAUDE.md with dev server info and sync commands

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-23 22:18:27 -05:00

58 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
set -e
# Error handling function
error_exit() {
echo "ERROR: $1" >&2
exit 1
}
# Cleanup function
cleanup() {
if [ -f "/tmp/prod_dump.sql" ]; then
echo "Cleaning up temporary files..."
rm -f /tmp/prod_dump.sql
fi
}
# Set trap for cleanup on exit
trap cleanup EXIT
echo "Starting production database sync..."
# Validate required environment variables
[ -z "$SBA_DB_USER" ] && error_exit "SBA_DB_USER environment variable not set"
[ -z "$SBA_DATABASE" ] && error_exit "SBA_DATABASE environment variable not set"
echo "Production server: akamai"
echo "Target database: ${SBA_DATABASE}"
echo "Target user: ${SBA_DB_USER}"
# Install necessary packages
echo "Installing required packages..."
apk add --no-cache openssh-client postgresql-client || error_exit "Failed to install required packages"
# Test SSH connection first
echo "Testing SSH connection to production server..."
ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 akamai "echo 'SSH connection successful'" || error_exit "Failed to connect to production server"
# Test local database connection
echo "Testing local database connection..."
pg_isready -h sba_postgres -U ${SBA_DB_USER} -d ${SBA_DATABASE} || error_exit "Local database is not ready"
# Create dump from production server first (safer than direct pipe)
echo "Creating dump from production server..."
ssh -o StrictHostKeyChecking=no akamai \
"cd container-data/sba-database && source .env && docker exec -e PGPASSWORD=\$SBA_DB_USER_PASSWORD sba_postgres pg_dump -U \$SBA_DB_USER -d \$SBA_DATABASE" \
> /tmp/prod_dump.sql || error_exit "Failed to create production database dump"
# Verify dump file is not empty
[ ! -s "/tmp/prod_dump.sql" ] && error_exit "Production dump file is empty"
echo "Dump created successfully ($(wc -l < /tmp/prod_dump.sql) lines)"
# Restore to local database
echo "Restoring to local database..."
PGPASSWORD=${SBA_DB_USER_PASSWORD} psql -h sba_postgres -U ${SBA_DB_USER} -d ${SBA_DATABASE} -f /tmp/prod_dump.sql || error_exit "Failed to restore database dump"
echo "Database sync completed successfully!"