Rebuilding¶
The grite rebuild command discards local projections and replays events to rebuild the materialized view.
When to Rebuild¶
Rebuild when:
grite doctorrecommends it- Database appears corrupted
- Query performance degrades
- After database crashes
Basic Rebuild¶
Standard rebuild from local store events:
This:
- Clears current projections
- Reads all events from local store
- Recomputes projections
- Compacts the database
Snapshot-Based Rebuild¶
Faster rebuild using the latest snapshot:
This:
- Finds the latest snapshot ref
- Loads events from snapshot
- Rebuilds projections
- Much faster for large repositories
When to Use Snapshots¶
Use --from-snapshot when:
- Repository has many events (>10,000)
- Standard rebuild is slow
- Snapshots are available
What Gets Rebuilt¶
| Rebuilt | Not Affected |
|---|---|
| Issue projections | WAL (git refs) |
| Issue indexes | Actor config |
| Label indexes | Signing keys |
| Metadata | Repository config |
JSON Output¶
{
"schema_version": 1,
"ok": true,
"data": {
"wal_head": "abc123...",
"event_count": 1234,
"from_snapshot": null
}
}
With snapshot:
{
"schema_version": 1,
"ok": true,
"data": {
"wal_head": "abc123...",
"event_count": 1234,
"from_snapshot": "refs/grite/snapshots/1700000000000"
}
}
Performance¶
Time Estimates¶
| Events | Standard Rebuild | Snapshot Rebuild |
|---|---|---|
| 1,000 | ~1 second | ~1 second |
| 10,000 | ~10 seconds | ~2 seconds |
| 100,000 | ~2 minutes | ~10 seconds |
Actual times depend on hardware and event complexity.
Optimizing Rebuild Time¶
- Create snapshots regularly:
grite snapshot - Use snapshot rebuild:
grite rebuild --from-snapshot - Clean old snapshots:
grite snapshot gc
Database Compaction¶
Rebuild automatically compacts the database because it rewrites all data from scratch. This can:
- Reduce database size
- Improve query performance
- Remove fragmentation
Checking Rebuild Status¶
Before rebuild:
{
"schema_version": 1,
"ok": true,
"data": {
"path": ".git/grite/actors/.../sled",
"size_bytes": 5242880,
"event_count": 1234,
"issue_count": 42,
"last_rebuild_ts": 1699900000000,
"events_since_rebuild": 500,
"days_since_rebuild": 7,
"rebuild_recommended": true
}
}
Manual Database Reset¶
For a complete reset (not usually needed):
# Stop daemon first
grite daemon stop
# Delete sled database
rm -rf .git/grite/actors/<actor_id>/sled
# Rebuild from scratch
grite rebuild
Or with snapshot:
Rebuild After Sync Issues¶
If sync brought in corrupted data:
# Verify WAL integrity first
grite db check --json
# If WAL is good, rebuild
grite rebuild
# If WAL is bad, resync from remote
grite sync --pull
grite rebuild
Automation¶
Scheduled Rebuild¶
#!/bin/bash
# weekly-rebuild.sh
# Check if rebuild needed
stats=$(grite db stats --json)
recommended=$(echo "$stats" | jq -r '.data.rebuild_recommended')
if [ "$recommended" = "true" ]; then
echo "Rebuild recommended, running..."
grite rebuild --from-snapshot
else
echo "Rebuild not needed"
fi
CI Integration¶
- name: Rebuild if needed
run: |
if grite db stats --json | jq -e '.data.rebuild_recommended'; then
grite rebuild --from-snapshot
fi
Best Practices¶
Use Snapshots¶
For large repositories, always use snapshots:
Stop Daemon First¶
For cleanest rebuild:
Verify After Rebuild¶
Troubleshooting¶
"Rebuild failed"¶
Check for:
- Disk space
- Corrupted WAL
- File permissions
"Snapshot not found"¶
No snapshots available:
"Rebuild takes too long"¶
Create a snapshot first:
Next Steps¶
- Snapshots - Manage snapshots
- Health Checks - Verify rebuild success
- Troubleshooting - Fix issues