* database: add wt_public_id domain type * static-hosts: add migrations * Rename migrations * migrations: add description column and fix foreign keys * Add protobuf for static host * Add HostCatalog and HostSet * Add Options * Add foreign key constraint * Add package doc * Add copied scanner code * Add NewHostCatalog with tests * Override default table name for static host catalog * WIP: save static host catalog to database * WIP: Print db url for debugging * Add iam test helper function * Make gorm default to null for names and descriptions * Add foreign key constraint from host catalog to scope * Names are not needed for the test org and project * Avoid an ordering dependency in down migrations * Use wt_timestamp * Add check constraint on host address column * Add Host * Add HostSet * Add HostSetMember * Test a HostSetMember can be written to the database * Move the getters and setters for table name to a new file The TableName methods are needed by gorm and the SetTableName methods are for oplog. Moving them to a new file so related functionality is in the same file. * Add repository * Add NewRepository * Close connection before cleanup of database * Add comments to protobuf file and regenerate * Define repository methods for HostCatalog * Assign PublicId when resource is inserted into the database * Wrap a nil parameter error * Add oplog tickets for static host types * WIP: first cut at CreateCatalog * CreateHostCatalog: more tests * Detect unique constraint violation * Use const for PublicId prefixes * Use PostgreSQL error name instead of error code * Add TODO comments * Fix comments * Use PublicId not PublicID * Refactor: move error variables from host/static to db The error variables defined in host/static are generic and can be reused by other packages. * Create a new repository for each test * Validate CreateCatalog returns a new HostCatalog * Do not modify the HostCatalog passed in * Implement UpdateCatalog * Minor cleanup to make folds work * Implement LookupCatalog * Refactor: extract allocCatalog function * Add comment * Refactor: extract newCatalogMetadata function * Make error strings more consistent * Move database specific error check to internal/db package * Change signature of DeleteCatalog and update docs * Implement DeleteCatalog * Make error strings more consistent * Add examples * Update docs * Add package level doc and example for repository * No need for using reflect.DeepEqual * Remove redundant comment * Clarify ErrMultipleRecords results in the transaction being rolled back * Remove redundant error checks * Remove unnecessary check * Return early instead of using nested 'else' statements * Update and Delete must return a row count * catch err on defers (#82) Co-authored-by: Jim Lambert <jimlambrt@Jims-MBP-3.home> * Remove 'if assert' pattern * Use assert package in test helper function * Add test for updating a non existent catalog * Use a single assert when a specific error is expected * Use assert package when it clarifies the check * Catch errors in cleanup functions * Update comments in protobuf file * Update doc for UpdateCatalog to reflect the new behavior * Add common error values for field mask errors * Require a field mask when calling update methods * Add TODO for removing direct call to gorm * Export Public ID prefixes * Add db timestamp guards * Remove unused option Co-authored-by: Jim <jlambert@hashicorp.com> Co-authored-by: Jim Lambert <jimlambrt@Jims-MBP-3.home>
db package
Usage
Just some high-level usage highlights to get you started. Read the godocs for a complete list of capabilities and their documentation.
conn, _ := gorm.Open("postgres", url)
// Db implements both the Reader and Writer interfaces
rw := Db{Tx: conn}
// There are writer methods like: Create, Update and Delete
// that will write Gorm struct to the db. These writer methods
// all support options for writing Oplog entries for the
// caller: WithOplog(yourWrapper, yourMetadata)
// 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 is almost always a rollback for the caller.
err = rw.Create(context.Background(), user)
// There are reader methods like: LookupByPublicId,
// LookupByName, SearchBy, LookupBy, etc
// which will lookup resources for you and scan them into your Gorm struct
err = rw.LookupByPublicId(context.Background(), foundUser)
// There's reader ScanRows that facilitates scanning rows from
// a "raw" SQL query into your Gorm struct
tx, err := rw.DB()
where := "select * from test_users where name in ($1, $2)"
rows, err := tx.Query(where, "alice", "bob")
defer rows.Close()
for rows.Next() {
user := db_test.NewTestUser()
// scan the row into your Gorm struct
if err := rw.ScanRows(rows, &user); err != nil {
return err
}
// Do something with the Gorm user struct
}
// DoTx is a writer function that wraps a TxHandler
// in a retryable transaction. You simply implement a
// TxHandler that does your writes and hand the handler
// to DoTx, which will wrap the writes in a retryable
// transaction with the retry attempts and backoff
// strategy you specify via options.
_, err = w.DoTx(
context.Background(),
10, // ten retries
ExpBackoff{}, // exponential backoff
func(w Writer) error {
// the TxHandler updates the user's friendly name
return w.Update(context.Background(), user, []string{"FriendlyName"},
// write oplogs for this update
WithOplog(
InitTestWrapper(t),
oplog.Metadata{
"key-only": nil,
"deployment": []string{"amex"},
"project": []string{"central-info-systems", "local-info-systems"},
},
),
)
})