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

fix(python): Harden alchemy session for old sqlalchemy versions #17366

Merged
merged 1 commit into from
Jul 6, 2024
Merged

fix(python): Harden alchemy session for old sqlalchemy versions #17366

merged 1 commit into from
Jul 6, 2024

Conversation

brunobbaraujo
Copy link
Contributor

Harden alchemy session for old sqlalchemy versions.

A similar fix was made before for the sqlalchemy engine to avoid import errors on old sqlalchemy packages (<2.0).

@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Jul 2, 2024
@stinodego stinodego changed the title fix: Harden alchemy session for old sqlalchemy versions fix(python): Harden alchemy session for old sqlalchemy versions Jul 2, 2024
Copy link

codecov bot commented Jul 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.72%. Comparing base (663fe4c) to head (20d888a).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17366      +/-   ##
==========================================
- Coverage   80.72%   80.72%   -0.01%     
==========================================
  Files        1475     1475              
  Lines      193383   193387       +4     
  Branches     2760     2761       +1     
==========================================
- Hits       156113   156102      -11     
- Misses      36760    36775      +15     
  Partials      510      510              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Harden alchemy session for old sqlalchemy versions.

A similar fix was made before for the sqlalchemy engine to avoid import errors on old sqlalchemy packages (<2.0).
@brunobbaraujo
Copy link
Contributor Author

As stated in the sqlalchemy documentation, the async_sessiomaker is a new feature for SQLAlchemy >= 2.0. After making the change proposed changes in this pull request, I was able to use pl.read_database(query, session.connection()) in my code with SQLAlchemy 1.4.52

@brunobbaraujo
Copy link
Contributor Author

Here's a sequence of steps on how to reproduce the error:

Install the version of SQLAlchemy

pip install 'sqlalchemy<2'

Then

import os

import polars as pl
from dotenv import load_dotenv
from sqlalchemy import create_engine, select
from sqlalchemy.orm.session import Session, sessionmaker

load_dotenv()

db_host = os.getenv("DB_HOST")
db_port = os.getenv("DB_PORT")
db_name = os.getenv("DB_NAME")
db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")

engine = create_engine(
    f"postgresql+psycopg2://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"
)
with sessionmaker(engine).begin() as session:
      print(pl.read_database(select(1), session.connection()))

@ritchie46
Copy link
Member

Thanks!

@ritchie46 ritchie46 merged commit 03a769d into pola-rs:main Jul 6, 2024
14 checks passed
henryharbeck pushed a commit to henryharbeck/polars that referenced this pull request Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants