Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions frontera/contrib/backends/sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.engine.reflection import Inspector

from frontera.core.components import DistributedBackend
from frontera.contrib.backends import CommonBackend
Expand Down Expand Up @@ -127,12 +126,10 @@ def strategy_worker(cls, manager):
drop_all_tables = settings.get('SQLALCHEMYBACKEND_DROP_ALL_TABLES')
clear_content = settings.get('SQLALCHEMYBACKEND_CLEAR_CONTENT')
model = b.models['StateModel']
inspector = Inspector.from_engine(b.engine)

if drop_all_tables:
if model.__table__.name in inspector.get_table_names():
model.__table__.drop(bind=b.engine)
model.__table__.create(bind=b.engine)
model.__table__.drop(bind=b.engine, checkfirst=True)
model.__table__.create(bind=b.engine, checkfirst=True)
Copy link
Member

Choose a reason for hiding this comment

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

This is happening because SQLA has non-unified behavior between various engines. For example that code works well for sqlite, without need of checkfirst=True. However, I still think it makes sense to merge it.


if clear_content:
session = b.session_cls()
Expand All @@ -148,18 +145,14 @@ def db_worker(cls, manager):
settings = manager.settings
drop = settings.get('SQLALCHEMYBACKEND_DROP_ALL_TABLES')
clear_content = settings.get('SQLALCHEMYBACKEND_CLEAR_CONTENT')
inspector = Inspector.from_engine(b.engine)

metadata_m = b.models['MetadataModel']
queue_m = b.models['QueueModel']
if drop:
existing = inspector.get_table_names()
if metadata_m.__table__.name in existing:
metadata_m.__table__.drop(bind=b.engine)
if queue_m.__table__.name in existing:
queue_m.__table__.drop(bind=b.engine)
metadata_m.__table__.create(bind=b.engine)
queue_m.__table__.create(bind=b.engine)
metadata_m.__table__.drop(bind=b.engine, checkfirst=True)
queue_m.__table__.drop(bind=b.engine, checkfirst=True)
metadata_m.__table__.create(bind=b.engine, checkfirst=True)
queue_m.__table__.create(bind=b.engine, checkfirst=True)

if clear_content:
session = b.session_cls()
Expand Down