Skip to content
This repository has been archived by the owner on Mar 11, 2022. It is now read-only.

Query index in couchdb and support dict syntax: _id in database #369

Merged
merged 1 commit into from May 7, 2018
Merged

Query index in couchdb and support dict syntax: _id in database #369

merged 1 commit into from May 7, 2018

Conversation

tiangolo
Copy link
Contributor

@tiangolo tiangolo commented Apr 5, 2018

What

I added 2 things, I needed both of them at the same time so I ended up doing a single PR instead of 2, sorry for that.

  • Move create_query_index from CloudantDatabase to CouchDatabase as the JSON indexes for Mango queries are now part of CouchDB.
  • Add a feature to allow checking if a document exists in a DB without having to resort to try-catch blocks, using standard dict syntax:
_id = 'julia30'
doc_exists = _id in my_database

if doc_exists:
    print('doc with _id julia30 exists in DB')

How

  • create_query_index: move the method from one class to the other. Move the tests from Cloudant specific to CouchDB.
  • Add a dict override for __contains__ that checks in the remote DB if the document exists.

Testing

Create a CouchDatabase object and call the create_query_index the same way you would for a Cloudant database.

Issues

I didn't create an issue before solving it.

@emlaver emlaver added this to New in python-cloudant Triage via automation Apr 9, 2018
@ricellis ricellis moved this from New to Review in python-cloudant Triage Apr 10, 2018
Copy link
Member

@emlaver emlaver left a comment

Choose a reason for hiding this comment

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

Moving the Mango index related methods to CouchDatabase requires our Travis CI to run against CouchDB 2.x. Required steps before further review of this PR:

  • Updating the Travis environment (.travis.yml) to use couchdb:2.1.1 or latest docker image
  • Fixing/updating test cases that fail/error against CouchDB 2.x.

Also, I've done a small test and it seems that moving the Mango index related methods to CouchDatabase doesn't break the API for existing users who call at these methods in CloudantDatabase. Please test and confirm.


.. code-block:: python

doc_exists = 'julia30' in my_database
Copy link
Member

Choose a reason for hiding this comment

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

A test case is required for this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done ✔️

@tiangolo
Copy link
Contributor Author

Thanks for the review @emlaver

About setting CouchDB to the latest Docker image, I don't see any documentation on how to specify the version of CouchDB in Travis using simple, standard Travis with services: https://docs.travis-ci.com/user/database-setup/#CouchDB


I know it could be quite simpler to with Docker.

And I see there are branch names in this repo related to migrating the tests to Docker.

Do you need help with some of that? I have "some" experience with Docker, but I don't want to do stuff that doesn't align with the plans of the team.

For example, this project is configured to run tests for a full app in Docker. And run them on Travis (and GitLab CI, etc. It's quite simple to change the CI system once it's all in Docker).

@emlaver
Copy link
Member

emlaver commented Apr 11, 2018

@tiangolo I'm currently working in a branch to use Docker to run our tests against the 2.1.1 version. I'll open a PR and our team with review when it's ready.

@tiangolo
Copy link
Contributor Author

Excellent, thanks for reporting back @emlaver .

BTW, I'm actually running my local tests against a Docker (Docker Compose) CouchDB. Please let me know if there's anything I can help with that.

And let me know if there's anything still missing here (in this PR) that should be fixed before your PR is ready (I already added the missing tests).

@emlaver
Copy link
Member

emlaver commented Apr 24, 2018

@tiangolo I've merged the CouchDB 2.1.1 testing PR. Please rebase your PR.

@tiangolo
Copy link
Contributor Author

Thanks @emlaver , that's great! 🎉

Done, rebased (also fixed an issue I had there) ✔️

@tiangolo
Copy link
Contributor Author

tiangolo commented May 2, 2018

Hi @emlaver , do you have any news about this? 😁


:param str key: Document id used to check if it exists in the database.

:returns: A boolean, True if the document exists in the local or remote
Copy link
Member

Choose a reason for hiding this comment

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

This can be simplified to: True if ..., otherwise False.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done ✅

Overrides dictionary __contains__ behavior to check if a document
by key exists in the current cached or remote database.

This allows using checks like
Copy link
Member

Choose a reason for hiding this comment

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

Replace with For example: to be consistent with other code blocks in comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done ✅

Copy link
Contributor

@smithsz smithsz left a comment

Choose a reason for hiding this comment

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

LGTM

CHANGES.md Outdated
@@ -1,5 +1,7 @@
# Unreleased

- [NEW] Moved `create_query_index` to `CouchDatabase` as it is now implemented there.
Copy link
Member

Choose a reason for hiding this comment

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

Two things:
i) There are a bunch of other index/query related methods that moved as well.
ii) I know you are referring to new versions of CouchDB, but a newbie might not in which case "implemented there" is a bit confusing because it could mean CouchDatabase.

How about:

- [NEW] Moved `create_query_index` and other query related methods to `CouchDatabase` as the `_index`/`_find` API is available in CouchDB 2.x.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done ✅

Copy link
Member

@emlaver emlaver left a comment

Choose a reason for hiding this comment

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

Looks good, please squash your commits down to one commit. You can use your CHANGES entry and any additional info. as the commit message.

…t for 'key in db'

[NEW] Moved `create_query_index` and other query related methods to `CouchDatabase` as the `_index`/`_find` API is available in CouchDB 2.x.
[NEW] Added functionality to test if a key is in a database as in `key in db`, overriding dict `__contains__` and checking in the remote database.
@tiangolo
Copy link
Contributor Author

tiangolo commented May 6, 2018

Thanks @emlaver , done ✅

@emlaver emlaver merged commit 9817315 into cloudant:master May 7, 2018
python-cloudant Triage automation moved this from Review to Done May 7, 2018
@tiangolo
Copy link
Contributor Author

tiangolo commented May 7, 2018

🎉 Thanks!

@tiangolo tiangolo deleted the query_index_in_couchdb branch May 7, 2018 14:49
@emlaver
Copy link
Member

emlaver commented May 7, 2018

@tiangolo Thank you for your work!

@ricellis ricellis added this to the 2.next milestone May 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants