add record size to hourly snapshot page
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:
@@ -24,6 +24,48 @@ type ColumnDef struct {
|
||||
Type string
|
||||
}
|
||||
|
||||
// TableRowCount returns COUNT(*) for a table.
|
||||
func TableRowCount(ctx context.Context, dbConn *sqlx.DB, table string) (int64, error) {
|
||||
if err := ValidateTableName(table); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var count int64
|
||||
query := fmt.Sprintf(`SELECT COUNT(*) FROM %s`, table)
|
||||
if err := dbConn.GetContext(ctx, &count, query); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// EnsureColumns adds the provided columns to a table if they are missing.
|
||||
func EnsureColumns(ctx context.Context, dbConn *sqlx.DB, tableName string, columns []ColumnDef) error {
|
||||
if _, err := SafeTableName(tableName); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, column := range columns {
|
||||
if err := AddColumnIfMissing(ctx, dbConn, tableName, column); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddColumnIfMissing performs a best-effort ALTER TABLE to add a column, ignoring "already exists".
|
||||
func AddColumnIfMissing(ctx context.Context, dbConn *sqlx.DB, tableName string, column ColumnDef) error {
|
||||
if _, err := SafeTableName(tableName); err != nil {
|
||||
return err
|
||||
}
|
||||
query := fmt.Sprintf(`ALTER TABLE %s ADD COLUMN "%s" %s`, tableName, column.Name, column.Type)
|
||||
if _, err := dbConn.ExecContext(ctx, query); err != nil {
|
||||
errText := strings.ToLower(err.Error())
|
||||
if strings.Contains(errText, "duplicate column") || strings.Contains(errText, "already exists") {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateTableName ensures table identifiers are safe for interpolation.
|
||||
func ValidateTableName(name string) error {
|
||||
if name == "" {
|
||||
|
||||
5
db/migrations/20250116101000_snapshot_count.sql
Normal file
5
db/migrations/20250116101000_snapshot_count.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE snapshot_registry ADD COLUMN snapshot_count BIGINT NOT NULL DEFAULT 0;
|
||||
|
||||
-- +goose Down
|
||||
ALTER TABLE snapshot_registry DROP COLUMN snapshot_count;
|
||||
5
db/migrations_postgres/20250116101000_snapshot_count.sql
Normal file
5
db/migrations_postgres/20250116101000_snapshot_count.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE snapshot_registry ADD COLUMN IF NOT EXISTS snapshot_count BIGINT NOT NULL DEFAULT 0;
|
||||
|
||||
-- +goose Down
|
||||
ALTER TABLE snapshot_registry DROP COLUMN IF EXISTS snapshot_count;
|
||||
@@ -70,7 +70,8 @@ CREATE TABLE IF NOT EXISTS snapshot_registry (
|
||||
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
"snapshot_type" TEXT NOT NULL,
|
||||
"table_name" TEXT NOT NULL UNIQUE,
|
||||
"snapshot_time" INTEGER NOT NULL
|
||||
"snapshot_time" INTEGER NOT NULL,
|
||||
"snapshot_count" BIGINT NOT NULL DEFAULT 0
|
||||
);
|
||||
|
||||
-- The following tables are declared for sqlc type-checking only.
|
||||
|
||||
Reference in New Issue
Block a user