This commit is contained in:
1 parent
c7c7fd3dc9
commit
fd9cc185ce
6 files changed
+113
-56
No files matched your search
@@ -135,12 +135,22 @@ func TableHasRows(ctx context.Context, dbConn *sqlx.DB, table string) (bool, err
|
||||
if err := ValidateTableName(table); err != nil {
|
||||
return false, err
|
||||
}
|
||||
// Avoid hanging on locked tables; apply a short timeout.
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, 15*time.Second)
|
||||
defer cancel()
|
||||
query := fmt.Sprintf(`SELECT 1 FROM %s LIMIT 1`, table)
|
||||
var exists int
|
||||
if err := getLog(ctx, dbConn, &exists, query); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return false, nil
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
|
||||
Reference in new issue
Block a user