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

Implement MergeOption as an option #4851

Merged
merged 11 commits into from
Apr 10, 2018

Conversation

chemelnucfin
Copy link
Contributor

@chemelnucfin chemelnucfin commented Feb 7, 2018

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

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Feb 7, 2018
@chemelnucfin chemelnucfin 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
@chemelnucfin chemelnucfin force-pushed the firestore_merge_option branch from 77795e1 to 2efc83f Compare February 7, 2018 22:35
@chemelnucfin chemelnucfin reopened this Feb 8, 2018
@chemelnucfin chemelnucfin force-pushed the firestore_merge_option branch from 2efc83f to 16c9588 Compare February 8, 2018 00:20
@chemelnucfin chemelnucfin removed the type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. label Feb 8, 2018
@chemelnucfin chemelnucfin force-pushed the firestore_merge_option branch from 16c9588 to c5e2ed4 Compare February 8, 2018 04:33
@chemelnucfin chemelnucfin requested a review from tseaver as a code owner February 8, 2018 04:33
@googlebot googlebot 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
@chemelnucfin chemelnucfin force-pushed the firestore_merge_option branch from c5e2ed4 to 03dc67f Compare February 8, 2018 04:35
@chemelnucfin chemelnucfin 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
@googleapis googleapis deleted a comment from googlebot Feb 8, 2018
@chemelnucfin chemelnucfin changed the title Firestore merge option Implement MergeOption as an option Feb 20, 2018
@chemelnucfin chemelnucfin self-assigned this Feb 20, 2018
@chemelnucfin chemelnucfin force-pushed the firestore_merge_option branch 2 times, most recently from 08f0ee5 to e0d9b28 Compare March 25, 2018 16:52
@@ -768,6 +763,22 @@ def get_doc_id(document_pb, expected_prefix):
return document_id


def get_field_paths(update_data):

This comment was marked as spam.

This comment was marked as spam.

@@ -880,15 +891,16 @@ def pbs_for_set(document_path, document_data, option):
or two ``Write`` protobuf instances for ``set()``.
"""
transform_paths, actual_data = remove_server_timestamp(document_data)

update_pb = write_pb2.Write(
update=document_pb2.Document(
name=document_path,
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.



DEFAULT_DATABASE = '(default)'
"""str: The default database used in a :class:`~.firestore.client.Client`."""
_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 chemelnucfin force-pushed the firestore_merge_option branch 4 times, most recently from 7eec589 to 1af5b12 Compare March 28, 2018 18:57
@chemelnucfin chemelnucfin force-pushed the firestore_merge_option branch 3 times, most recently from fa1e5d0 to 53159e9 Compare April 6, 2018 14:51
@chemelnucfin chemelnucfin force-pushed the firestore_merge_option branch from 2ea0832 to 93f0441 Compare April 6, 2018 18:57
@chemelnucfin chemelnucfin force-pushed the firestore_merge_option branch from 93f0441 to 138111b Compare April 6, 2018 19:00
Copy link

@schmidt-sebastian schmidt-sebastian left a comment

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.

@@ -39,8 +39,8 @@
DEFAULT_DATABASE = '(default)'
"""str: The default database used in a :class:`~.firestore.client.Client`."""
_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.


# 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.

snapshot = document.get()
assert not snapshot.exists

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

This comment was marked as spam.

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

This comment was marked as spam.

elif kind == "set":
tp = test_proto.set
client, doc = self.setup(firestore_api, tp)
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 chemelnucfin force-pushed the firestore_merge_option branch from 10d612b to b166f06 Compare April 6, 2018 20:39
@tseaver tseaver merged commit dd4b646 into googleapis:master Apr 10, 2018
@chemelnucfin chemelnucfin deleted the firestore_merge_option branch April 10, 2018 19:18
@mikedh
Copy link

mikedh commented Oct 16, 2018

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
Contributor

tseaver commented Oct 16, 2018

@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

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

@mikedh
Copy link

mikedh commented Oct 16, 2018

Thanks! The API change seems like an improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the Firestore API. cla: yes This 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