Skip to content

Commit

Permalink
pymongo 4: Specify UuidRepresentation=PYTHON_LEGACY
Browse files Browse the repository at this point in the history
Existing databases will have the old pymongo 3.x format, so we need
to use PYTHON_LEGACY until there is some kind of migration mechanism.
STANDARD would be better (more compatible with Java, C#, etc), but
we only use python any way, so that should not be a significant issue.
Plus, we use orjson encoded fields, so the database is already only
accessible via st2 python code.
  • Loading branch information
cognifloyd committed Sep 19, 2024
1 parent c4d2605 commit a6d51ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 11 additions & 0 deletions st2common/st2common/models/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import ssl as ssl_lib

import six
from bson.binary import UuidRepresentation
from oslo_config import cfg
import mongoengine
from mongoengine.queryset import visitor
Expand Down Expand Up @@ -190,7 +191,16 @@ def _db_connect(
# 30 seconds, which means it will block up to 30 seconds and fail if there are any SSL related
# or other errors
connection_timeout = cfg.CONF.database.connection_timeout

# TODO: Add uuid_representation option in st2.conf + a migration guide/script.
# This preserves the uuid handling from pymongo 3.x, but it is not portable:
# https://pymongo.readthedocs.io/en/stable/examples/uuid.html#handling-uuid-data-example
uuid_representation = UuidRepresentation.PYTHON_LEGACY

connection = mongoengine.connection.connect(
# kwargs are defined by mongoengine and pymongo.MongoClient:
# https://docs.mongoengine.org/apireference.html#mongoengine.connect
# https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient
db_name,
host=db_host,
port=db_port,
Expand All @@ -199,6 +209,7 @@ def _db_connect(
password=password,
connectTimeoutMS=connection_timeout,
serverSelectionTimeoutMS=connection_timeout,
uuidRepresentation=uuid_representation,
**ssl_kwargs,
**compressor_kwargs,
)
Expand Down
4 changes: 3 additions & 1 deletion st2common/st2common/models/db/stormbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import bson
import six
import mongoengine as me
from mongoengine.pymongo_support import LEGACY_JSON_OPTIONS
from oslo_config import cfg

from st2common.util import mongoescape
Expand Down Expand Up @@ -105,7 +106,8 @@ def to_serializable_dict(self, mask_secrets=False):
if isinstance(v, JSON_UNFRIENDLY_TYPES):
v = str(v)
elif isinstance(v, me.EmbeddedDocument):
v = json_decode(v.to_json())
# TODO: Allow overriding json_options.uuid_representation via cfg
v = json_decode(v.to_json(json_options=LEGACY_JSON_OPTIONS))

serializable_dict[k] = v

Expand Down

0 comments on commit a6d51ec

Please sign in to comment.