multiple db raise err should not exit bt continue #878
Unanswered
AmuCh1
asked this question in
Usage Questions
Replies: 1 comment
-
to pass data from wihtin env.py outside to the caller of command.upgrade(), we can use the same approach as that described at https://alembic.sqlalchemy.org/en/latest/api/commands.html, which is to place your own "status" inside of config.attributes: ##### your program ######
# call upon command.upgrade
from alembic.config import Config
from alembic import command
alembic_cfg = Config("/path/to/yourapp/alembic.ini")
alembic_cfg.attributes["status"] = []
command.upgrade(alembic_cfg, "head")
# then you can look at errors in status
for msg in alembic_cfg.attributes["status"]:
print(f"got status: {msg}")
#### env.py #####
from alembic import context
config = context.config
def run_migrations_online():
# ...
for name, rec in engines.items():
engine = rec["engine"]
rec["connection"] = conn = engine.connect()
rec["transaction"] = conn.begin()
try:
logger.info("Migrating database %s" % name)
context.configure(
connection=rec["connection"],
upgrade_token="%s_upgrades" % name,
downgrade_token="%s_downgrades" % name,
target_metadata=target_metadata.get(name),
)
context.run_migrations(engine_name=name)
rec["transaction"].commit()
except Exception as err:
config.attributes["status"].append(f"database {name} had a problem: {err}")
rec["transaction"].rollback()
finally:
rec["connection"].close() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I have seen this topic here #691 but didnt discuss if there is multiple dbs.
I do not want to use the error message but i want to continue with all other dbs to getting updated.
I have used except TypeError. and for me i have multiple directories each directory will have set of dbs.
but if one of the directory has an issue and didnt do successful migration it should not stop but do the next directory.
at the same time, i want to count number of such directories and tell the user those directories are not migrated.
basically i am using alembic.command.upgrade(config,rev), i set directory in the config -- so is there a way i get the status of command.upgrade -- so that i can take care of
how do i want to proceed in top script which calls command.upgrade with different directories.
for name, rec in engines.items():
engine = rec["engine"]
rec["connection"] = conn = engine.connect()
Beta Was this translation helpful? Give feedback.
All reactions