1package controller
2
3import (
4 "context"
5 "errors"
6
7 "github.com/pressly/goose/v3/database"
8)
9
10// A StoreController is used by the goose package to interact with a database. This type is a
11// wrapper around the Store interface, but can be extended to include additional (optional) methods
12// that are not part of the core Store interface.
13type StoreController struct{ database.Store }
14
15var _ database.StoreExtender = (*StoreController)(nil)
16
17// NewStoreController returns a new StoreController that wraps the given Store.
18//
19// If the Store implements the following optional methods, the StoreController will call them as
20// appropriate:
21//
22// - TableExists(context.Context, DBTxConn) (bool, error)
23//
24// If the Store does not implement a method, it will either return a [errors.ErrUnsupported] error
25// or fall back to the default behavior.
26func NewStoreController(store database.Store) *StoreController {
27 return &StoreController{store}
28}
29
30func (c *StoreController) TableExists(ctx context.Context, db database.DBTxConn) (bool, error) {
31 if t, ok := c.Store.(interface {
32 TableExists(ctx context.Context, db database.DBTxConn) (bool, error)
33 }); ok {
34 return t.TableExists(ctx, db)
35 }
36 return false, errors.ErrUnsupported
37}