Overview
When adding aggregation to an existing table or fixing out-of-sync aggregates, you need to backfill existing data. This guide covers migration strategies using idempotent operations and the migrations component.Adding Aggregates to Existing Tables
When you add a TableAggregate to a table with existing data, the aggregate starts empty. You need to backfill all existing documents.Migration Process
Follow these steps to safely backfill an aggregate:1. Use Idempotent Operations
First, update your live mutations to use idempotent operations:2. Deploy Code with Idempotent Operations
Deploy your updated mutations so new writes use idempotent operations:3. Run Background Backfill
Use a paginated migration to backfill existing data:4. Switch to Regular Operations
Once the backfill completes, switch back to regular operations:5. Start Using Read Methods
Now you can safely query the aggregates:Complete Example
Here’s a full example from the leaderboard:Using Idempotent Triggers
If you’re using triggers, switch to idempotent triggers during migration:Repairing Out-of-Sync Aggregates
If mutations or dashboard edits updated tables without updating aggregates, they can get out of sync.Option 1: Clear and Rebuild (Simplest)
The simplest approach is to start over:Option 2: Diff and Patch (Advanced)
Compare the source table to the aggregate and fix differences:Migration Best Practices
- Always test migrations on development data first
- Use idempotent operations during migration period
- Monitor backfill progress - use console.log in migrateOne
- Don’t query aggregates until backfill completes
- Switch back to regular operations after backfill
- Keep migrations in version control for repeatability
Installing the Migrations Component
First, install the migrations package:Common Migration Scenarios
Scenario 1: New Aggregate on Existing Table
Scenario 2: Changing Aggregate Configuration
Scenario 3: Fixing Out-of-Sync Data
Next Steps
- Learn about Keeping Data in Sync to prevent future issues
- Understand Table Aggregates configuration
- Explore the Migrations package documentation