Skip to content

Commit

Permalink
Fix the implementation of save
Browse files Browse the repository at this point in the history
So the previous implementation of `save` broke in the case that we tried to
insert an entry with an `_id` field.

```
In [4]: list(db.test_coll.find())
Out[4]: [{'_id': ObjectId('5e672b717cc2bf7fb0f7adc6'), 'b': 201, 'c': 301}]
In [9]: result = db.test_coll.replace_one({}, {'user_id': UUID('0e77ea51-184a-41e5-a025-
   ...: 9aa7f143d9f8'), '_id': ObjectId('584e66d088f6635c46b95a50'), 'data': {'reading':
   ...:  0.41276121139526367, 'name': 'POST_/usercache/get', 'ts': 1481533136.761428}, '
   ...: metadata': {'write_fmt_time': '2016-12-12T00:58:56.761957-08:00', 'write_ts': 14
   ...: 81533136.761957, 'time_zone': 'America/Los_Angeles', 'platform': 'server', 'writ
   ...: e_local_dt': {'hour': 0, 'month': 12, 'second': 56, 'weekday': 0, 'year': 2016,
   ...: 'timezone': 'America/Los_Angeles', 'day': 12, 'minute': 58}, 'key': 'stats/serve
   ...: r_api_time'}}, upsert=True)
WriteError                                Traceback (most recent call last)
WriteError: After applying the update, the (immutable) field '_id' was found to have been altered to _id: ObjectId('584e66d088f6635c46b95a50')
```

While this is not common in regular operation, it is used frequently in the
tests when we load existing data with `_id` fields.

So I had to modify the `save` command to treat the case with and without the
`_id` field separately.  If the update doc has an `_id` field, it must appear
in the selection part of the method as well, even if there is no actual
matching entry in the database. For example, the previous call works if we change it to

```
result = db.test_coll.replace_one({'_id': ObjectId('584e66d088f6635c46b95a50')}
    ...: , {'user_id': UUID('0e77ea51-184a-41e5-a025-9aa7f143d9f8'), '_id': ObjectId('58
    ...: 4e66d088f6635c46b95a50'), 'data': {...}, 'metadata': {...}})
```

Note that the entry was inserted (or rather upserted) instead of modified

```
In [12]: result.raw_result
Out[12]:
{'n': 1,
 'nModified': 0,
 'ok': 1.0,
 'updatedExisting': False,
 'upserted': ObjectId('584e66d088f6635c46b95a50')}
```
  • Loading branch information
shankari committed Mar 10, 2020
1 parent 9337547 commit 78c0908
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion emission/core/get_database.py
Expand Up @@ -263,4 +263,7 @@ def get_fake_sections_db():
# pymongo 3.0.
# https://github.com/e-mission/e-mission-server/issues/533#issuecomment-349430623
def save(db, entry):
db.replace_one({}, entry, upsert=True)
if '_id' in entry:
db.replace_one({'_id': entry['_id']}, entry, upsert=True)
else:
db.replace_one({}, entry, upsert=True)

0 comments on commit 78c0908

Please sign in to comment.