SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
806 stars 171 forks source link

Can't delete collection metadata properly #317

Closed hannomalie closed 5 years ago

hannomalie commented 5 years ago

We want to eliminate obsolete hooks from collections without deleting the collections.

Expected Behavior

We used hooks to gather request metrics in our restheart-based stack. Since we added metrics to restheart as a pull request some time ago, our hooks are obsolete now. There is a way to remove all hooks metadata from collections without the need for deleting collections.

Current Behavior

We wanted to remove the hooks from existing data, but didn't find a way to do it. The documentation doesn't give any hints on that. Patching hooks with an empty array gives an error.

Environment

Restheart version 3.4.2 MongoDB version 3.4.10

Steps to Reproduce

  1. Create a hook with curl -X PATCH http://localhost:8080/myproject/mycollection --header "content-type: application/json" --data '{"hooks": [{"name":"metricsHook"}]}'
  2. Try to remove the metadata with curl -X PATCH http://localhost:8080/myproject/mycollection --header "content-type: application/json" --data '{"hooks": [{"name":"metricsHook"}]}' curl -X PATCH http://localhost:8080/myproject/mycollection --header "content-type: application/json" --data '{"hooks": [{}]}' curl -X PATCH http://localhost:8080/myproject/mycollection --header "content-type: application/json" --data '{"hooks": []}'
  3. Take a look at various error messages.

Possible Implementation

Thanks guys :)

ujibang commented 5 years ago

I just tried and cannot reproduce the error.

create the collection with empty hooks array

$ curl -v -u a:a -X PUT http://localhost:8080/db/mycollection --header "content-type: application/json" --data '{"hooks": []}'

< HTTP/1.1 201 Created

add the hook

$ curl -v -u a:a -X PATCH http://localhost:8080/db/mycollection --header "content-type: application/json" --data '{"hooks": [{"name":"metricsHook"}]}'
< HTTP/1.1 200 OK

check that hook is actually defined (getting warning since hook is not defined in the configuration)

$ curl -v -u a:a http://localhost:8080/db/mycollection

< HTTP/1.1 200 OK

{
    "_embedded": [],
    "_id": "mycollection",
    "hooks": [{
        "name": "metricsHook"
    }],
    "_etag": {
        "$oid": "5beac97233efcc15d2cbd4c1"
    },
    "_returned": 0,
    "_warnings": ["error applying hook: no singleton configured with name: metricsHook"]
}

remove the hook from collection properties

$ curl -v -u a:a -X PATCH http://localhost:8080/db/mycollection --header "content-type: application/json" --data '{"hooks": []}'

< HTTP/1.1 200 OK

{
    "_warnings": ["error applying hook: no singleton configured with name: metricsHook"]
}

test that everything is fine

$ curl -v -u a:a http://localhost:8080/db/mycollection

< HTTP/1.1 200 OK

{
    "_embedded": [],
    "_id": "mycollection",
    "hooks": [],
    "_etag": {
        "$oid": "5beac9e033efcc15d2cbd4c3"
    },
    "_returned": 0
}
ujibang commented 5 years ago

regarding the partial updates, this is exactly the semantic of the verb PATCH.

from https://restheart.org/learn/write-requests/

While PUT and POST verbs replace the whole state of the resource identified by the request URL, PATCH verb only modifies the properties passed with the request JSON body.

ujibang commented 5 years ago

Also you can use the mongodb update operators

this removes the hooks property

$ curl -v -u a:a -X PATCH http://localhost:8080/db/mycollection --header "content-type: application/json" --data '{"$unset": {"hooks":true}}'

this removes the first element of the array hooks

$ curl -v -u a:a -X PATCH http://localhost:8080/db/mycollection --header "content-type: application/json" --data '{"$pop": {"hooks":-1}}' 
hannomalie commented 5 years ago

Thank you a ton for your fast answer - we're going to check this again with our setup as soon as we can and give you feedback :)

ujibang commented 5 years ago

closing for now. feel free to reopen with more information

hannomalie commented 5 years ago

I can confirm that this works. No idea what we did back then that made us believe it doesn't work though.. Thank you for your efforts, great job, as always :)