Repository navigation
Description
index-tool: _get_compatible_metadata returns the unfiltered metadata, so database-level incompatibilities are reported and then restored anyway
Summary
DocumentDbIndexTool._get_compatible_metadata builds a filtered copy named compatible_metadata
but returns metadata, the original argument. It also uses a shallow dict.copy(), so the two
mistakes partly cancel out and the bug is easy to miss:
- Collection-level and index-level exclusions appear to work — only because a shallow copy
shares the nested dictionaries, sodel compatible_metadata[db][coll]mutatesmetadatatoo. - Database-level exclusions are silently lost —
del compatible_metadata[db_name]removes the
key from the copy, which is then discarded byreturn metadata.
A database flagged with exceeded_limits (a name longer than
DocumentDbLimits.DATABASE_NAME_MAX_LENGTH, 63 characters) is reported by --show-issues and
then included in the restore regardless, including when --skip-incompatible is passed.
Affected code
index-tool/migrationtools/documentdb_index_tool.py, _get_compatible_metadata (currently lines
148-170 on master):
def _get_compatible_metadata(self, metadata, compatibility_issues):
compatible_metadata = metadata.copy() # shallow
for db_name in compatibility_issues:
if self.EXCEEDED_LIMITS in compatibility_issues[db_name]:
del compatible_metadata[db_name] # removes from the copy only
continue
...
return metadata # returns the originalThe return value is used on both paths that call it — the restore path and --show-compatible.
Reproduction
Standalone, no server needed. The function body is copied verbatim with the class constants
inlined:
EXCEEDED_LIMITS = "exceeded_limits"
UNSUPPORTED_COLLECTION_OPTIONS_KEY = "unsupported_collection_options"
INDEXES = "indexes"
def _get_compatible_metadata(metadata, compatibility_issues):
compatible_metadata = metadata.copy()
for db_name in compatibility_issues:
if EXCEEDED_LIMITS in compatibility_issues[db_name]:
del compatible_metadata[db_name]
continue
for collection_name in compatibility_issues[db_name]:
if UNSUPPORTED_COLLECTION_OPTIONS_KEY in compatibility_issues[db_name][collection_name]:
del compatible_metadata[db_name][collection_name]
continue
if EXCEEDED_LIMITS in compatibility_issues[db_name][collection_name]:
del compatible_metadata[db_name][collection_name]
continue
for index_name in compatibility_issues[db_name][collection_name]:
del compatible_metadata[db_name][collection_name][INDEXES][index_name]
return metadata
too_long = "d" * 64
metadata = {
too_long: {"coll": {INDEXES: {"idx_a": {}}}},
"fine_db": {"coll": {INDEXES: {"idx_b": {}, "idx_c": {}}}},
}
issues = {
too_long: {EXCEEDED_LIMITS: {"Database name greater than 63 characters": too_long}},
"fine_db": {"coll": {"idx_c": {EXCEEDED_LIMITS: {"too many keys": 33}}}},
}
result = _get_compatible_metadata(metadata, issues)
print(too_long in result) # True -- expected False
print("idx_c" in result["fine_db"]["coll"][INDEXES]) # False -- expected False
print("idx_b" in result["fine_db"]["coll"][INDEXES]) # True -- expected True
print(result is metadata) # TrueObserved:
database-level exclusion
over-long database still present : True <-- expected False
index-level exclusion (for contrast)
incompatible idx_c still present : False <-- expected False
compatible idx_b still present : True <-- expected True
returned object is the caller's metadata, not the filtered copy: True
Suggested fix
Both changes are needed, and they have to go in together:
import copy
def _get_compatible_metadata(self, metadata, compatibility_issues):
compatible_metadata = copy.deepcopy(metadata)
...
return compatible_metadataChanging only the return leaves the function mutating its caller's metadata as a side effect.
Changing only the copy to a deep copy would break the collection-level and index-level filtering
that currently works by aliasing. Together they make the function do what its name says and leave
the caller's metadata untouched.
Environment
Reproduced against master as of 2026-11-27, Python 3.12. Found while migrating a
production-shaped MongoDB 8.0 database to Amazon DocumentDB 8.0.3 with
--restore-indexes --skip-incompatible.
Activity