Files
threshold/internal/oplog/writer.go
T
Jim 591ec476e7 refactor: Update internal/db and oplog to use go-dbw package for database operations. (#1785)
* chore: Add new dbw dependency

* refactor: Convert to use dbw.DB

* refactor (db): Change GetTicket(...) signature

* feature (db tests): Added TestDeleteWhere(...)
Adding this allows us to eliminate a leak of the gorm library

* refactor: Add ctx to ScanRows(...) signature
This is required to refactor all the deprecated errors pkg calls.
This refactor required adding a context to quite a few call chains
and in particular the job scheduler bits

* feature (db/tests): Add support for test opt: WithTestLogLevel(...)
This test only option allows callers to specify a log level for the
underlying db package (where supported/applicable) when calling
TestSetup(...)

* refactor (db): Simplify Db type
Only store the underlying *DB and create the *RW on demand when
needed.

* refactor (oplog): Port to using dbw pkg for db operations

* refactor (db): Remove gormDB(...) func

* tests (db): Add unit tests for TestDeleteWhere(...)

* chore: make fmt changes to formatting
2022-01-05 14:38:38 -05:00

71 lines
2.0 KiB
Go

package oplog
import (
"context"
"fmt"
"github.com/hashicorp/go-dbw"
"github.com/hashicorp/boundary/internal/errors"
)
// Writer provides a database writer for oplog operations
type Writer struct {
*dbw.DB
}
// HasTable checks if tableName exists
func (w *Writer) hasTable(ctx context.Context, tableName string) (bool, error) {
const op = "oplog.(Writer).hasTable"
if tableName == "" {
return false, errors.New(ctx, errors.InvalidParameter, op, "missing table name")
}
var count int64
rw := dbw.New(w.DB)
rows, err := rw.Query(context.Background(), "select count(*) from information_schema.tables where table_name = ? and table_type = ?", []interface{}{tableName, "BASE TABLE"})
if err != nil {
return false, errors.Wrap(ctx, err, op)
}
if ok := rows.Next(); ok {
rw.ScanRows(rows, &count)
}
return count > 0, nil
}
// CreateTableLike will create a newTableName like the model's table
// the new table should have all things like the existing model's table (defaults, constraints, indexes, etc)
func (w *Writer) createTableLike(ctx context.Context, existingTableName string, newTableName string) error {
const op = "oplog.(Writer).createTableLike"
if existingTableName == "" {
return errors.NewDeprecated(errors.InvalidParameter, op, "missing existing table name")
}
if newTableName == "" {
return errors.NewDeprecated(errors.InvalidParameter, op, "missing new table name")
}
sql := fmt.Sprintf(
`CREATE TABLE "%s" ( LIKE %s INCLUDING ALL );`,
newTableName,
existingTableName,
)
_, err := dbw.New(w.DB).Exec(ctx, sql, nil)
if err != nil {
return errors.Wrap(ctx, err, op)
}
return nil
}
// DropTableIfExists will drop the table if it exists
func (w *Writer) dropTableIfExists(ctx context.Context, tableName string) error {
const op = "oplog.(Writer).dropTableIfExists"
if tableName == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing table name")
}
sql := fmt.Sprintf("drop table if exists %s ", tableName)
_, err := dbw.New(w.DB).Exec(ctx, sql, nil)
if err != nil {
return errors.Wrap(ctx, err, op)
}
return nil
}