Merge pull request #14564 from jdavcs/dev_alembic_fix1

Don't query the database when running migrations in offline mode
This commit is contained in:
John Chilton
2022-09-09 13:19:06 -04:00
committed by GitHub
+16 -1
View File
@@ -1,6 +1,9 @@
import logging
from alembic import op
from alembic import (
context,
op,
)
from sqlalchemy import inspect
log = logging.getLogger(__name__)
@@ -12,7 +15,19 @@ def drop_column(table_name, column_name):
def column_exists(table_name, column_name):
if is_offline_mode():
msg = (
"This script is being executed in offline mode and cannot connect to the database. "
f"Therefore, `column_exists({table_name}, {column_name})` returns `False` by default."
)
log.debug(msg)
return False
bind = op.get_context().bind
insp = inspect(bind)
columns = insp.get_columns(table_name)
return any(c["name"] == column_name for c in columns)
def is_offline_mode():
cmd_opts = context.config.cmd_opts
return cmd_opts and cmd_opts.sql