Migrate to mongoengine 0.13/pymongo 3.5.1
Known pymongo incompatibilities:
aggregate
PyMongo 2.6 added an option to return an iterable cursor from aggregate(). In PyMongo 3 aggregate() always returns a cursor
- r["ok"] checks must be removed
- for record in r["result"] must be replaced with for record in r
Affected modules:
-
core/etl/bi/extractor/alarms.py -
fixes/fix_broken_outages.py -
main/models/textindex.py -
sa/models/servicesummary.py -
services/web/apps/cm/validationrule/views.py -
services/web/apps/fm/alarm/views.py -
services/web/apps/fm/monitor/views.py -
services/web/apps/fm/reportavailability/views.py -
services/web/apps/fm/reportreboots/views.py -
services/web/apps/inv/map/views.py -
services/web/apps/inv/monitor/views.py -
services/web/apps/inv/reportdiscovery/views.py -
services/web/apps/inv/reportdiscoverylinks/views.py -
services/web/apps/inv/reportdiscoverypoison/views.py -
services/web/apps/inv/reportobjectsummary/views.py -
services/web/apps/inv/reportpendinglinks/views.py -
services/web/apps/sa/reportdiscoveryproblem/views.py -
services/web/apps/sa/reportobjectdetail/views.py
cursor timeouts
find(...., timeout=False) must be replaced with find(..., no_cursor_timeout=True)
Affected modules:
-
core/etl/bi/extractor/alarms.py -
core/etl/bi/extractor/reboots.py
pymongo connection options
The “slave_okay” option is removed The slave_okay option is removed from PyMongo’s API. The secondaryPreferred read preference provides the same behavior. Code like this:
client = MongoClient(slave_okay=True)
can be changed to this with PyMongo 2.9 or newer:
client = MongoClient(readPreference="secondaryPreferred")
-
lib/nosql.py
MongoEngine connection error handling
ConnectionError is MongoEngineConnectionError now
-
lib/nosql.py
safe option is removed
In PyMongo 3 the safe option is removed from the entire API. MongoClient has always defaulted to acknowledged write operations and continues to do so in PyMongo 3.
-
core/scheduler/scheduler.py -
gis/migrations/0002_world_area.py
use update_many instead of multi=True
Use update_many for multi-document updates
-
core/scheduler/scheduler.py -
fixes/compact_schedules.py -
fm/models/alarmdiagnostic.py -
inv/migrations/0014_networksegmentprofile.py -
inv/models/networksegment.py -
main/models/authldapdomain.py -
main/models/synccache.py -
sa/migrations/0089_failed_log_expire.py -
sa/migrations/0154_profilecheckrule_profile.py -
sa/migrations/0155_actioncommands_profile.py
update operations return UpdateResult
r["ok"] must be replaced with r.acknowledges
r["nModified"] must be replaced with r.modified_count
-
core/scheduler/scheduler.py -
fm/models/activealarm.py
BulkWrite API changes
initialize_ordered_bulk_op/initialize_unordered_bulk_op should be replaced with simpler bulk_write API
from pprint import pprint
from pymongo import InsertOne, DeleteMany, ReplaceOne, UpdateOne
result = db.test.bulk_write([
DeleteMany({}), # Remove all documents from the previous example.
InsertOne({'_id': 1}),
InsertOne({'_id': 2}),
InsertOne({'_id': 3}),
UpdateOne({'_id': 1}, {'$set': {'foo': 'bar'}}),
UpdateOne({'_id': 4}, {'$inc': {'j': 1}}, upsert=True),
ReplaceOne({'j': 1}, {'j': 2})])
bulk_write operations return BulkWriteResult instead of dict.
r["nInserted"] must be replaced with r.inserted_count
r["nModified"] must be replaced with r.modified_count
r["nRemoved"] must be replaced with r.deleted_count
-
cm/engine.py -
commands/nri-link.py -
core/collection/base.py -
core/scheduler/scheduler.py -
fixes/fix_discoveryid_macs.py -
fm/migrations/0041_i18n.py -
fm/migrations/0042_upgrade_uptime.py -
gis/migrations/0004_migrate_geodata.py -
inv/models/macvendor.py -
main/migrations/0052_audit_data.py -
pm/migrations/0006_migrate_local.py -
pm/migrations/0007_set_has_children.py -
sa/migrations/0139_object_data.py -
sa/migrations/0140_move_object_path.py -
sa/models/objectdata.py -
sa/models/selectorcache.py -
sa/models/serviceprofile.py -
sa/models/servicesummary.py -
services/discovery/jobs/box/nri.py -
services/discovery/jobs/periodic/interfacestatus.py
The “read_preference” attribute is immutable
Code like this:
cursor = collection.find({"a": 1}, read_preference=ReadPreference.SECONDARY)
can be changed to this with PyMongo 2.9 or later:
coll2 = collection.with_options(read_preference=ReadPreference.SECONDARY)
cursor = coll2.find({"a": 1})
-
inv/models/discoveryid.py -
inv/models/interface.py -
sa/models/selectorcache.py -
services/discovery/jobs/periodic/interfacestatus.py -
services/discovery/jobs/periodic/metrics.py -
services/web/apps/fm/reportavailability/views.py -
services/web/apps/inv/reportdiscoverylinks/views.py -
services/web/apps/inv/reportpendinglinks/views.py -
services/web/apps/sa/reportdiscoveryproblem/views.py -
services/web/apps/sa/reportobjectdetail/views.py -
services/web/apps/sa/reportprofilechecksdetailed/views.py -
services/web/apps/sa/reportprofilechecksummary/views.py
NB:
All changes are performed on feature/moversion branch