Skip to content

Latest commit

 

History

History
468 lines (325 loc) · 15 KB

File metadata and controls

468 lines (325 loc) · 15 KB

Migration Documentation - JudgeFinder Platform

Last Updated: 2025-10-25 Status: Production Ready Version: 2.0


Overview

This directory contains comprehensive documentation for the JudgeFinder Platform database migrations. These migrations fix critical data loss issues and complete security hardening for the platform.

Critical Issue Being Addressed

Analytics Data Loss (90% missing):

  • Application code writes to judge_analytics_cache table
  • Database only has judge_analytics table (wrong name)
  • Result: 644 of 715 analytics silently lost
  • Only 71 analytics stored in fallback location

Security Hardening:

  • Complete RLS (Row Level Security) policy coverage
  • Fix SECURITY DEFINER vulnerabilities
  • Harden function search paths to prevent SQL injection

Documentation Structure

For Executing Migrations

Document Purpose Audience Time to Read
QUICK_REFERENCE.md Common commands, quick fixes Everyone 5 min
MIGRATION_RUNBOOK.md Step-by-step execution guide Database Admin 20 min
MIGRATION_STATUS_REPORT_v2.md Current status, risk assessment Stakeholders 15 min

For Troubleshooting

Document Purpose Audience Time to Read
MIGRATION_TROUBLESHOOTING.md Error debugging, recovery Database Admin, DevOps 30 min
QUICK_REFERENCE.md Quick SQL snippets Everyone 5 min

For Developers

Document Purpose Audience Time to Read
MIGRATION_DEVELOPER_GUIDE.md Creating new migrations, best practices Developers 45 min
MIGRATION_ACTION_PLAN.md Historical context, detailed analysis Developers 30 min

Legacy Documentation (Reference Only)

These documents were created during initial investigation and are kept for historical reference:

  • MIGRATION_DEPLOYMENT_GUIDE.md - Earlier deployment guide (superseded by RUNBOOK)
  • MIGRATION_QUICK_START.md - Initial quick start (superseded by QUICK_REFERENCE)
  • MIGRATION_FIX_REPORT.md - Initial issue analysis (superseded by STATUS_REPORT)

Quick Start

For First-Time Migration Execution

  1. Read the Executive Summary (5 minutes)

  2. Check Prerequisites (5 minutes)

    • Review QUICK_REFERENCE.md Pre-Migration section
    • Verify you have database access
    • Confirm helper functions exist
  3. Execute Migrations (30-50 minutes)

    • Follow MIGRATION_RUNBOOK.md step-by-step
    • Start with Phase 1: Analytics Cache Fix
    • Continue through all 7 phases
    • Run verification after each phase
  4. Verify Success (10 minutes)

    • Run all verification queries from RUNBOOK
    • Check application health
    • Monitor error logs
  5. Post-Migration (1-2 hours)

    • Regenerate missing analytics (644 entries)
    • Monitor performance metrics
    • Send completion notification

For Troubleshooting Issues

  1. Identify the Error (2 minutes)

    • Copy full error message
    • Note which migration phase failed
  2. Check Common Issues (5 minutes)

  3. Detailed Debugging (15-30 minutes)

  4. Escalate if Needed (immediately)

    • Contact database admin
    • Provide context from troubleshooting guide
    • Consider rollback if critical

For Creating New Migrations

  1. Review Best Practices (20 minutes)

  2. Use Migration Template (15 minutes)

    • Copy template from Developer Guide
    • Follow naming conventions
    • Add proper verification steps
  3. Test Locally (30-60 minutes)

    • Apply migration to local database
    • Run verification queries
    • Test application integration
  4. Document and Review (15 minutes)

    • Add comments explaining why migration is needed
    • Document rollback procedure
    • Request peer review

Migration Files

Current Pending Migrations

Located in: /supabase/migrations/

