Skip to content

Commit

Permalink
removed idempotency (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
subroseio authored Dec 1, 2023
1 parent 4d8c674 commit d01b038
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
7 changes: 7 additions & 0 deletions api/collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ func TestCollections(t *testing.T) {

response = performRequest(t, app, request)
checkResponse(t, response, http.StatusNotFound, nil)

// Delete the record again (should return 404)
request = newRequest(t, http.MethodDelete, fmt.Sprintf("/collections/customers/records/%s", returnedRecordIds[0]), map[string]string{
"Authorization": createBasicAuthHeader(core.conf.ADMIN_USERNAME, core.conf.ADMIN_PASSWORD),
}, nil)
response = performRequest(t, app, request)
checkResponse(t, response, http.StatusNotFound, nil)
})

t.Run("cant create a bad record", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion api/test.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ THORN_ENCRYPTION_SECRET="abc&1*~#^2^#s0^=)^^7%b34"
THORN_SIGNING_KEY=secret
THORN_DEV_MODE=false
THORN_LOG_LEVEL=debug
THORN_LOG_SINK=stdout
THORN_LOG_SINK=none
THORN_LOG_FORMAT=text
36 changes: 26 additions & 10 deletions vault/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,18 @@ func (st SqlStore) DeleteCollection(ctx context.Context, name string) error {
}
}()

_, err = tx.ExecContext(ctx, "DELETE FROM collection_metadata WHERE name = $1", name)
result, err := tx.ExecContext(ctx, "DELETE FROM collection_metadata WHERE name = $1", name)
if err != nil {
return err
}

rowsAffected, err := result.RowsAffected()
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return &NotFoundError{"collection", name}
}
return err
}
if rowsAffected == 0 {
return &NotFoundError{"collection", name}
}

tableName := "collection_" + name
_, err = tx.ExecContext(ctx, "DROP TABLE IF EXISTS "+tableName)
Expand Down Expand Up @@ -309,14 +314,19 @@ func (st SqlStore) UpdateRecord(ctx context.Context, collectionName string, reco
}

func (st SqlStore) DeleteRecord(ctx context.Context, collectionName string, recordID string) error {
_, err := st.db.ExecContext(ctx, "DELETE FROM collection_"+collectionName+" WHERE id = $1", recordID)
res, err := st.db.ExecContext(ctx, "DELETE FROM collection_"+collectionName+" WHERE id = $1", recordID)
if err != nil {
return err
}

rowsAffected, err := res.RowsAffected()
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return &NotFoundError{"record", recordID}
}
return err
}
if rowsAffected == 0 {
return &NotFoundError{"record", recordID}
}

return nil
}

Expand Down Expand Up @@ -418,11 +428,17 @@ func (st SqlStore) DeletePrincipal(ctx context.Context, username string) error {
return err
}

_, err = tx.ExecContext(ctx, "DELETE FROM principals WHERE username = $1", username)
result, err := tx.ExecContext(ctx, "DELETE FROM principals WHERE username = $1", username)
if err != nil {
return err
}

rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
return &NotFoundError{"principal", username}
}
err = tx.Commit()
if err != nil {
return err
Expand Down

0 comments on commit d01b038

Please sign in to comment.