Skip to content

Commit 585fc75

Browse files
appleboyclaude
andauthored
fix(store): handle Count errors in seedData to fail fast on DB issues (#90)
The Count() calls for users and OAuth clients in seedData() previously ignored errors, which could cause incorrect seeding behavior if the database connection fails (e.g., creating duplicate admin users). Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e3bd48e commit 585fc75

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

internal/store/sqlite.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ func generateRandomPassword(length int) (string, error) {
139139
func (s *Store) seedData(ctx context.Context, cfg *config.Config) error {
140140
// Create default user if not exists
141141
var userCount int64
142-
s.db.WithContext(ctx).Model(&models.User{}).Count(&userCount)
142+
if err := s.db.WithContext(ctx).Model(&models.User{}).Count(&userCount).Error; err != nil {
143+
return fmt.Errorf("failed to count users: %w", err)
144+
}
143145
var userID string
144146
if userCount == 0 {
145147
userID = uuid.New().String()
@@ -182,7 +184,12 @@ func (s *Store) seedData(ctx context.Context, cfg *config.Config) error {
182184

183185
// Create default OAuth client if not exists
184186
var clientCount int64
185-
s.db.WithContext(ctx).Model(&models.OAuthApplication{}).Count(&clientCount)
187+
if err := s.db.WithContext(ctx).
188+
Model(&models.OAuthApplication{}).
189+
Count(&clientCount).
190+
Error; err != nil {
191+
return fmt.Errorf("failed to count OAuth clients: %w", err)
192+
}
186193
if clientCount == 0 {
187194
// If admin user was not just created, look up the existing admin user ID
188195
if userID == "" {

0 commit comments

Comments
 (0)