add CreateItems and DeleteItems to writer interface (#156)

This commit is contained in:
Jim authored and GitHub committed 2020-06-29 19:28:15 -04:00
1 parent 8399764442
commit 8d8bb42418
2 files changed
+21 -1

No files matched your search

+18 -1
View File
@@ -76,6 +76,13 @@ type Writer interface {
// 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: WithOplog. 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
// 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
@@ -83,6 +90,13 @@ type Writer interface {
// 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. 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)
// DB returns the sql.DB
DB() (*sql.DB, error)
@@ -294,7 +308,10 @@ func (rw *Db) CreateItems(ctx context.Context, createItems []interface{}, opt ..
}
}
for _, item := range createItems {
rw.Create(ctx, item)
if err := rw.Create(ctx, item); err != nil {
return fmt.Errorf("create items: %w", err)
}
}
if opts.withOplog {
if err := rw.addOplogForItems(ctx, CreateOp, opts, ticket, createItems); err != nil {
+3
View File
@@ -1618,6 +1618,9 @@ func TestDb_CreateItems(t *testing.T) {
u.PublicId = item.(*db_test.TestUser).PublicId
err := rw.LookupByPublicId(context.Background(), &u)
assert.NoError(err)
if _, ok := item.(*db_test.TestUser); ok {
assert.Truef(proto.Equal(item.(*db_test.TestUser).StoreTestUser, u.StoreTestUser), "%s and %s should be equal", item, u)
}
}
if tt.wantOplogId != "" {
err = TestVerifyOplog(t, rw, tt.wantOplogId, WithOperation(oplog.OpType_OP_TYPE_CREATE), WithCreateNotBefore(10*time.Second))