Documentation ¶
Index ¶
- Constants
- func AssertPublicId(t *testing.T, prefix, actual string)
- func Clear(i interface{}, fields []string, depth int) error
- func GetGormLogFormatter(log hclog.Logger) func(values ...interface{}) (messages []interface{})
- func NewPrivateId(prefix string, opt ...Option) (string, error)
- func NewPublicId(prefix string, opt ...Option) (string, error)
- func TestCreateTables(t *testing.T, conn *DB)
- func TestVerifyOplog(t *testing.T, r Reader, resourceId string, opt ...TestOption) error
- func TestWrapper(t *testing.T) wrapping.Wrapper
- type Backoff
- type ColumnValue
- type Columns
- type ConstBackoff
- type Constraint
- type DB
- type Db
- func (rw *Db) Create(ctx context.Context, i interface{}, opt ...Option) error
- func (rw *Db) CreateItems(ctx context.Context, createItems []interface{}, opt ...Option) error
- func (rw *Db) Delete(ctx context.Context, i interface{}, opt ...Option) (int, error)
- func (rw *Db) DeleteItems(ctx context.Context, deleteItems []interface{}, opt ...Option) (int, error)
- func (w *Db) DoTx(ctx context.Context, retries uint, backOff Backoff, Handler TxHandler) (RetryInfo, error)
- func (rw *Db) Exec(ctx context.Context, sql string, values []interface{}, _ ...Option) (int, error)
- func (rw *Db) GetTicket(i interface{}) (*store.Ticket, error)
- func (rw *Db) LookupById(ctx context.Context, resourceWithIder interface{}, _ ...Option) error
- func (rw *Db) LookupByPublicId(ctx context.Context, resource ResourcePublicIder, opt ...Option) error
- func (rw *Db) LookupWhere(ctx context.Context, resource interface{}, where string, args ...interface{}) error
- func (rw *Db) Query(ctx context.Context, sql string, values []interface{}, _ ...Option) (*sql.Rows, error)
- func (rw *Db) ScanRows(rows *sql.Rows, result interface{}) error
- func (rw *Db) SearchWhere(ctx context.Context, resources interface{}, where string, args []interface{}, ...) error
- func (rw *Db) Update(ctx context.Context, i interface{}, fieldMaskPaths []string, ...) (int, error)
- func (rw *Db) WriteOplogEntryWith(ctx context.Context, wrapper wrapping.Wrapper, ticket *store.Ticket, ...) error
- type DbType
- type DoNothing
- type ExpBackoff
- type ExprValue
- type OnConflict
- type OpType
- type Option
- func NewOplogMsg(msg *oplog.Message) Option
- func NewOplogMsgs(msgs *[]*oplog.Message) Option
- func WithDebug(with bool) Option
- func WithFieldMaskPaths(paths []string) Option
- func WithGormFormatter(l hclog.Logger) Option
- func WithLimit(limit int) Option
- func WithLookup(enable bool) Option
- func WithMaxOpenConnections(max int) Option
- func WithNullPaths(paths []string) Option
- func WithOnConflict(onConflict *OnConflict) Option
- func WithOplog(wrapper wrapping.Wrapper, md oplog.Metadata) Option
- func WithOrder(withOrder string) Option
- func WithPrngValues(withPrngValues []string) Option
- func WithReturnRowsAffected(rowsAffected *int64) Option
- func WithSkipVetForWrite(enable bool) Option
- func WithVersion(version *uint32) Option
- func WithWhere(whereClause string, args ...interface{}) Option
- type Options
- type OrderBy
- type Reader
- type ResourcePrivateIder
- type ResourcePublicIder
- type RetryInfo
- type TestOption
- type TxHandler
- type UpdateAll
- type VetForWriter
- type Writer
Constants ¶
const ( NoRowsAffected = 0 // DefaultLimit is the default for results for boundary DefaultLimit = 10000 )
const ( // UnknownOrderBy would designate an unknown ordering of the column, which // is the standard ordering for any select without an order by clause. UnknownOrderBy = iota // AscendingOrderBy would designate ordering the column in ascending order. AscendingOrderBy // DescendingOrderBy would designate ordering the column in decending order. DescendingOrderBy )
const (
StdRetryCnt = 20
)
Variables ¶
This section is empty.
Functions ¶
func AssertPublicId ¶
AssertPublicId is a test helper that asserts that the provided id is in the format of a public id.
func Clear ¶
Clear sets fields in the value pointed to by i to their zero value. Clear descends i to depth clearing fields at each level. i must be a pointer to a struct. Cycles in i are not detected.
A depth of 2 will change i and i's children. A depth of 1 will change i but no children of i. A depth of 0 will return with no changes to i.
func GetGormLogFormatter ¶
func GetGormLogFormatter(log hclog.Logger) func(values ...interface{}) (messages []interface{})
func NewPublicId ¶
NewPublicId creates a new public id with the prefix
func TestCreateTables ¶ added in v0.7.0
TestCreateTables will create the test tables for the db pkg
func TestVerifyOplog ¶
TestVerifyOplog will verify that there is an oplog entry that matches the provided resourceId. By default it matches the provided resourceId against the `resource-public-id` tag. If the WithResourcePrivateId option is provided, the lookup will use the `resource-private-id` tag. An error is returned if the entry or it's metadata is not found. Returning an error allows clients to test if an entry was not written, which is a valid use case.
Types ¶
type ColumnValue ¶ added in v0.7.0
type ColumnValue struct {
// contains filtered or unexported fields
}
ColumnValue defines a column and it's assigned value for a database operation
func SetColumnValues ¶ added in v0.7.0
func SetColumnValues(columnValues map[string]interface{}) []ColumnValue
SetColumnValues defines a map from column names to values
func SetColumns ¶ added in v0.7.0
func SetColumns(names []string) []ColumnValue
SetColumns defines a list of column (names) to update using the set of proposed insert columns during an on conflict update.
type ConstBackoff ¶
type Constraint ¶ added in v0.7.0
type Constraint string
Constraint defines database constraint name
type DB ¶ added in v0.7.0
DB is a wrapper around the ORM
func Open ¶
Open a database connection which is long-lived. The options of WithGormFormatter and WithMaxOpenConnections are supported.
Note: Consider if you need to call Close() on the returned DB. Typically the answer is no, but there are occasions when it's necessary. See the sql.DB docs for more information.
func TestSetup ¶
setup the tests (initialize the database one-time and intialized testDatabaseURL). Do not close the returned db.
type Db ¶
type Db struct {
// contains filtered or unexported fields
}
Db uses a gorm DB connection for read/write
func (*Db) Create ¶
Create an object in the db with options: WithDebug, WithOplog, NewOplogMsg, WithLookup, WithReturnRowsAffected, OnConflict, WithVersion, and WithWhere.
WithOplog will write an oplog entry for the create. NewOplogMsg will return in-memory oplog message. WithOplog and NewOplogMsg cannot be used together. WithLookup with to force a lookup after create.
OnConflict specifies alternative actions to take when an insert results in a unique constraint or exclusion constraint error. If WithVersion is used, then the update for on conflict will include the version number, which basically makes the update use optimistic locking and the update will only succeed if the existing rows version matches the WithVersion option. Zero is not a valid value for the WithVersion option and will return an error. WithWhere allows specifying an additional constraint on the on conflict operation in addition to the on conflict target policy (columns or constraint).
func (*Db) CreateItems ¶
CreateItems will create multiple items of the same type. Supported options: WithDebug, WithOplog, WithOplogMsgs, WithReturnRowsAffected, OnConflict, WithVersion, and WithWhere WithOplog and WithOplogMsgs may not be used together. WithLookup is not a supported option.
func (*Db) Delete ¶
Delete an object in the db with options: WithOplog, NewOplogMsg, WithWhere. WithOplog will write an oplog entry for the delete. NewOplogMsg will return in-memory oplog message. WithOplog and NewOplogMsg cannot be used together. WithWhere allows specifying an additional constraint on the operation in addition to the PKs. Delete returns the number of rows deleted and any errors.
func (*Db) DeleteItems ¶
func (rw *Db) DeleteItems(ctx context.Context, deleteItems []interface{}, opt ...Option) (int, error)
DeleteItems will delete multiple items of the same type. Supported options: WithOplog and WithOplogMsgs. WithOplog and WithOplogMsgs may not be used together.
func (*Db) DoTx ¶
func (w *Db) DoTx(ctx context.Context, retries uint, backOff Backoff, Handler TxHandler) (RetryInfo, error)
DoTx will wrap the Handler func passed within a transaction with retries you should ensure that any objects written to the db in your TxHandler are retryable, which means that the object may be sent to the db several times (retried), so things like the primary key must be reset before retry
func (*Db) Exec ¶
Exec will execute the sql with the values as parameters. The int returned is the number of rows affected by the sql. No options are currently supported.
func (*Db) GetTicket ¶
GetTicket returns an oplog ticket for the aggregate root of "i" which can be used to WriteOplogEntryWith for that aggregate root.
func (*Db) LookupById ¶
LookupByPublicId will lookup resource by its public_id or private_id, which must be unique. Options are ignored.
func (*Db) LookupByPublicId ¶
func (rw *Db) LookupByPublicId(ctx context.Context, resource ResourcePublicIder, opt ...Option) error
LookupByPublicId will lookup resource by its public_id, which must be unique. Options are ignored.
func (*Db) LookupWhere ¶
func (rw *Db) LookupWhere(ctx context.Context, resource interface{}, where string, args ...interface{}) error
LookupWhere will lookup the first resource using a where clause with parameters (it only returns the first one)
func (*Db) Query ¶
func (rw *Db) Query(ctx context.Context, sql string, values []interface{}, _ ...Option) (*sql.Rows, error)
Query will run the raw query and return the *sql.Rows results. Query will operate within the context of any ongoing transaction for the db.Reader. The caller must close the returned *sql.Rows. Query can/should be used in combination with ScanRows.
func (*Db) SearchWhere ¶
func (rw *Db) SearchWhere(ctx context.Context, resources interface{}, where string, args []interface{}, opt ...Option) error
SearchWhere will search for all the resources it can find using a where clause with parameters. An error will be returned if args are provided without a where clause.
Supports the WithLimit option. If WithLimit < 0, then unlimited results are returned. If WithLimit == 0, then default limits are used for results. Supports the WithOrder and WithDebug options.
func (*Db) Update ¶
func (rw *Db) Update(ctx context.Context, i interface{}, fieldMaskPaths []string, setToNullPaths []string, opt ...Option) (int, error)
Update an object in the db, fieldMask is required and provides field_mask.proto paths for fields that should be updated. The i interface parameter is the type the caller wants to update in the db and its fields are set to the update values. setToNullPaths is optional and provides field_mask.proto paths for the fields that should be set to null. fieldMaskPaths and setToNullPaths must not intersect. The caller is responsible for the transaction life cycle of the writer and if an error is returned the caller must decide what to do with the transaction, which almost always should be to rollback. Update returns the number of rows updated.
Supported options: WithOplog, NewOplogMsg, WithWhere, WithDebug, and WithVersion. WithOplog will write an oplog entry for the update. NewOplogMsg will return in-memory oplog message. WithOplog and NewOplogMsg cannot be used together. If WithVersion is used, then the update will include the version number in the update where clause, which basically makes the update use optimistic locking and the update will only succeed if the existing rows version matches the WithVersion option. Zero is not a valid value for the WithVersion option and will return an error. WithWhere allows specifying an additional constraint on the operation in addition to the PKs. WithDebug will turn on debugging for the update call.
func (*Db) WriteOplogEntryWith ¶
func (rw *Db) WriteOplogEntryWith(ctx context.Context, wrapper wrapping.Wrapper, ticket *store.Ticket, metadata oplog.Metadata, msgs []*oplog.Message, _ ...Option) error
WriteOplogEntryWith will write an oplog entry with the msgs provided for the ticket's aggregateName. No options are currently supported.
type DoNothing ¶ added in v0.7.0
type DoNothing bool
DoNothing defines an "on conflict" action of doing nothing
type ExpBackoff ¶
type ExpBackoff struct{}
type ExprValue ¶ added in v0.7.0
type ExprValue struct {
// contains filtered or unexported fields
}
ExprValue encapsulates an expression value for a column assignment. See Expr(...) to create these values.
func Expr ¶ added in v0.7.0
Expr creates an expression value (ExprValue) which can be used when setting column values for database operations. See: Expr(...)
Set name column to null example:
SetColumnValues(map[string]interface{}{"name": Expr("NULL")})
Set exp_time column to N seconds from now:
SetColumnValues(map[string]interface{}{"exp_time": Expr("wt_add_seconds_to_now(?)", 10)})
type OnConflict ¶ added in v0.7.0
type OnConflict struct { // Target specifies what conflict you want to define a policy for. This can // be any one of these: // Columns: the name of a specific column or columns // Constraint: the name of a unique constraint Target interface{} // Action specifies the action to take on conflict. This can be any one of // these: // DoNothing: leaves the conflicting record as-is // UpdateAll: updates all the columns of the conflicting record using the resource's data // []ColumnValue: update a set of columns of the conflicting record using the set of assignments Action interface{} }
OnConflict specifies how to handle alternative actions to take when an insert results in a unique constraint or exclusion constraint error.
type Option ¶
type Option func(*Options)
Option - how Options are passed as arguments.
func NewOplogMsg ¶
NewOplogMsg provides an option to ask for a new in-memory oplog message. The new msg will be returned in the provided *oplog.Message parameter. WithOplog and NewOplogMsg cannot be used together.
func NewOplogMsgs ¶
NewOplogMsgs provides an option to ask for multiple new in-memory oplog messages. The new msgs will be returned in the provided *[]oplog.Message parameter. NewOplogMsgs can only be used with write functions that operate on multiple items(CreateItems, DeleteItems). WithOplog and NewOplogMsgs cannot be used together.
func WithDebug ¶ added in v0.7.0
WithDebug specifies the given operation should invoke debug mode in Gorm
func WithFieldMaskPaths ¶
WithFieldMaskPaths provides an option to provide field mask paths.
func WithGormFormatter ¶ added in v0.7.0
func WithGormFormatter(l hclog.Logger) Option
WithGormFormatter specifies an optional hclog to use for gorm's log formmater
func WithLimit ¶
WithLimit provides an option to provide a limit. Intentionally allowing negative integers. If WithLimit < 0, then unlimited results are returned. If WithLimit == 0, then default limits are used for results.
func WithMaxOpenConnections ¶ added in v0.7.0
WithMaxOpenConnections specifices and optional max open connections for the database
func WithNullPaths ¶
WithNullPaths provides an option to provide null paths.
func WithOnConflict ¶ added in v0.7.0
func WithOnConflict(onConflict *OnConflict) Option
WithOnConflict specifies an optional on conflict criteria which specify alternative actions to take when an insert results in a unique constraint or exclusion constraint error
func WithOplog ¶
WithOplog provides an option to write an oplog entry. WithOplog and NewOplogMsg cannot be used together.
func WithPrngValues ¶ added in v0.2.0
WithPrngValues provides an option to provide values to seed an PRNG when generating IDs
func WithReturnRowsAffected ¶ added in v0.7.0
WithReturnRowsAffected specifies an option for returning the rows affected
func WithSkipVetForWrite ¶
WithSkipVetForWrite provides an option to allow skipping vet checks to allow testing lower-level SQL triggers and constraints
func WithVersion ¶
WithVersion provides an option version number for update operations.
type Options ¶
type Options struct { // WithLimit must be accessible in other packages. WithLimit int // WithFieldMaskPaths must be accessible from other packages. WithFieldMaskPaths []string // WithNullPaths must be accessible from other packages. WithNullPaths []string // WithVersion must be accessible from other packages. WithVersion *uint32 // contains filtered or unexported fields }
Options - how Options are represented.
type OrderBy ¶ added in v0.2.0
type OrderBy int
OrderBy defines an enum type for declaring a column's order by criteria.
type Reader ¶
type Reader interface { // LookupById will lookup a resource by its primary key id, which must be // unique. If the resource implements either ResourcePublicIder or // ResourcePrivateIder interface, then they are used as the resource's // primary key for lookup. Otherwise, the resource tags are used to // determine it's primary key(s) for lookup. LookupById(ctx context.Context, resource interface{}, opt ...Option) error // LookupByPublicId will lookup resource by its public_id which must be unique. LookupByPublicId(ctx context.Context, resource ResourcePublicIder, opt ...Option) error // LookupWhere will lookup and return the first resource using a where clause with parameters LookupWhere(ctx context.Context, resource interface{}, where string, args ...interface{}) error // SearchWhere will search for all the resources it can find using a where // clause with parameters. Supports the WithLimit option. If // WithLimit < 0, then unlimited results are returned. If WithLimit == 0, then // default limits are used for results. SearchWhere(ctx context.Context, resources interface{}, where string, args []interface{}, opt ...Option) error // Query will run the raw query and return the *sql.Rows results. Query will // operate within the context of any ongoing transaction for the db.Reader. The // caller must close the returned *sql.Rows. Query can/should be used in // combination with ScanRows. Query(ctx context.Context, sql string, values []interface{}, opt ...Option) (*sql.Rows, error) // ScanRows will scan sql rows into the interface provided ScanRows(rows *sql.Rows, result interface{}) error }
Reader interface defines lookups/searching for resources
type ResourcePrivateIder ¶
type ResourcePrivateIder interface {
GetPrivateId() string
}
ResourcePrivateIder defines an interface that LookupById() can use to get the resource's private id.
type ResourcePublicIder ¶
type ResourcePublicIder interface {
GetPublicId() string
}
ResourcePublicIder defines an interface that LookupByPublicId() can use to get the resource's public id.
type TestOption ¶
type TestOption func(*testOptions)
TestOption - how Options are passed as arguments
func WithCreateNotBefore ¶
func WithCreateNotBefore(nbfDuration time.Duration) TestOption
WithCreateNotBefore provides an option to specify that the create time is not before (nbf) N seconds
func WithOperation ¶
func WithOperation(op oplog.OpType) TestOption
WithOperation provides an option to specify the operation type
func WithResourcePrivateId ¶ added in v0.2.2
func WithResourcePrivateId(enable bool) TestOption
WithResourcePrivateId provides a way to specify that the resource lookup action uses `resource-private-id` tag instead of the default `resource-public-id` tag
func WithTemplate ¶ added in v0.6.1
func WithTemplate(template string) TestOption
WithTemplate provides a way to specify the source database template for creating a database.
func WithTestDatabaseUrl ¶ added in v0.1.1
func WithTestDatabaseUrl(url string) TestOption
WithTestDatabaseUrl provides a way to specify an existing database for tests
type UpdateAll ¶ added in v0.7.0
type UpdateAll bool
UpdateAll defines an "on conflict" action of updating all columns using the proposed insert column values
type VetForWriter ¶
type VetForWriter interface {
VetForWrite(ctx context.Context, r Reader, opType OpType, opt ...Option) error
}
VetForWriter provides an interface that Create and Update can use to vet the resource before before writing it to the db. For optType == UpdateOp, options WithFieldMaskPath and WithNullPaths are supported. For optType == CreateOp, no options are supported
type Writer ¶
type Writer interface { // DoTx will wrap the TxHandler in a retryable transaction DoTx(ctx context.Context, retries uint, backOff Backoff, Handler TxHandler) (RetryInfo, error) // Update an object in the db, fieldMask is required and provides // field_mask.proto paths for fields that should be updated. The i interface // parameter is the type the caller wants to update in the db and its // fields are set to the update values. setToNullPaths is optional and // provides field_mask.proto paths for the fields that should be set to // null. fieldMaskPaths and setToNullPaths must not intersect. The caller // is responsible for the transaction life cycle of the writer and if an // error is returned the caller must decide what to do with the transaction, // which almost always should be to rollback. Update returns the number of // rows updated or an error. Supported options: WithOplog. Update(ctx context.Context, i interface{}, fieldMaskPaths []string, setToNullPaths []string, opt ...Option) (int, error) // Create an object in the db with options: WithDebug, WithOplog, NewOplogMsg, // WithLookup, WithReturnRowsAffected, OnConflict, WithVersion, and // WithWhere. The caller is responsible for the transaction life cycle of // the writer and if an error is returned the caller must decide what to do // with the transaction, which almost always should be to rollback. Create(ctx context.Context, i interface{}, opt ...Option) error // CreateItems will create multiple items of the same type. // Supported options: WithDebug, WithOplog, WithOplogMsgs, // WithReturnRowsAffected, OnConflict, WithVersion, and WithWhere. /// WithOplog and WithOplogMsgs may not be used together. WithLookup is not // a supported option. The caller is responsible for the transaction life // cycle of the writer and if an error is returned the caller must decide // what to do with the transaction, which almost always should be to // rollback. CreateItems(ctx context.Context, createItems []interface{}, opt ...Option) error // Delete an object in the db with options: WithOplog, WithDebug. // The caller is responsible for the transaction life cycle of the writer // and if an error is returned the caller must decide what to do with // the transaction, which almost always should be to rollback. Delete // returns the number of rows deleted or an error. Delete(ctx context.Context, i interface{}, opt ...Option) (int, error) // DeleteItems will delete multiple items of the same type. // Supported options: WithOplog and WithOplogMsgs. WithOplog and // WithOplogMsgs may not be used together. The caller is responsible for the // transaction life cycle of the writer and if an error is returned the // caller must decide what to do with the transaction, which almost always // should be to rollback. Delete returns the number of rows deleted or an error. DeleteItems(ctx context.Context, deleteItems []interface{}, opt ...Option) (int, error) // Exec will execute the sql with the values as parameters. The int returned // is the number of rows affected by the sql. No options are currently // supported. Exec(ctx context.Context, sql string, values []interface{}, opt ...Option) (int, error) // Query will run the raw query and return the *sql.Rows results. Query will // operate within the context of any ongoing transaction for the db.Writer. The // caller must close the returned *sql.Rows. Query can/should be used in // combination with ScanRows. Query is included in the Writer interface // so callers can execute updates and inserts with returning values. Query(ctx context.Context, sql string, values []interface{}, opt ...Option) (*sql.Rows, error) // GetTicket returns an oplog ticket for the aggregate root of "i" which can // be used to WriteOplogEntryWith for that aggregate root. GetTicket(i interface{}) (*store.Ticket, error) // WriteOplogEntryWith will write an oplog entry with the msgs provided for // the ticket's aggregateName. No options are currently supported. WriteOplogEntryWith( ctx context.Context, wrapper wrapping.Wrapper, ticket *store.Ticket, metadata oplog.Metadata, msgs []*oplog.Message, opt ...Option, ) error // ScanRows will scan sql rows into the interface provided ScanRows(rows *sql.Rows, result interface{}) error }
Writer interface defines create, update and retryable transaction handlers
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package dbassert provides a set of assertions for testing the boundary database applications.
|
Package dbassert provides a set of assertions for testing the boundary database applications. |
common package contains functions from internal/db which need to be shared commonly with other packages that have a cyclic dependency on internal/db like internal/oplog.
|
common package contains functions from internal/db which need to be shared commonly with other packages that have a cyclic dependency on internal/db like internal/oplog. |
Package db_test provides some helper funcs for testing db integrations
|
Package db_test provides some helper funcs for testing db integrations |
Package sanitize contains a set of functions that sanitizes input received from external systems before being persisted in the database.
|
Package sanitize contains a set of functions that sanitizes input received from external systems before being persisted in the database. |
Package schema is used to apply sql migrations to modify the state of a database instance.
|
Package schema is used to apply sql migrations to modify the state of a database instance. |
internal/edition
Package edition provides internal structs for the schema package for defining and organizing database migration editions.
|
Package edition provides internal structs for the schema package for defining and organizing database migration editions. |
internal/log
Package log provides internal structs and options for the schema package for tracking logs generated when applying migrations.
|
Package log provides internal structs and options for the schema package for tracking logs generated when applying migrations. |
internal/postgres
Package postgres provides an implementation of the schema.driver interface for a PostgreSQL database.
|
Package postgres provides an implementation of the schema.driver interface for a PostgreSQL database. |
internal/provider
Package provider provides an iterator for iterating over all of the migration statements that need to be applied.
|
Package provider provides an iterator for iterating over all of the migration statements that need to be applied. |
migrations
Package migrations contains the base sql statements needed to bootstrap the migration process.
|
Package migrations contains the base sql statements needed to bootstrap the migration process. |
migrations/oss
Package oss is used to embed the sql statements for the oss edition and registering the edition for the schema.Manager.
|
Package oss is used to embed the sql statements for the oss edition and registering the edition for the schema.Manager. |
Package sentinel allows for the use of Unicode non-characters to distinguish between Boundary defined sentinels and values provided by external systems.
|
Package sentinel allows for the use of Unicode non-characters to distinguish between Boundary defined sentinels and values provided by external systems. |