Avancini
Avancini

Reputation: 73

Deprecated Session.close_all() vs 'scoped_session' object has no attribute 'close_all_sessions'

I am trying to use 'drop_all' after service test fails or finishes on flask app layer:

@pytest.fixture(scope='class')
def db_connection():
    db_url = TestConfig.db_url
    db = SQLAlchemyORM(db_url)
    db.create_all(True)
    yield db_connection
    db.drop_all()

When some test passes the 'drop_all' works, but when it fails the test freezes.

So, that solution solves my problem: https://stackoverflow.com/a/44437760/3050042

Unfortunately, I got a mess with that.

When I use the 'Session.close_all()' SQLAlchemy warns:

The Session.close_all() method is deprecated and will be removed in a future release.  Please refer to session.close_all_sessions().

When I change to the suggestion:

AttributeError: 'scoped_session' object has no attribute 'close_all_sessions'

Yes, I use scoped_session and pure SQLAlchemy.

How to solve this?

Upvotes: 4

Views: 2340

Answers (2)

korolkevichm
korolkevichm

Reputation: 45

asyncio (AsyncSession) analog:

from sqlalchemy.ext.asyncio import close_all_sessions

await close_all_sessions()

Upvotes: 1

David Alber
David Alber

Reputation: 18091

The close_all_sessions function is defined at the top level of sqlalchemy.orm.session. At the time of writing this answer, here is how it looks. Thus, you can use it as follows.

from sqlalchemy.orm.session import close_all_sessions


close_all_sessions()

Upvotes: 12

Related Questions