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

WIP: Check if table already exists before creating it #158

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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