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

db: adapt SopelDB to SQLAlchemy 2.x new style #2243

Merged
merged 2 commits into from
Jul 11, 2022

Conversation

Exirel
Copy link
Contributor

@Exirel Exirel commented Jan 25, 2022

Description

SQLAlchemy 1.4 (used by Sopel) defines a cross-compatible new style to query the database using its ORM, its engine, and its connection:

  • Engine cannot execute SQL directly
  • Connection can be used as context manager
  • Session can be used as context manager
  • Instead of doing *.query, use *.execute(stmt)
  • SQL statements are easier to create with select, update, and delete function from sqlalchemy.sql
  • And many other things

One of the key change here is the usage of session object as context manager instead of using try/except block: less code, same feature!

We can't set future=True on the Engine yet, because we need the non-compatible and now deprecated behavior of direct SQL execution from the Engine object: SopelDB.execute has been deprecated and should be removed in Sopel 8.1, so we can make Sopel compatible with both SQLAlchemy 1.4 and 2.0, as planned by SQLAlchemy migration strategy.

In order to lower @dgw 's blood pressure, there are new tests, existing tests have been updated as well, and the coverage has increased from ~70% to ~85%.

Also, more doc, because I always write more doc.

PS: yes, some tests have been moved around to follow the same order of declaration as the method they are testing.

Checklist

  • I have read CONTRIBUTING.md
  • I can and do license this contribution under the EFLv2
  • No issues are reported by make qa (runs make quality and make test)
  • I have tested the functionality of the things this change touches

@Exirel Exirel added the Tweak label Jan 25, 2022
@Exirel Exirel added this to the 8.0.0 milestone Jan 25, 2022
@Exirel Exirel requested review from dgw and RustyBower January 25, 2022 21:52
Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

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

I realized that there are some unreviewed PRs, and also that I had the time to look at one.

sopel/db.py Outdated Show resolved Hide resolved
sopel/db.py Outdated Show resolved Hide resolved
sopel/db.py Outdated Show resolved Hide resolved
sopel/db.py Outdated Show resolved Hide resolved
sopel/db.py Outdated Show resolved Hide resolved
sopel/db.py Show resolved Hide resolved
sopel/db.py Show resolved Hide resolved
test/test_db.py Outdated Show resolved Hide resolved
test/test_db.py Outdated Show resolved Hide resolved
test/test_db.py Outdated Show resolved Hide resolved
@Exirel
Copy link
Contributor Author

Exirel commented Apr 22, 2022

Yeah, this one's good for a last round of review!

Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

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

Wow, you went whole-hog on the Monty Python update! 😹 Since I don't quite know what I'd replace "SQLAlchemy ORM's mapping" with, let's leave it alone.

test/test_db.py Outdated Show resolved Hide resolved
Exirel and others added 2 commits May 14, 2022 00:33
SQLAlchemy 1.4 (used by Sopel) defines a cross-compatible new style to
query the database using its ORM, its engine, and its connection:

* Engine cannot execute SQL directly
* Connection can be used as context manager
* Session can be used as context manager
* Instead of doing *.query, use *.execute(stmt)
* SQL statements are easier to create with select, update, and delete
  function from `sqlalchemy.sql`
* And many other things

One of the key change here is the usage of session object as context
manager instead of using try/except block: less code, same feature!

We can't set `future=True` on the Engine yet, because we need the
non-compatible and now deprecated behavior of direct SQL execution from
the Engine object: `SopelDB.execute` has been deprecated and should be
removed in Sopel 8.1, so we can make Sopel compatible with both
SQLAlchemy 1.4 and 2.0, as planned by SQLAlchemy migration strategy.

In order to lower dgw's blood pressure, there are new tests, existing
tests have been updated as well, and the coverage has increased from
~70% to ~85%.

Also, more doc, because I always write more doc.

Co-authored-by: dgw <dgw@technobabbl.es>
As noted by dgw, it's better to replace Sopel's owner/dev/users
nicks by more neutral references to Monty Python characters
and actors.

Is that a dead parrot?
@Exirel
Copy link
Contributor Author

Exirel commented May 13, 2022

Squashed!

@dgw
Copy link
Member

dgw commented May 16, 2022

*stares in Klingon at @RustyBower* (not really to rush you, but to give a status update in-band since we've only talked about your ongoing review on IRC)

sopel/db.py Show resolved Hide resolved
sopel/db.py Show resolved Hide resolved
sopel/db.py Show resolved Hide resolved
@Exirel Exirel requested a review from RustyBower June 2, 2022 06:49
@Exirel
Copy link
Contributor Author

Exirel commented Jun 5, 2022

@dgw all good now!

@dgw
Copy link
Member

dgw commented Jun 5, 2022

I think you meant to ping @RustyBower for final thumbs-up, lol. This already got a ✔️ from me. 😁

@Exirel
Copy link
Contributor Author

Exirel commented Jul 10, 2022

Hm, I'm not sure we are going to get that review from @RustyBower. I'm pretty confident in my change in any case.

@RustyBower
Copy link
Contributor

Still LGTM :)

@Exirel
Copy link
Contributor Author

Exirel commented Jul 10, 2022

Still LGTM :)

Well the PR says "@RustyBower requested changes" so... 😁

@dgw dgw merged commit 5c4f56e into sopel-irc:master Jul 11, 2022
@Exirel Exirel deleted the db-for-sqlalchemy20 branch July 14, 2022 11:45
@ghost ghost mentioned this pull request Jul 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants