Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix copied doc updates not insert #4729

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions fiftyone/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7773,7 +7773,12 @@ def _clone_dataset_or_view(dataset_or_view, name, persistent):

dataset._reload()

_id = ObjectId()
#
# Clone dataset document
#

dataset_doc = dataset._doc.copy_with_new_id()
_id = dataset_doc.id

sample_collection_name = _make_sample_collection_name(_id)

Expand All @@ -7784,13 +7789,6 @@ def _clone_dataset_or_view(dataset_or_view, name, persistent):
else:
frame_collection_name = None

#
# Clone dataset document
#

dataset_doc = dataset._doc.copy()

dataset_doc.id = _id
dataset_doc.name = name
dataset_doc.slug = slug
dataset_doc.created_at = datetime.utcnow()
Expand Down Expand Up @@ -8251,14 +8249,12 @@ def _clone_extras(src_dataset, dst_dataset):


def _clone_reference_doc(ref_doc):
_ref_doc = ref_doc.copy()
_ref_doc.id = ObjectId()
_ref_doc = ref_doc.copy_with_new_id()
return _ref_doc


def _clone_run(run_doc):
_run_doc = run_doc.copy()
_run_doc.id = ObjectId()
_run_doc = run_doc.copy_with_new_id()
_run_doc.results = None

# Unfortunately the only way to copy GridFS files is to read-write them...
Expand Down
15 changes: 15 additions & 0 deletions fiftyone/core/odm/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from copy import deepcopy
import json

import bson
from bson import json_util, ObjectId
import mongoengine
from pymongo import InsertOne, UpdateOne
Expand Down Expand Up @@ -583,6 +584,20 @@ class Document(BaseDocument, mongoengine.Document):
def _doc_name(cls):
return "Document"

def copy_with_new_id(self):
"""Returns a copy of this document with a new ID"""
doc_copy = self.copy()
new_id = bson.ObjectId()

# pylint: disable=no-member
id_field = self._meta.get("id_field", "id")
doc_copy.set_field(id_field, new_id)

# Setting _created explicitly as True because we know this is a new
# document
doc_copy._created = True
return doc_copy

def reload(self, *fields, **kwargs):
"""Reloads the document from the database.

Expand Down
25 changes: 25 additions & 0 deletions tests/unittests/odm_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from bson import ObjectId

import fiftyone as fo
import fiftyone.core.odm as foo


class ColorSchemeTests(unittest.TestCase):
Expand All @@ -29,3 +30,27 @@ def test_color_scheme_serialization(self):

self.assertIsInstance(d["_id"], dict)
assert color_scheme == also_color_scheme


class DocumentTests(unittest.TestCase):
def test_doc_copy_with_new_id(self):
dataset_doc = foo.DatasetDocument(
name="unique",
slug="unique",
sample_collection_name="samples.unique",
version="51.51",
)
dataset_doc.save()

# Copy with new ID -- ID should be new, _created should be True
doc_copy = dataset_doc.copy_with_new_id()
self.assertNotEqual(
dataset_doc.get_field("id"), doc_copy.get_field("id")
)
self.assertTrue(doc_copy._created)

# Now if we set ID to be same, the doc should be the same
doc_copy.set_field("id", dataset_doc.get_field("id"))
self.assertEqual(doc_copy, dataset_doc)

dataset_doc.delete()
Loading