# Database Migrations ## License Heartbeats Table Migration ### Overview The `create_license_heartbeats_table.sql` migration creates a partitioned table for storing real-time license validation data. This table is essential for the Live Dashboard & Analytics functionality. ### Features - Monthly partitioning for efficient data management - Automatic creation of current and next month partitions - Optimized indexes for performance - Foreign key relationship with licenses table ### Running the Migration #### Option 1: Using the Python Script ```bash cd /path/to/v2_adminpanel python apply_license_heartbeats_migration.py ``` #### Option 2: Manual SQL Execution ```bash psql -h postgres -U postgres -d v2_adminpanel -f migrations/create_license_heartbeats_table.sql ``` #### Option 3: Docker Exec ```bash docker exec -it v2_adminpanel_postgres psql -U postgres -d v2_adminpanel -f /migrations/create_license_heartbeats_table.sql ``` ### Verification After running the migration, verify the table was created: ```sql -- Check if table exists SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_name = 'license_heartbeats' ); -- List all partitions SELECT tablename FROM pg_tables WHERE tablename LIKE 'license_heartbeats_%' ORDER BY tablename; ``` ### Partition Management The system automatically creates partitions as needed. To manually create future partitions: ```python from utils.partition_helper import create_future_partitions import psycopg2 conn = psycopg2.connect(...) create_future_partitions(conn, 'license_heartbeats', months_ahead=6) ``` ### Data Retention Consider implementing a data retention policy to remove old partitions: ```sql -- Drop partitions older than 3 months DROP TABLE IF EXISTS license_heartbeats_2025_03; ``` ### Troubleshooting 1. **Table already exists**: The migration is idempotent and will skip creation if the table exists. 2. **Permission denied**: Ensure the database user has CREATE privileges. 3. **Foreign key violation**: The licenses table must exist before running this migration.