2020-04-20 16:42:16 -04:00
// Copyright (c) HashiCorp, Inc.
2022-01-05 14:38:38 -05:00
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
2020-04-08 14:55:40 -04:00
package oplog
import (
2022-01-05 14:38:38 -05:00
"context"
2020-04-08 14:55:40 -04:00
"fmt"
2020-06-10 06:56:46 -04:00
2022-01-05 14:38:38 -05:00
"github.com/hashicorp/go-dbw"
2021-09-30 08:47:34 -04:00
2021-01-13 15:50:07 -08:00
"github.com/hashicorp/boundary/internal/errors"
2020-04-08 14:55:40 -04:00
)
2022-01-05 14:38:38 -05:00
// Writer provides a database writer for oplog operations
type Writer struct {
* dbw . DB
2020-04-08 14:55:40 -04:00
}
2022-01-05 14:38:38 -05:00
// 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" )
2020-04-08 14:55:40 -04:00
}
2022-01-05 14:38:38 -05:00
var count int64
rw := dbw . New ( w . DB )
2022-10-25 18:29:38 -05:00
rows , err := rw . Query ( context . Background (), "select count(*) from information_schema.tables where table_name = ? and table_type = ?" , [] any { tableName , "BASE TABLE" })
2020-06-10 06:56:46 -04:00
if err != nil {
2022-01-05 14:38:38 -05:00
return false , errors . Wrap ( ctx , err , op )
2020-04-08 14:55:40 -04:00
}
2022-01-05 14:38:38 -05:00
if ok := rows . Next (); ok {
rw . ScanRows ( rows , & count )
2020-04-08 14:55:40 -04:00
}
2024-01-22 14:03:40 -05:00
if err := rows . Err (); err != nil {
return false , errors . Wrap ( ctx , err , op )
}
2022-01-05 14:38:38 -05:00
return count > 0 , nil
2020-04-20 16:42:16 -04:00
}
// 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)
2022-01-05 14:38:38 -05:00
func ( w * Writer ) createTableLike ( ctx context . Context , existingTableName string , newTableName string ) error {
const op = "oplog.(Writer).createTableLike"
2020-04-20 16:42:16 -04:00
if existingTableName == "" {
2023-06-06 17:09:08 -04:00
return errors . New ( ctx , errors . InvalidParameter , op , "missing existing table name" )
2020-04-20 16:42:16 -04:00
}
if newTableName == "" {
2023-06-06 17:09:08 -04:00
return errors . New ( ctx , errors . InvalidParameter , op , "missing new table name" )
2020-04-20 16:42:16 -04:00
}
2021-09-30 08:47:34 -04:00
2022-01-05 14:38:38 -05:00
sql := fmt . Sprintf (
`CREATE TABLE "%s" ( LIKE %s INCLUDING ALL );` ,
newTableName ,
existingTableName ,
)
_ , err := dbw . New ( w . DB ). Exec ( ctx , sql , nil )
2021-01-13 15:50:07 -08:00
if err != nil {
2022-01-05 14:38:38 -05:00
return errors . Wrap ( ctx , err , op )
2020-04-20 16:42:16 -04:00
}
2021-01-13 15:50:07 -08:00
return nil
2020-04-20 16:42:16 -04:00
}
// DropTableIfExists will drop the table if it exists
2022-01-05 14:38:38 -05:00
func ( w * Writer ) dropTableIfExists ( ctx context . Context , tableName string ) error {
const op = "oplog.(Writer).dropTableIfExists"
2020-04-20 16:42:16 -04:00
if tableName == "" {
2022-01-05 14:38:38 -05:00
return errors . New ( ctx , errors . InvalidParameter , op , "missing table name" )
2020-04-20 16:42:16 -04:00
}
2022-01-05 14:38:38 -05:00
sql := fmt . Sprintf ( "drop table if exists %s " , tableName )
_ , err := dbw . New ( w . DB ). Exec ( ctx , sql , nil )
2021-01-13 15:50:07 -08:00
if err != nil {
2022-01-05 14:38:38 -05:00
return errors . Wrap ( ctx , err , op )
2021-01-13 15:50:07 -08:00
}
return nil
2020-04-20 16:42:16 -04:00
}