File Priority Purpose Duration
20251024_001_fix_judge_analytics_cache_table.sql P0-CRITICAL Fix analytics cache table name mismatch 10 min
20251120_001_create_rls_policies_part1.sql P0-CRITICAL RLS policies for core tables 5 min
20251121_001_create_rls_policies_part2.sql P0-CRITICAL RLS policies for analytics tables 5 min
20251126_001_create_rls_policies_part3.sql P0-CRITICAL RLS policies for user data tables 5 min
20251123_001_create_rls_policies_advertising.sql P0-CRITICAL RLS policies for advertising system 5 min
20251124_001_fix_security_definer_views.sql P0-CRITICAL Remove SECURITY DEFINER from views 3 min
20251125_001_add_function_search_paths.sql P1-HIGH Harden function security 3 min

Total Execution Time: ~35 minutes (plus verification time)


Key Concepts

Row Level Security (RLS)

RLS policies control which rows users can access in a table. They're essential for:

  • Multi-tenant data isolation
  • User privacy protection
  • GDPR compliance
  • Preventing unauthorized access

Example:

-- Users can only see their own data
CREATE POLICY "Users can read own data" ON app_users
  FOR SELECT
  TO authenticated
  USING (clerk_user_id = public.current_user_id());

SECURITY DEFINER Functions

Functions marked SECURITY DEFINER run with privileges of the function owner (like sudo). They can:

  • Bypass RLS policies
  • Access restricted tables
  • Perform administrative tasks

Security Concern: Without explicit search_path, attackers could create malicious tables to trick these functions.

Fix:

CREATE FUNCTION my_admin_func()
RETURNS void
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public, extensions  -- Locked down!
AS $$ ... $$;

Analytics Cache Architecture

Old (Broken):

Application Code → writes to "judge_analytics_cache"
Database → only has "judge_analytics" table
Result → 90% data loss

New (Fixed):

Application Code → writes to "judge_analytics_cache"
Database → has "judge_analytics_cache" table ✓
Result → All analytics cached properly

Execution Checklist

Pre-Migration

  • Read Executive Summary
  • Verify database access
  • Check helper functions exist
  • Notify team (24 hours advance)
  • Review rollback procedure
  • Confirm backup exists

Execution

  • Open Supabase Dashboard → SQL Editor
  • Apply Migration 1: Analytics Cache Fix
  • Verify Phase 1 success
  • Apply Migration 2: RLS Policies Part 1
  • Verify Phase 2 success
  • Apply Migration 3: RLS Policies Part 2
  • Verify Phase 3 success
  • Apply Migration 4: RLS Policies Part 3
  • Verify Phase 4 success
  • Apply Migration 5: RLS Advertising
  • Verify Phase 5 success
  • Apply Migration 6: Security Definer Views
  • Verify Phase 6 success
  • Apply Migration 7: Function Search Paths
  • Verify Phase 7 success

Post-Migration

  • Run all verification queries
  • Check application health
  • Monitor error logs (1 hour)
  • Regenerate missing analytics
  • Send completion notification
  • Update documentation

Success Criteria

Critical (Must Pass)

  • judge_analytics_cache table exists
  • ✅ 71 analytics migrated from fallback
  • ✅ All 7 migrations applied successfully
  • ✅ RLS enabled on all public tables
  • ✅ Zero RLS policy violations
  • ✅ Application responds normally

Important (Should Pass)

  • ✅ 644 missing analytics regenerated (within 24 hours)
  • ✅ Cache hit rate >80% (after warm-up)
  • ✅ Query performance maintained or improved
  • ✅ Security audit warnings cleared

Nice to Have

  • ✅ Analytics regeneration time <2 hours
  • ✅ Zero manual interventions
  • ✅ Team properly notified
  • ✅ Documentation updated

Risk Assessment

Overall Risk: LOW

Why Low Risk:

  1. All migrations are additive only (no DELETE statements)
  2. Zero downtime (non-blocking changes)
  3. Backward compatible (old code continues to work)
  4. Idempotent (safe to run multiple times)
  5. Quick rollback (<5 minutes if needed)

Specific Risks

Risk Level Mitigation
Data Loss VERY LOW No destructive operations
Downtime NONE All changes non-blocking
Performance Impact LOW Indexes improve performance
Security Regression VERY LOW Improves security posture
Application Breakage VERY LOW Backward compatible

Rollback Procedure

Quick Rollback (5 minutes)

BEGIN;

-- Drop analytics cache
DROP TABLE IF EXISTS public.judge_analytics_cache CASCADE;

-- Remove RLS policies
-- (See MIGRATION_RUNBOOK.md for complete procedure)

-- Remove migration tracking
DELETE FROM supabase_migrations.schema_migrations
WHERE version >= '20251024';

COMMIT;

When to Rollback:

  • Critical application errors
  • Data integrity issues
  • Unexpected performance degradation

When NOT to Rollback:

  • Log warnings (not errors)
  • Expected behavior changes
  • Temporary cache warm-up

Support and Resources

Documentation

External Resources

Team Contacts

  • Database Admin: [INSERT NAME/SLACK]
  • DevOps Lead: [INSERT NAME/SLACK]
  • Backend Lead: [INSERT NAME/SLACK]
  • On-call Engineer: [INSERT PAGERDUTY]

Communication Channels

  • General Questions: #engineering
  • Database Issues: #database
  • Urgent Issues: @on-call
  • Documentation: GitHub issues

Frequently Asked Questions

Q: How long will migrations take?

A: ~30-50 minutes total, including verification. Each migration takes 3-10 minutes.

Q: Will there be downtime?

A: No. All changes are additive and non-blocking. Users won't be affected.

Q: What if something goes wrong?

A: Follow the rollback procedure in MIGRATION_RUNBOOK.md. Worst case: drop the new table and revert tracking. Takes <5 minutes.

Q: Can I apply migrations in a different order?

A: No. Migrations have dependencies and must be applied in the order shown. Later migrations depend on earlier ones.

Q: What if a migration fails partway through?

A: The migration uses transactions, so either everything succeeds or nothing changes. See MIGRATION_TROUBLESHOOTING.md for recovery steps.

Q: How do I know if migrations succeeded?

A: Run the verification queries at the end of each phase. All should return expected values. Also check application health.

Q: Can I test migrations first?

A: Yes! Apply to a local database or staging environment first. See MIGRATION_DEVELOPER_GUIDE.md for testing procedures.

Q: What about the 644 missing analytics?

A: They'll be regenerated automatically after migration using npm run analytics:batch-generate. Takes 1-2 hours.


Version History

Version Date Author Changes
1.0 2025-10-24 Database Team Initial migration documentation
2.0 2025-10-25 Documentation Expert Comprehensive documentation suite

Next Steps

  1. For Database Admin:

    • Review MIGRATION_RUNBOOK.md
    • Schedule execution window
    • Notify team
    • Execute migrations following runbook
  2. For Developers:

  3. For DevOps:

    • Verify environment variables
    • Monitor deployment logs
    • Track performance metrics
    • Schedule analytics regeneration
  4. For Stakeholders:


Conclusion

These migrations fix critical data loss (90% analytics missing) and complete security hardening for the JudgeFinder platform. They are:

  • Low Risk: Additive only, backward compatible
  • Well Tested: Verified against production schema
  • Well Documented: Complete runbooks and troubleshooting guides
  • Easily Reversible: Quick rollback procedure available

The documentation suite provides everything needed for safe execution, troubleshooting, and future maintenance.

Ready to execute? Start with MIGRATION_RUNBOOK.md.

Need help? Check QUICK_REFERENCE.md or contact the database admin.


Document Maintained By: Database Team Last Review Date: 2025-10-25 Next Review: After migration completion