further improve log noise
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -3,6 +3,7 @@ package db
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sort"
|
||||
@@ -56,7 +57,13 @@ func EnsureColumns(ctx context.Context, dbConn *sqlx.DB, tableName string, colum
|
||||
func execLog(ctx context.Context, dbConn *sqlx.DB, query string, args ...interface{}) (sql.Result, error) {
|
||||
res, err := dbConn.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
slog.Warn("db exec failed", "query", strings.TrimSpace(query), "error", err)
|
||||
q := strings.TrimSpace(query)
|
||||
msg := strings.ToLower(err.Error())
|
||||
if strings.Contains(msg, "duplicate column name") || strings.Contains(msg, "already exists") {
|
||||
slog.Debug("db exec skipped (already exists)", "query", q, "error", err)
|
||||
} else {
|
||||
slog.Warn("db exec failed", "query", q, "error", err)
|
||||
}
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
@@ -86,6 +93,10 @@ func AddColumnIfMissing(ctx context.Context, dbConn *sqlx.DB, tableName string,
|
||||
if _, err := SafeTableName(tableName); err != nil {
|
||||
return err
|
||||
}
|
||||
// Skip ALTER if the column already exists to avoid noisy duplicate errors.
|
||||
if exists, err := ColumnExists(ctx, dbConn, tableName, column.Name); err == nil && exists {
|
||||
return nil
|
||||
}
|
||||
query := fmt.Sprintf(`ALTER TABLE %s ADD COLUMN "%s" %s`, tableName, column.Name, column.Type)
|
||||
if _, err := execLog(ctx, dbConn, query); err != nil {
|
||||
errText := strings.ToLower(err.Error())
|
||||
|
||||
Reference in New Issue
Block a user