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

added: exclusion violation custom error #3200

Merged
merged 17 commits into from Dec 4, 2023

Conversation

manthan-jsharma
Copy link
Contributor

@manthan-jsharma manthan-jsharma commented Sep 8, 2023

Fixes #2126

Technical details

Screenshots

Screenshot 2023-09-08 at 11 48 10 PM

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.

dmos62

This comment was marked as outdated.

@rajatvijay rajatvijay added the pr-status: revision A PR awaiting follow-up work from its author after review label Sep 22, 2023
@rajatvijay rajatvijay added this to the Backlog milestone Sep 22, 2023
@rajatvijay

This comment was marked as resolved.

@manthan-jsharma

This comment was marked as resolved.

Aritra8438

This comment was marked as resolved.

dmos62
dmos62 previously requested changes Oct 3, 2023
Copy link
Contributor

@dmos62 dmos62 left a comment

Choose a reason for hiding this comment

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

Looking better! Yes, you want a test for this. I wouldn't bother using a CSV file. Create the fixture you need via psycopg calls. Read other fixtures for inspiration.

@seancolsen
Copy link
Contributor

@manthan-jsharma Are you still planning to work on this?

@manthan-jsharma
Copy link
Contributor Author

Certainly, I will be completing it. Currently, I am in the process of understanding how to import CSV files for the test cases. It would be helpful if you could provide guidance on this matter. Apart from that, the work is nearly finished.

@seancolsen
Copy link
Contributor

@manthan-jsharma

It would be helpful if you could provide guidance on this matter

If you need help, please ask specific questions, taking care to explain exactly what problem you're having. Be clear about what steps you've taken, what expectation you have, and what observations you've made. If your questions are specific to the changes in this PR or are about implementation strategy, then the best place to ask is within this PR thread. However if your questions are more general questions about development processes and workflows, then it's probably better to ask in Matrix

@manthan-jsharma
Copy link
Contributor Author

Hello @seancolsen and @dmos62,

I have implemented the custom message for the exclusion violation, added room_reservation.csv for the test case, and made an effort to include the corresponding test case. However, I'm currently facing challenges in writing the class for the ExclusionViolation constraint, and this is the only remaining part.

The code for the test case has been pushed in this commit: Link to the commit.

@seancolsen and @dmos62, could you please guide me on how to write the expression for the exclusion constraint in this section of the code: Link to the code? Additionally, I am unsure if it is even possible to add the exclusion constraint using SQLAlchemy version 1.4.26. I couldn't find relevant information in the documentation; here is the link to the documentation: SQLAlchemy Documentation.

@seancolsen seancolsen 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 Nov 13, 2023
@seancolsen
Copy link
Contributor

@manthan-jsharma, @dmos62 will no longer be working on this ticket. I'm re-assigning this ticket to @Anish9901 who I hope will be able to answer your questions.

Copy link
Member

@Anish9901 Anish9901 left a comment

Choose a reason for hiding this comment

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

Hey @manthan-jsharma, I have requested some changes and addressed your queries in the code review please take a look at the specific comments.

@@ -64,3 +64,20 @@ def get_constraint_def_json(self):
}
]
)


class ExclusionViolation(Constraint):
Copy link
Member

Choose a reason for hiding this comment

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

This class won't be necessary for now, the constraint classes that are defined in this file are the ones that we support , meaning, we allow DDL operations like CREATE & DROP and DQL operations like SELECT. The issue you are solving is not to add support for this constraint but to handle any errors properly if violation for this constraint does occur on a table which has EXCLUSION constraint on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've removed the class

Comment on lines 422 to 425
except TypeError:
details = {
"constraint": None,
}
Copy link
Member

Choose a reason for hiding this comment

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

When does this occur?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've just handled an edge case. This code will only execute in the event of a failure within the try block.

Copy link
Member

@Anish9901 Anish9901 Nov 28, 2023

Choose a reason for hiding this comment

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

Shouldn't that be handled using Exception as it a base class for all the python exceptions instead of TypeError? The TypeError you have used here is a python type error and not a database type error.

Comment on lines 432 to 436
exception_detail = get_default_exception_detail(
exception, self.error_code, message, field, details
)._asdict()
self.detail = [exception_detail]
self.status_code = status_code
Copy link
Member

Choose a reason for hiding this comment

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

use super() instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is done, I've used super here

Comment on lines 1474 to 1478
# table.add_constraint(
# ExclusionViolation(
# what parameters should i pass ?
# )
# )
Copy link
Member

Choose a reason for hiding this comment

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

You could just use raw SQL to add the required constraint for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've attempted several SQL queries, but none of them seems to be working successful. Additionally, I couldn't find any examples in the documentation on how to add exclusion constraints using SQL. Could you please provide guidance on adding exclusion constraints in SQL here @Anish9901

Copy link
Member

Choose a reason for hiding this comment

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

Try something similar to the SQL provided in the issue(#2126)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed, i am sharing the solution but it did not work.

  1. tried the query by directly using conn.execute, but got an error that TSRANGE is not defined.
  2. tried by not using text from sqlalchemy but got an internal error from sqlalchemy library.

Copy link
Member

Choose a reason for hiding this comment

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

You are facing this problem because the data type of the columns of the CSV are getting inferred as TEXT instead of DATE

@@ -1464,3 +1464,33 @@ def test_record_patch_unique_violation(create_patents_table, client):
f'/api/db/v0/tables/{table.id}/constraints/{constraint_id}/'
).json()
assert actual_constraint_details['name'] == 'NASA unique record PATCH_pkey'


def test_record_patch_exclusion_violation(room_reservations_table, client):
Copy link
Member

Choose a reason for hiding this comment

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

Please create a test case for POST as well.

@Anish9901 Anish9901 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 Nov 15, 2023
@Anish9901
Copy link
Member

@manthan-jsharma I've added 12aa7e8 that takes care of the issues you were facing and the commit also include some general cleanups. Let me know if you have any concerns.

@Anish9901 Anish9901 dismissed stale reviews from Aritra8438 and dmos62 December 4, 2023 23:43

Outdated

@Anish9901 Anish9901 added this pull request to the merge queue Dec 4, 2023
Merged via the queue into mathesar-foundation:develop with commit 1644ec5 Dec 4, 2023
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-status: revision A PR awaiting follow-up work from its author after review
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Unhandled error for exclusion constraint violation
6 participants