-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathwriter.go
70 lines (62 loc) · 2.05 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
}