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

Add constraint copying to column extration logic #3168

Merged
merged 2 commits into from Aug 25, 2023

Conversation

mathemancer
Copy link
Contributor

Fixes #3160

This adds logic to correctly copy foreign key constraints when extracting columns to a new table.

Technical details

This logic was lost when originally porting the column extraction to SQL. We missed it due to the fact that our new constraint logic operates almost entirely on the 'table constraint' level, whereas the previous python logic sometimes operated on the 'column constraint' level.

I think the new paradigm of 'everything is a table constraint' will make things more consistent moving forward, avoiding this kind of problem in the future.

Sadly, I had to comment out the same tests here that I did in my PR #3156 . This will probably cause merge conflicts when we merge this back into develop.

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.

@mathemancer mathemancer added priority: urgent Blocks other ongoing work pr-status: review A PR awaiting review labels Aug 24, 2023
@mathemancer mathemancer added this to the v0.1.3 milestone Aug 24, 2023
Copy link
Contributor

@silentninja silentninja 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! I made some comments related to readability and should not block this PR as the requests are related to broader concerns. Please feel free to merge this PR.

column_defs := msar.process_col_def_jsonb(null, col_defs, false, true);
constraint_defs := msar.process_con_def_jsonb(null, con_defs);
column_defs := msar.process_col_def_jsonb(0, col_defs, false, true);
constraint_defs := msar.process_con_def_jsonb(0, con_defs);
Copy link
Contributor

Choose a reason for hiding this comment

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

I really got confused as the function name is very similar to msar.process_col_def_jsonb. I would prefer to call it as msar.process_column_def. Just wanted to put my thoughts out, no need to take any action 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 recognize that in this case the abbreviations are a little confusing. I'd rather make more distinguishable abbreviations in this case rather than lengthening the function names. As described elsewhere, horizontal space is at a premium for function signatures (including names). I'll try to think of something clearer.

@@ -2333,7 +2369,7 @@ BEGIN
msar.get_relation_namespace_oid(tab_id),
new_tab_name,
extracted_col_defs,
Copy link
Contributor

Choose a reason for hiding this comment

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

Now I see why I missed this bug when reviewing the column moving logic PR. I was assuming extracted_col_defs contained constraints too.

msar.process_col_def_jsonb actually handles NOT NULL constraint because it cannot be added as a table constraint. Handling constraints in two different places is pesky. We should add comments to the msar.process_col_def_jsonb function to clear up the confusion

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 I described above, the problem is actually that we're dealing with constraints in both ways in Python, and I wanted to unify it to table-only constraints. Irritatingly, NOT NULL isn't really implemented as a constraint at all in PostgreSQL. It's a column property (like its type or default). I.e., the nullability of a column is stored as a boolean in the pg_attribute table, not as a row in the pg_constraint table. So the fact that you can't store a null value in a given column is treated the same as the fact that you can't store a value of 'abc' in a numeric column. However, SQLAlchemy, numerous online resources, and even the PostgreSQL docs often call NOT NULL a constraint, leading to this seeming inconsistency, and lots of confusion all around. Quite irritating.

I'll try to add some comments to that effect, though.

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.

LGMT!

@@ -2324,6 +2359,7 @@ The extraction takes a set of columns from the table, and creates a new table fr
*/
DECLARE
extracted_col_defs CONSTANT jsonb := msar.get_extracted_col_def_jsonb(tab_id, col_ids);
extracted_con_defs CONSTANT jsonb := msar.get_extracted_con_def_jsonb(tab_id, col_ids);
fkey_name CONSTANT text := msar.build_unique_fkey_column_name(tab_id, fk_col_name, new_tab_name);
Copy link
Member

Choose a reason for hiding this comment

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

Just a minor nitpick this name got me confused as to if we were creating a unique foreign key i.e a one-to-one link instead of a many-to-one link between the extracted tables.

@mathemancer mathemancer merged commit 9fd15fb into 0.1.3 Aug 25, 2023
11 checks passed
@mathemancer mathemancer deleted the fkey_regression_bufix branch August 25, 2023 13:21
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 priority: urgent Blocks other ongoing work
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants