Skip to content

Commit adc3ed9

Browse files
authored
Fix negative value from PostgreSQL Count functions (#1186)
1 parent 595c0e5 commit adc3ed9

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

storage/data/sql.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,9 +1379,11 @@ func (d *SQLDatabase) CountUsers(ctx context.Context) (int, error) {
13791379
Scan(&tableStatus).Error
13801380
count = tableStatus.Rows
13811381
case Postgres:
1382+
var pgCount float64
13821383
err = d.gormDB.WithContext(ctx).
13831384
Raw(fmt.Sprintf("SELECT reltuples AS estimate FROM pg_class where relname = '%s'", d.UsersTable())).
1384-
Scan(&count).Error
1385+
Scan(&pgCount).Error
1386+
count = max(int64(pgCount), 0)
13851387
default:
13861388
err = d.gormDB.WithContext(ctx).Table(d.UsersTable()).Count(&count).Error
13871389
}
@@ -1403,9 +1405,11 @@ func (d *SQLDatabase) CountItems(ctx context.Context) (int, error) {
14031405
Scan(&tableStatus).Error
14041406
count = tableStatus.Rows
14051407
case Postgres:
1408+
var pgCount float64
14061409
err = d.gormDB.WithContext(ctx).
14071410
Raw(fmt.Sprintf("SELECT reltuples AS estimate FROM pg_class where relname = '%s'", d.ItemsTable())).
1408-
Scan(&count).Error
1411+
Scan(&pgCount).Error
1412+
count = max(int64(pgCount), 0)
14091413
default:
14101414
err = d.gormDB.WithContext(ctx).Table(d.ItemsTable()).Count(&count).Error
14111415
}
@@ -1431,7 +1435,7 @@ func (d *SQLDatabase) CountFeedback(ctx context.Context) (int, error) {
14311435
err = d.gormDB.WithContext(ctx).
14321436
Raw(fmt.Sprintf("SELECT reltuples AS estimate FROM pg_class where relname = '%s'", d.FeedbackTable())).
14331437
Scan(&pgCount).Error
1434-
count = int64(pgCount)
1438+
count = max(int64(pgCount), 0)
14351439
default:
14361440
err = d.gormDB.WithContext(ctx).Table(d.FeedbackTable()).Count(&count).Error
14371441
}

0 commit comments

Comments
 (0)