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

chore(sqlalchemy): Remove erroneous SQLAlchemy ORM session.merge operations #24776

Merged

Conversation

john-bodley
Copy link
Member

@john-bodley john-bodley commented Jul 22, 2023

SUMMARY

This PR addresses a non-controversial observation from [SIP-99A] Primer on managing SQLAlchemy sessions where we have a tendency to unnecessarily re-merge objects into the SQLAlchemy session which are already present in the seession.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

CI.

ADDITIONAL INFORMATION

  • Has associated issue: [SIP-99A] Primer on managing SQLAlchemy sessions #25107
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@john-bodley john-bodley force-pushed the john-bodley--cleanup-sqlalchemy-merge branch 2 times, most recently from 375f79a to f34edc5 Compare July 23, 2023 04:20
@codecov
Copy link

codecov bot commented Jul 23, 2023

Codecov Report

Merging #24776 (6c9deb5) into master (7397ab3) will decrease coverage by 10.49%.
The diff coverage is 21.05%.

❗ Current head 6c9deb5 differs from pull request most recent head 9e6e897. Consider uploading reports for the commit 9e6e897 to get more accurate results

@@             Coverage Diff             @@
##           master   #24776       +/-   ##
===========================================
- Coverage   68.96%   58.48%   -10.49%     
===========================================
  Files        1906     1906               
  Lines       74122    74107       -15     
  Branches     8208     8208               
===========================================
- Hits        51116    43339     -7777     
- Misses      20883    28645     +7762     
  Partials     2123     2123               
Flag Coverage Δ
hive 54.18% <21.05%> (+0.01%) ⬆️
mysql ?
presto 54.08% <21.05%> (+0.01%) ⬆️
python 61.36% <21.05%> (-21.93%) ⬇️
sqlite ?
unit 55.06% <10.52%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
superset/connectors/sqla/models.py 76.40% <ø> (-14.98%) ⬇️
superset/daos/chart.py 68.51% <ø> (-24.21%) ⬇️
superset/daos/dashboard.py 35.71% <ø> (-60.97%) ⬇️
superset/daos/report.py 43.24% <ø> (-40.69%) ⬇️
superset/examples/bart_lines.py 0.00% <0.00%> (ø)
superset/examples/country_map.py 0.00% <ø> (ø)
superset/examples/deck.py 0.00% <0.00%> (ø)
superset/examples/energy.py 0.00% <0.00%> (ø)
superset/examples/flights.py 0.00% <0.00%> (ø)
superset/examples/long_lat.py 0.00% <0.00%> (ø)
... and 14 more

... and 282 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@john-bodley john-bodley force-pushed the john-bodley--cleanup-sqlalchemy-merge branch from f34edc5 to 5b429b3 Compare July 23, 2023 04:38
@john-bodley john-bodley force-pushed the john-bodley--cleanup-sqlalchemy-merge branch from 5b429b3 to 9e6e897 Compare August 4, 2023 22:47
@john-bodley john-bodley changed the title chore(sqlalchemy): Remove erroneous SQLAlchemy ORM session merge chore(sqlalchemy): Remove erroneous SQLAlchemy ORM session.merge Aug 4, 2023
@john-bodley john-bodley force-pushed the john-bodley--cleanup-sqlalchemy-merge branch from 9e6e897 to 0c55386 Compare August 29, 2023 17:59
@@ -141,10 +141,9 @@ def upgrade_slice(cls, slc: Slice) -> Slice:
if "form_data" in (query_context := try_load_json(slc.query_context)):
query_context["form_data"] = clz.data
slc.query_context = json.dumps(query_context)
return slc
Copy link
Member Author

Choose a reason for hiding this comment

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

Previously this was returning the same object as the one being passed in.

@john-bodley john-bodley force-pushed the john-bodley--cleanup-sqlalchemy-merge branch 3 times, most recently from 9e5d609 to 1b61a9e Compare August 31, 2023 23:11
@@ -326,7 +326,8 @@ def virtual_dataset():
TableColumn(column_name="col5", type="VARCHAR(255)", table=dataset)

SqlMetric(metric_name="count", expression="count(*)", table=dataset)
db.session.merge(dataset)
db.session.add(dataset)
db.session.commit()
Copy link
Member Author

@john-bodley john-bodley Aug 31, 2023

Choose a reason for hiding this comment

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

There's likely a lot wrong happening here, but committing seems consistent with the logic on lines 334-335.

@john-bodley john-bodley force-pushed the john-bodley--cleanup-sqlalchemy-merge branch from 1b61a9e to edc8500 Compare August 31, 2023 23:40
@john-bodley john-bodley marked this pull request as ready for review September 1, 2023 17:27
@john-bodley john-bodley requested a review from a team as a code owner September 1, 2023 17:27
@john-bodley
Copy link
Member Author

ping @michael-s-molina

@@ -87,7 +87,6 @@ def update(self) -> Optional[Key]:
entry.expires_on = self.expires_on
entry.changed_on = datetime.now()
entry.changed_by_fk = get_user_id()
db.session.merge(entry)
Copy link
Member

Choose a reason for hiding this comment

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

Do you know if the autoflush(False) when querying for the model influences anything?

Copy link
Member Author

Choose a reason for hiding this comment

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

@michael-s-molina I'm not entirely sure why auto-flushing has been disabled here (and elsewhere in the key/value commands). Maybe @villebro can provide some context as this was added in #19078.

Copy link
Member Author

Choose a reason for hiding this comment

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

@michael-s-molina I removed the unnecessary auto-flush logic in #26009.

@john-bodley john-bodley force-pushed the john-bodley--cleanup-sqlalchemy-merge branch from 46ffd85 to 77ef809 Compare November 17, 2023 03:52
@john-bodley john-bodley changed the title chore(sqlalchemy): Remove erroneous SQLAlchemy ORM session.merge chore(sqlalchemy): Remove erroneous SQLAlchemy ORM session.merge operations Nov 17, 2023
Copy link
Member

@michael-s-molina michael-s-molina left a comment

Choose a reason for hiding this comment

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

LGTM

@john-bodley john-bodley merged commit dd58b31 into apache:master Nov 21, 2023
2 checks passed
josedev-union pushed a commit to Ortege-xyz/studio that referenced this pull request Jan 22, 2024
cccs-rc pushed a commit to CybercentreCanada/superset that referenced this pull request Mar 6, 2024
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 3.1.0 labels Mar 8, 2024
sfirke pushed a commit to sfirke/superset that referenced this pull request Mar 22, 2024
vinothkumar66 pushed a commit to vinothkumar66/superset that referenced this pull request Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/L 🚢 3.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants