mirror of
https://github.com/bitbaum/orangecat.git
synced 2026-12-05 07:08:24 +01:00
- Fix currency defaults (SATS → CHF) across forms, configs, and API routes - Remove @ts-nocheck from socialService.ts and improve type safety - Replace console.log statements with proper logger utility in production code - Additional type safety improvements and code quality enhancements - Documentation updates for improved maintainability All changes follow DRY, SSOT, and separation of concerns principles.
8.0 KiB
8.0 KiB
🧪 Migration Testing Environment
Safe migration testing for safer deployments!
Comprehensive testing environment for Supabase migrations with isolated databases, data integrity validation, and performance analysis.
Important: OrangeCat uses remote-only Supabase. Do not run
supabase startor any local DB. This document describes a legacy local testing approach and is kept for historical context only.
🚀 Quick Start (Legacy)
1. Setup Testing Environment
node scripts/migration-testing-env.js setup
2. Start Supabase Local (do not use in OrangeCat)
npx supabase start
3. Test a Migration (legacy flow)
node scripts/migration-testing-env.js supabase/migrations/20251113000001_timeline_social_features.sql
4. List Available Migrations
node scripts/migration-testing-env.js list
✅ What It Tests
Database Safety
- Isolated Testing - Tests run in isolated database environment
- Automatic Backup - Creates backup before testing
- Auto Cleanup - Restores original state after testing
- No Production Impact - Zero risk to production data
Migration Validation
- SQL Syntax - Validates using the SQL validator
- Schema Changes - Tracks added/removed tables and columns
- Data Integrity - Validates referential integrity
- Performance Impact - Analyzes query performance changes
Production Readiness
- Rollback Safety - Tests rollback procedures
- RLS Policies - Validates security policies
- Index Performance - Checks query optimization
- Function Dependencies - Validates function relationships
📊 Example Test Output
🧪 MIGRATION TESTING ENVIRONMENT 🧪
Safe Testing for Safer Deployments
Testing migration: 20251113000001_timeline_social_features.sql
=== Creating Database Backup ===
✅ Database backup created: pre_migration_backup_1733328000000.sql
=== Applying Test Migration: 20251113000001_timeline_social_features.sql ===
✅ Migration applied successfully: 20251113000001_timeline_social_features.sql
=== Data Integrity Validation ===
✅ Timeline Events Exist: 42
✅ Users Table Exists: 15
✅ No Orphaned Records: 0
=== Performance Analysis ===
✅ Timeline Feed Query: 45.2ms (Good)
=== Cleanup ===
✅ Test environment cleaned up
📊 MIGRATION TEST RESULTS:
Migration: 20251113000001_timeline_social_features.sql
Duration: 12.3s
✅ Passed: 8
❌ Failed: 0
⚠️ Warnings: 2
📈 Success Rate: 80.0%
🎉 MIGRATION TEST PASSED!
Migration is safe for production deployment.
🏗️ Architecture
Test Environment Structure
migration-testing/
├── backups/ # Database backups
├── reports/ # Test reports
└── .gitignore # Ignore test artifacts
Testing Workflow
- Validate SQL - Run syntax validation
- Create Backup - Backup current database state
- Apply Migration - Test migration in isolation
- Verify Integrity - Check data consistency
- Analyze Performance - Measure query performance
- Restore State - Clean up test environment
- Generate Report - Comprehensive test results
🎯 Key Features
Automatic Backup & Restore
# Creates timestamped backup
pre_migration_backup_1733328000000.sql
# Automatically restores after testing
# No manual cleanup needed
Data Integrity Checks
- Orphaned records detection
- Foreign key validation
- Table existence verification
- Row count consistency
Performance Analysis
- Query execution time measurement
- EXPLAIN plan analysis
- Index effectiveness validation
- Bottleneck identification
Schema Comparison
- Added/removed tables tracking
- Column changes detection
- Index modifications
- Constraint validation
🔧 Integration
Pre-deployment Checklist
# 1. Validate SQL syntax
node scripts/validate-migration-sql.js migration.sql
# 2. Test in isolated environment
node scripts/migration-testing-env.js migration.sql
# 3. Verify in staging (if available)
# Deploy to staging environment
# 4. Production deployment
npx supabase db push
CI/CD Pipeline Integration
# .github/workflows/migration-testing.yml
name: Migration Testing
on: [pull_request]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- name: Setup Supabase
run: npm install -g supabase
- name: Setup test database
run: |
createdb -h localhost -U postgres test_db
psql -h localhost -U postgres -d test_db -f init.sql
- name: Run migration tests
run: node scripts/migration-testing-env.js supabase/migrations/*.sql
🚨 Error Handling
Common Issues & Solutions
Database Not Running
⚠️ Supabase local database is not running
ℹ️ Start it with: npx supabase db start
Solution:
npx supabase start # Start all services
# OR
npx supabase db start # Start database only
Migration Validation Failed
❌ Migration validation failed. Not applying to test database.
Solution:
- Fix SQL syntax errors
- Check function parameter orders
- Validate CREATE INDEX expressions
- Review RLS policies
Performance Issues Detected
❌ Timeline Feed Query: 1250.5ms (Slow - needs optimization)
Solution:
- Add missing indexes
- Optimize query structure
- Review table relationships
- Consider query caching
📋 Testing Checklist
Before Migration Testing
- Supabase local is running
- Test environment is set up
- Migration file exists and is readable
- SQL validation passes
- Backup directory has sufficient space
During Migration Testing
- Backup creation succeeds
- Migration applies without errors
- Data integrity checks pass
- Performance is acceptable
- Schema changes are expected
After Migration Testing
- Database is restored to original state
- Test artifacts are cleaned up
- Test report is generated
- Results are reviewed by team
🎯 Best Practices
Migration Development
- Write migrations incrementally - Small, focused changes
- Test each migration - Don't batch untested migrations
- Validate before commit - Catch issues early
- Document changes - Explain what and why
Testing Strategy
- Test in isolation - Each migration independently
- Verify rollback - Ensure safe rollback procedures
- Check performance - Monitor query performance impact
- Validate security - Confirm RLS policies work
Deployment Safety
- Backup production - Always before deploying
- Deploy during low traffic - Minimize user impact
- Monitor after deployment - Watch for issues
- Have rollback plan - Be prepared to revert
📁 Files
scripts/migration-testing-env.js- Main testing scriptscripts/validate-migration-sql.js- SQL validationmigration-testing/backups/- Database backupsmigration-testing/reports/- Test reportssupabase/migrations/*.sql- Migration files
🚀 Advanced Usage
Custom Test Scenarios
// In migration-testing-env.js
function runCustomTests(migrationPath) {
// Add custom validation logic
// Test specific business rules
// Validate complex relationships
}
Performance Baselines
// Define acceptable performance thresholds
const PERFORMANCE_THRESHOLDS = {
timeline_feed: 100, // ms
user_search: 50, // ms
data_aggregation: 200, // ms
};
Integration Testing
# Test migration with application code
npm run test:e2e -- --migration=20251113000001_timeline_social_features.sql
Remember: Test migrations thoroughly to prevent production disasters! 🛡️