Skip to content

Implement MergeOption as an option - #4851

Merged
tseaver merged 11 commits into
googleapis:masterfrom
chemelnucfin:firestore_merge_option
Apr 10, 2018
Merged

Implement MergeOption as an option#4851
tseaver merged 11 commits into
googleapis:masterfrom
chemelnucfin:firestore_merge_option

Conversation

@chemelnucfin

@chemelnucfinchemelnucfin commented Feb 7, 2018

Copy link
Copy Markdown
Contributor

Should close #4111
The first and second commits is #4654.

@googlebotgooglebot added the cla: yes This human has signed the Contributor License Agreement. label Feb 7, 2018
@chemelnucfinchemelnucfin added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. api: firestore Issues related to the Firestore API. labels Feb 7, 2018
@chemelnucfinchemelnucfin removed the type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. label Feb 8, 2018
@googlebotgooglebot added cla: no This human has *not* signed the Contributor License Agreement. and removed cla: yes This human has signed the Contributor License Agreement. labels Feb 8, 2018
@chemelnucfinchemelnucfin added cla: yes This human has signed the Contributor License Agreement. and removed cla: no This human has *not* signed the Contributor License Agreement. labels Feb 8, 2018
@googleapisgoogleapis deleted a comment from googlebotFeb 8, 2018
@chemelnucfinchemelnucfin changed the title Firestore merge optionImplement MergeOption as an optionFeb 20, 2018
@chemelnucfinchemelnucfin self-assigned this Feb 20, 2018
@chemelnucfin
chemelnucfinforce-pushed the firestore_merge_option branch 2 times, most recently from 08f0ee5 to e0d9b28CompareMarch 25, 2018 16:52
return document_id


def get_field_paths(update_data):

This comment was marked as spam.

This comment was marked as spam.

fields=encode_dict(actual_data),
),
)
if option is not None:

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

_BAD_OPTION_ERR = (
'Exactly one of ``create_if_missing``, ``last_update_time`` '
'and ``exists`` must be provided.')
'Exactly one of ``last_update_time`` or ``exists`` must be provided.')

This comment was marked as spam.

This comment was marked as spam.

elif not self._create_if_missing:
current_doc = types.Precondition(exists=True)
write_pb.current_document.CopyFrom(current_doc)
mask = common_pb2.DocumentMask(field_paths=sorted(field_paths))

This comment was marked as spam.

This comment was marked as spam.

@chemelnucfin
chemelnucfinforce-pushed the firestore_merge_option branch 4 times, most recently from 7eec589 to 1af5b12CompareMarch 28, 2018 18:57
@chemelnucfin
chemelnucfinforce-pushed the firestore_merge_option branch 2 times, most recently from fa1e5d0 to 53159e9CompareApril 6, 2018 14:51
@chemelnucfin
chemelnucfinforce-pushed the firestore_merge_option branch from 2ea0832 to 93f0441CompareApril 6, 2018 18:57
@chemelnucfin
chemelnucfinforce-pushed the firestore_merge_option branch from 93f0441 to 138111bCompareApril 6, 2018 19:00

@schmidt-sebastianschmidt-sebastian left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty close from a functional perspective. Some comments (mostly small nits) remaining.

Can you send this over to someone that understands Python after you resolved these last comments? Thanks!

for sub_path in sub_transform_paths:
field_path = FieldPath.from_string(field_name)
field_path.parts = field_path.parts + sub_path.parts
transform_paths.extend([field_path])

This comment was marked as spam.

if split_on_dots:
transform_paths.append(FieldPath(*field_name.split(".")))
else:
transform_paths.append(FieldPath.from_string(field_name))

This comment was marked as spam.

else:
return field_paths, document_data
field_paths.append(FieldPath(field_name))
if not transform_paths:

This comment was marked as spam.

This comment was marked as spam.

_BAD_OPTION_ERR = (
'Exactly one of ``create_if_missing``, ``last_update_time`` '
'and ``exists`` must be provided.')
'Exactly one of ``last_update_time``, ``exists`` '

This comment was marked as spam.

Comment threadfirestore/tests/system.py Outdated
assert snapshot.to_dict() is None

# 1. Use ``set()`` to create the document (using an option).
# 1. Use ``create()`` to create the document (using an option).

This comment was marked as spam.


data2 = {'1a.ab': '4d', '6f.7g': '9h'}
option2 = client.write_option(create_if_missing=True)
option2 = client.write_option(exists=True)

This comment was marked as spam.

This comment was marked as spam.

Comment threadfirestore/tests/system.py Outdated
snapshot = document.get()
assert not snapshot.exists

# 1. Use ``set()`` to create the document (using an option).

This comment was marked as spam.

try:
self.run_write_test(test_proto, desc)
except (AssertionError, Exception) as error:
count += 1

This comment was marked as spam.

data = convert_data(json.loads(tp.json_data))
# TODO: call doc.set.
if tp.HasField("option"):
option = True

This comment was marked as spam.

@chemelnucfin
chemelnucfinforce-pushed the firestore_merge_option branch from 10d612b to b166f06CompareApril 6, 2018 20:39
@tseaver
tseaver merged commit dd4b646 into googleapis:masterApr 10, 2018
@chemelnucfin
chemelnucfin deleted the firestore_merge_option branch April 10, 2018 19:18
@tseavertseaver mentioned this pull request Apr 10, 2018
@mikedh

Copy link
Copy Markdown

Hey, this finally made it to Pypi in a release, and unfortunately broke some things for me. This PR removes firestore.CreateIfMissingOption, which is still recommended in the Firebase docs. The docs:

If the document does not exist, it will be created. If the document does exist, its contents will be overwritten with the newly provided data, unless you specify that the data should be merged into the existing document, as follows:
# The option to merge data is not yet available for Python. Instead, call the
# update method and pass the option to create the document if it's missing.
city_ref = db.collection(u'cities').document(u'BJ')
city_ref.update({
u'capital': True
}, firestore.CreateIfMissingOption(True))

It looks like the migration here is (?):

city_ref.update({
u'capital': True}, merge=True)

Given the arg ordering, an ugly backwards compatibility patch could be:

CreateIfMissingOption = lambda x : bool(x)

@tseaver

Copy link
Copy Markdown
Contributor

@mikedh The Firestore API team explicitly asked us to remove CreateIfMissingOption from the API surface of google-cloud-firestore.

@schmidt-sebastian Can you comment here?

@samtstern

Copy link
Copy Markdown

That's the correct API change, just looks like it took a bit longer for it to land in Python than the other SDKs and we didn't time the doc updates with the release. I'll get the docs updated.

@samtstern

Copy link
Copy Markdown

@mikedh

Copy link
Copy Markdown

Thanks! The API change seems like an improvement.

parthea pushed a commit that referenced this pull request Nov 24, 2025
Remove `CreateIfMissing` option
Closes#4111.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: firestoreIssues related to the Firestore API.cla: yesThis human has signed the Contributor License Agreement.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@chemelnucfin@jba@mikedh@tseaver@samtstern@schmidt-sebastian@googlebot