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

Improve exception handling while failing to connect to a database #3388

Merged
merged 8 commits into from
Jan 16, 2024

Conversation

Anish9901
Copy link
Member

@Anish9901 Anish9901 commented Jan 4, 2024

Fixes #3329
Fixes #3361 (part 2)
Fixes #3383

Technical details

This PR globally catches any OperationalError that occurs throughout the app. This error is generally caused when an attempt to connect to a database fails, due to reasons such as invalid credentials, db deleted outside mathesar, etc.

Example error message: (psycopg2.OperationalError) connection to server at "mathesar_dev_db" (172.18.0.2), port 5435 failed: Connection refused\n\tIs the server running on that host and accepting TCP/IP connections?

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the develop branch of the repository
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no
    visible errors.

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.

@Anish9901 Anish9901 requested a review from mathemancer January 4, 2024 20:54
@Anish9901 Anish9901 added the pr-status: review A PR awaiting review label Jan 4, 2024
@Anish9901 Anish9901 added this to the v0.1.4 milestone Jan 4, 2024
Copy link
Contributor

@mathemancer mathemancer left a comment

Choose a reason for hiding this comment

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

Won't the rearrangement of the install functions (i.e., by moving the save step after the installation step) leave the possibility of inconsistencies? I.e., if we can't save the model, there could be a problem where the user ends up with Mathesar tooling installed on their user DB, but no working connection to that DB. I'd rather have it the other way around. I.e., we should save, then install, then roll back the save if the installation fails.

@mathemancer mathemancer added pr-status: revision A PR awaiting follow-up work from its author after review and removed pr-status: review A PR awaiting review labels Jan 12, 2024
@mathemancer mathemancer removed their assignment Jan 12, 2024
@Anish9901
Copy link
Member Author

we should save, then install, then roll back the save if the installation fails.

This does seem more robust than my previous implementation, I've made the changes accordingly.

Won't the rearrangement of the install functions (i.e., by moving the save step after the installation step) leave the possibility of inconsistencies?

I don't think so, we do require an internal database connection at the start for our Django server to make migrations on the target database if the django_db_url is not configured properly the app won't even start. The only way I can think of this happening is when django server is successfully started, then the pg server on which the internal db is hosted is shut down/ not accepting connections and then there is an attempt to add a connection via the UI. I am curious to know what other scenarios you envision when model instances might fail to save?

@Anish9901 Anish9901 assigned mathemancer and unassigned Anish9901 Jan 15, 2024
@Anish9901 Anish9901 added pr-status: review A PR awaiting review and removed pr-status: revision A PR awaiting follow-up work from its author after review labels Jan 15, 2024
@mathemancer
Copy link
Contributor

we do require an internal database connection at the start for our Django server to make migrations on the target database if the django_db_url is not configured properly the app won't even start. The only way I can think of this happening is when django server is successfully started, then the pg server on which the internal db is hosted is shut down/ not accepting connections and then there is an attempt to add a connection via the UI. I am curious to know what other scenarios you envision when model instances might fail to save?

Hmmm That's a really good point that the user will definitely at least have some working connection, or the install step would already fail. I was thinking about uniqueness violations resulting in the model instance failing to save. The nickname should be unique I think.

Copy link
Contributor

@mathemancer mathemancer left a comment

Choose a reason for hiding this comment

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

See my specific comment. The db library needs to stand alone, and shouldn't know about Django model instances.

db/install.py Outdated
password,
hostname,
port,
db_model,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather avoid having Django classes leak down into the db library. Eventually, we want to be able to ship this code separately, and we don't want users of that code having to spoof some model class to use this function. (Especially this function, since it's really useful in a python script).

Copy link
Member Author

Choose a reason for hiding this comment

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

That makes sense. I've reverted the function signature.

@mathemancer mathemancer assigned Anish9901 and unassigned mathemancer Jan 16, 2024
@mathemancer mathemancer added pr-status: revision A PR awaiting follow-up work from its author after review and removed pr-status: review A PR awaiting review labels Jan 16, 2024
@Anish9901 Anish9901 requested a review from mathemancer January 16, 2024 13:10
@Anish9901 Anish9901 added pr-status: review A PR awaiting review and removed pr-status: revision A PR awaiting follow-up work from its author after review labels Jan 16, 2024
@Anish9901 Anish9901 assigned mathemancer and unassigned Anish9901 Jan 16, 2024
Copy link
Contributor

@mathemancer mathemancer left a comment

Choose a reason for hiding this comment

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

Okay, I think we're in business here. Nice work, @Anish9901 .

@mathemancer mathemancer added this pull request to the merge queue Jan 16, 2024
Merged via the queue into develop with commit d2e9603 Jan 16, 2024
19 checks passed
@mathemancer mathemancer deleted the invalid_db_conn branch January 16, 2024 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-status: review A PR awaiting review
Projects
None yet
2 participants