Add table_exists() util function

This commit is contained in:
Lance Edgar 2020-03-31 14:09:51 -05:00
parent d818100da3
commit 87fd5367a1

View file

@ -36,3 +36,25 @@ def get_last_card_number(session):
""" """
return session.query(sa.func.max(corepos.Customer.card_number))\ return session.query(sa.func.max(corepos.Customer.card_number))\
.scalar() or 0 .scalar() or 0
def table_exists(session, model_class):
"""
Determine if a table exists in the database.
:param session: SQLAlchemy session object, opened against the database in
question.
:param model_class: The model class associated with the table in question.
:returns: Boolean indicating if the table exists.
"""
try:
session.query(model_class).count()
except sa.exc.ProgrammingError as error:
if "doesn't exist" in str(error):
return False
else:
raise
else:
return True