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

feat: port to SQLAlchemy 1.4 / 2.0 #28

Merged
merged 10 commits into from
Apr 8, 2021
Merged

feat: port to SQLAlchemy 1.4 / 2.0 #28

merged 10 commits into from
Apr 8, 2021

Conversation

hadrien
Copy link
Member

@hadrien hadrien commented Apr 6, 2021

Description

  • Pagination now allows 2.0 sqla queries.

Related PRs

Blocked by

@hadrien hadrien requested a review from vicrep April 6, 2021 00:19
@codecov
Copy link

codecov bot commented Apr 6, 2021

Codecov Report

Merging #28 (fe3ebc6) into master (78308f4) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #28   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            2         2           
  Lines          128       148   +20     
  Branches         1         2    +1     
=========================================
+ Hits           128       148   +20     
Flag Coverage Δ
sqlalchemy1.3 94.59% <71.42%> (-5.41%) ⬇️
sqlalchemy1.4 100.00% <100.00%> (?)

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

Impacted Files Coverage Δ
fastapi_sqla/__init__.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 78308f4...fe3ebc6. Read the comment docs.

Base automatically changed from chore/fix-tox to master April 6, 2021 00:50
@hadrien hadrien force-pushed the feat/select-count branch from 60282d8 to b4011e1 Compare April 6, 2021 00:50
@hadrien hadrien changed the title feat!: port to SQLAlchemy 2.0 feat!: port to SQLAlchemy 1.4 / 2.0 Apr 6, 2021
@hadrien hadrien requested a review from a team April 6, 2021 18:03
@hadrien
Copy link
Member Author

hadrien commented Apr 6, 2021

If you have bandwidth @vicrep 🙏🏽 ?

Copy link
Contributor

@vicrep vicrep left a comment

Choose a reason for hiding this comment

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

Could you elaborate on why we need to make the query_counter a dependency? Could we not make it a pure function which takes as arguments the query, which could be either a legacy or non-legacy query?

IMO requiring you make a dependency that returns a callable as a counter function feels a little indirected from a consumer's PoV / is not a very straightforward interface.

Could we make the pagination dependency smart enough to then determine if the query is a session.query or a DbQuery, and only call session.execute() on the latter?

Honestly, as I think about it more, could it be worth having two separate dependencies, one to paginate classic SQLA queries, and one to paginate when passed a 1.4 DbQuery? That way no breaking changes, and I think there's enough overlap that most of the code would be reused between both, but that way we're super explicit.

@hadrien
Copy link
Member Author

hadrien commented Apr 8, 2021

Could you elaborate on why we need to make the query_counter a dependency? Could we not make it a pure function which takes as arguments the query, which could be either a legacy or non-legacy query?

Dependency is nice to do this:

def query_count_dep(q: str = Query(...), session:Session=Depends())-> QueryCount:
    def query_count(query:DbQuery)->int:
        query = select(func.count()).where(User.name.like(q)
         return session.execute(query).scalar()

IMO requiring you make a dependency that returns a callable as a counter function feels a little indirected from a consumer's PoV / is not a very straightforward interface.

I agree.

Could we make the pagination dependency smart enough to then determine if the query is a session.query or a DbQuery, and only call session.execute() on the latter?

yes, it is actually what that PR achieves. Out of the box, it just works with legacy and modern queries.

Honestly, as I think about it more, could it be worth having two separate dependencies, one to paginate classic SQLA queries, and one to paginate when passed a 1.4 DbQuery? That way no breaking changes, and I think there's enough overlap that most of the code would be reused between both, but that way we're super explicit.

I will keep the same query_count and remove the query count dependency thing..

That PR breaks the scoping rule. It does 2 things: porting to SQL 2.0 queries. Introducing a query count dependency. I'll lean and remove the breaking change.

@vicrep
Copy link
Contributor

vicrep commented Apr 8, 2021

Oh I totally agree that the dependency a nice "advanced" usage option; I just would offer a simpler interface for people who want something a little more straightforward :)

Sounds good about scoping, I think we can probably find a sleek way to allow users to do both (simple/pure counter, and advanced dependency based counter).

@vicrep
Copy link
Contributor

vicrep commented Apr 8, 2021

Something similar to how the python JSON module lets you override encoders could be nice -- when calling json.dumps, you can pass either: a "default" encoder which overrides just the default method of the default encoder (as a pure function), or a "cls" argument to override the entire encoder class.

@hadrien hadrien force-pushed the feat/select-count branch from a915063 to 4ad6882 Compare April 8, 2021 15:35
@hadrien hadrien requested a review from vicrep April 8, 2021 15:36
@hadrien
Copy link
Member Author

hadrien commented Apr 8, 2021

not anymore a breaking change FYI @vicrep

fastapi_sqla/__init__.py Outdated Show resolved Hide resolved
fastapi_sqla/__init__.py Outdated Show resolved Hide resolved
@hadrien hadrien changed the title feat!: port to SQLAlchemy 1.4 / 2.0 feat: port to SQLAlchemy 1.4 / 2.0 Apr 8, 2021
Copy link
Contributor

@vicrep vicrep left a comment

Choose a reason for hiding this comment

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

🚢 🎉

@hadrien hadrien merged commit cbf06d1 into master Apr 8, 2021
@hadrien hadrien deleted the feat/select-count branch April 8, 2021 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants