Skip to content

Commit c874c96

Browse files
authored
PYTHON-3232 Improved change stream event visibility for C2C Replication (#1062)
1 parent 2af12e6 commit c874c96

File tree

9 files changed

+803
-1
lines changed

9 files changed

+803
-1
lines changed

doc/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ PyMongo 4.3 brings a number of improvements including:
2222
deadlocks are still possible because libraries that PyMongo depends like
2323
OpenSSL cannot be made fork() safe in multithreaded applications.
2424
(`PYTHON-2484`_). For more info see :ref:`pymongo-fork-safe`.
25+
- When used with MongoDB 6.0+, :class:`~pymongo.change_stream.ChangeStream` s
26+
now allow for new types of events (such as DDL and C2C replication events)
27+
to be recorded with the new parameter ``show_expanded_events``
28+
that can be passed to methods such as :meth:`~pymongo.collection.Collection.watch`.
2529

2630
Bug fixes
2731
.........

pymongo/aggregation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def __init__(
4040
user_fields=None,
4141
result_processor=None,
4242
comment=None,
43+
show_expanded_events=None,
4344
):
4445
if "explain" in options:
4546
raise ConfigurationError(
@@ -60,6 +61,7 @@ def __init__(
6061
options["let"] = let
6162
if comment is not None:
6263
options["comment"] = comment
64+
6365
self._options = options
6466

6567
# This is the batchSize that will be used for setting the initial

pymongo/change_stream.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def __init__(
109109
start_after: Optional[Mapping[str, Any]],
110110
comment: Optional[Any] = None,
111111
full_document_before_change: Optional[str] = None,
112+
show_expanded_events: Optional[bool] = None,
112113
) -> None:
113114
if pipeline is None:
114115
pipeline = []
@@ -143,6 +144,7 @@ def __init__(
143144
self._comment = comment
144145
self._closed = False
145146
self._timeout = self._target._timeout
147+
self._show_expanded_events = show_expanded_events
146148
# Initialize cursor.
147149
self._cursor = self._create_cursor()
148150

@@ -175,6 +177,10 @@ def _change_stream_options(self):
175177

176178
if self._start_at_operation_time is not None:
177179
options["startAtOperationTime"] = self._start_at_operation_time
180+
181+
if self._show_expanded_events:
182+
options["showExpandedEvents"] = self._show_expanded_events
183+
178184
return options
179185

180186
def _command_options(self):
@@ -230,6 +236,7 @@ def _run_aggregation_cmd(self, session, explicit_session):
230236
explicit_session,
231237
result_processor=self._process_result,
232238
comment=self._comment,
239+
show_expanded_events=self._show_expanded_events,
233240
)
234241
return self._client._retryable_read(
235242
cmd.get_cursor, self._target._read_preference_for(session), session

pymongo/collection.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2495,6 +2495,7 @@ def watch(
24952495
start_after: Optional[Mapping[str, Any]] = None,
24962496
comment: Optional[Any] = None,
24972497
full_document_before_change: Optional[str] = None,
2498+
show_expanded_events: Optional[bool] = None,
24982499
) -> CollectionChangeStream[_DocumentType]:
24992500
"""Watch changes on this collection.
25002501
@@ -2579,12 +2580,16 @@ def watch(
25792580
This option and `resume_after` are mutually exclusive.
25802581
- `comment` (optional): A user-provided comment to attach to this
25812582
command.
2583+
- `show_expanded_events` (optional): Include expanded events such as DDL events like `dropIndexes`.
25822584
25832585
:Returns:
25842586
A :class:`~pymongo.change_stream.CollectionChangeStream` cursor.
25852587
2588+
.. versionchanged:: 4.3
2589+
Added `show_expanded_events` parameter.
2590+
25862591
.. versionchanged:: 4.2
2587-
Added ``full_document_before_change`` parameter.
2592+
Added ``full_document_before_change`` parameter.
25882593
25892594
.. versionchanged:: 4.1
25902595
Added ``comment`` parameter.
@@ -2615,6 +2620,7 @@ def watch(
26152620
start_after,
26162621
comment,
26172622
full_document_before_change,
2623+
show_expanded_events,
26182624
)
26192625

26202626
@_csot.apply

pymongo/database.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ def watch(
547547
start_after: Optional[Mapping[str, Any]] = None,
548548
comment: Optional[Any] = None,
549549
full_document_before_change: Optional[str] = None,
550+
show_expanded_events: Optional[bool] = None,
550551
) -> DatabaseChangeStream[_DocumentType]:
551552
"""Watch changes on this database.
552553
@@ -624,10 +625,14 @@ def watch(
624625
This option and `resume_after` are mutually exclusive.
625626
- `comment` (optional): A user-provided comment to attach to this
626627
command.
628+
- `show_expanded_events` (optional): Include expanded events such as DDL events like `dropIndexes`.
627629
628630
:Returns:
629631
A :class:`~pymongo.change_stream.DatabaseChangeStream` cursor.
630632
633+
.. versionchanged:: 4.3
634+
Added `show_expanded_events` parameter.
635+
631636
.. versionchanged:: 4.2
632637
Added ``full_document_before_change`` parameter.
633638
@@ -657,6 +662,7 @@ def watch(
657662
start_after,
658663
comment,
659664
full_document_before_change,
665+
show_expanded_events=show_expanded_events,
660666
)
661667

662668
def _command(

pymongo/mongo_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@ def watch(
895895
start_after: Optional[Mapping[str, Any]] = None,
896896
comment: Optional[Any] = None,
897897
full_document_before_change: Optional[str] = None,
898+
show_expanded_events: Optional[bool] = None,
898899
) -> ChangeStream[_DocumentType]:
899900
"""Watch changes on this cluster.
900901
@@ -972,10 +973,14 @@ def watch(
972973
This option and `resume_after` are mutually exclusive.
973974
- `comment` (optional): A user-provided comment to attach to this
974975
command.
976+
- `show_expanded_events` (optional): Include expanded events such as DDL events like `dropIndexes`.
975977
976978
:Returns:
977979
A :class:`~pymongo.change_stream.ClusterChangeStream` cursor.
978980
981+
.. versionchanged:: 4.3
982+
Added `show_expanded_events` parameter.
983+
979984
.. versionchanged:: 4.2
980985
Added ``full_document_before_change`` parameter.
981986
@@ -1005,6 +1010,7 @@ def watch(
10051010
start_after,
10061011
comment,
10071012
full_document_before_change,
1013+
show_expanded_events=show_expanded_events,
10081014
)
10091015

10101016
@property

0 commit comments

Comments
 (0)