WordPress / openverse-api

The Openverse API allows programmatic access to search for CC-licensed and public domain digital media.
https://api.openverse.engineering/v1
MIT License
76 stars 50 forks source link

Add database connectivity to healthcheck endpoint #1083

Closed krysal closed 1 year ago

krysal commented 1 year ago

Fixes

Fixes #1079 by @sarayourfriend

Description

As the issue suggest, this PR adds the check using django.db.connection.ensure_connection(). It's actually hard to find official documentation about it (or is just me that didn't find it) but I think it's used in the expected way.

Not sure how to test a bad connection here though. I tried two approaches but the test do not fail in those cases. As it's basically just a call to a django function I assume it should work but it'd be nice to have this case covered just in case.

Attemp 1 ```python # api/test/unit/views/health_views_test.py from django.test import override_settings @override_settings(DATABASES=None) @pytest.mark.django_db def test_health_check_catch_bad_db_connection(api_client): res = api_client.get("/healthcheck/") assert res.status_code != 200 # fails ```
Attemp 2 ```python # api/test/unit/views/health_views_test.py from django.db import connection @pytest.mark.django_db def test_health_check_catch_bad_db_connection(api_client): connection.creation.destroy_test_db() res = api_client.get("/healthcheck/") assert res.status_code != 200 # fails ```

Testing Instructions

Checklist

[best_practices]: https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines

Developer Certificate of Origin

Developer Certificate of Origin ``` Developer Certificate of Origin Version 1.1 Copyright (C) 2004, 2006 The Linux Foundation and its contributors. 1 Letterman Drive Suite D4700 San Francisco, CA, 94129 Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Developer's Certificate of Origin 1.1 By making a contribution to this project, I certify that: (a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or (b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or (c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it. (d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved. ```
github-actions[bot] commented 1 year ago

API Developer Docs Preview: Ready

https://wordpress.github.io/openverse-api/_preview/1083

Please note that GitHub pages takes a little time to deploy newly pushed code, if the links above don't work or you see old versions, wait 5 minutes and try again.

You can check the GitHub pages deployment action list to see the current status of the deployments.

openverse-bot commented 1 year ago

Based on the medium urgency of this PR, the following reviewers are being gently reminded to review this PR:

@dhruvkb This reminder is being automatically generated due to the urgency configuration.

Excluding weekend[^1] days, this PR was updated 4 day(s) ago. PRs labelled with medium urgency are expected to be reviewed within 4 weekday(s)[^2].

@krysal, if this PR is not ready for a review, please draft it to prevent reviewers from getting further unnecessary pings.

[^1]: Specifically, Saturday and Sunday. [^2]: For the purpose of these reminders we treat Monday - Friday as weekdays. Please note that the that generates these reminders runs at midnight UTC on Monday - Friday. This means that depending on your timezone, you may be pinged outside of the expected range.