fix: sync ID counters after import to prevent collisions

When importing issues with explicit high IDs (e.g., bd-100), the
issue_counters table wasn't being updated. This caused the next
auto-generated issue to collide with existing IDs (bd-4 instead of bd-101).

Changes:
- Add SyncAllCounters() to scan all issues and update counters atomically
- Add SyncCounterForPrefix() for granular counter synchronization
- Call SyncAllCounters() in import command after creating issues
- Add comprehensive tests for counter sync functionality
- Update TestImportCounterSyncAfterHighID to verify fix

The fix uses a single efficient SQL query to prevent ID collisions
with subsequently auto-generated issues.
This commit is contained in:
v4rgas
2025-10-13 19:56:34 -03:00
committed by Steve Yegge
parent 187d291647
commit 73f5acadfa
4 changed files with 95 additions and 0 deletions

View File

@@ -107,6 +107,22 @@ func (s *SQLiteStorage) getNextIDForPrefix(ctx context.Context, prefix string) (
return nextID, nil
}
// SyncCounterForPrefix synchronizes the counter to be at least the given value
// This is used after importing issues with explicit IDs to prevent ID collisions
// with subsequently auto-generated IDs
func (s *SQLiteStorage) SyncCounterForPrefix(ctx context.Context, prefix string, minValue int) error {
_, err := s.db.ExecContext(ctx, `
INSERT INTO issue_counters (prefix, last_id)
VALUES (?, ?)
ON CONFLICT(prefix) DO UPDATE SET
last_id = MAX(last_id, ?)
`, prefix, minValue, minValue)
if err != nil {
return fmt.Errorf("failed to sync counter for prefix %s: %w", prefix, err)
}
return nil
}
// SyncAllCounters synchronizes all ID counters based on existing issues in the database
// This scans all issues and updates counters to prevent ID collisions with auto-generated IDs
func (s *SQLiteStorage) SyncAllCounters(ctx context.Context) error {