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 f30b9be
Showing 1 changed file with 11 additions and 0 deletions.
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

0 comments on commit f30b9be

Please sign in to comment.