Repository navigation
Can't get alembic api working #1631
Replies: 1 comment
|
I think the migrations ran, just in a different database. The fixture passes a connection to the server's default database while pointing alembic_cfg.attributes["connection"] = connection # connected to the default db
alembic_cfg.set_main_option("sqlalchemy.url", f".../{db}_test")What happens next depends on your
So look for The fix is to keep the admin connection only for admin = create_engine(database_url)
with admin.connect().execution_options(isolation_level="AUTOCOMMIT") as conn:
conn.execute(text(f"DROP DATABASE IF EXISTS {db}_test"))
conn.execute(text(f"CREATE DATABASE {db}_test"))
test_engine = create_engine(f"{database_url}/{db}_test")
with test_engine.begin() as connection:
cfg = AlembicConfig()
cfg.set_main_option("script_location", "/app/src/alembic")
cfg.attributes["connection"] = connection
alembic_command.upgrade(cfg, "head")
test_engine.dispose()
yield
with admin.connect().execution_options(isolation_level="AUTOCOMMIT") as conn:
conn.execute(text(f"DROP DATABASE {db}_test"))This assumes your On logging, the stock |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I'm trying to get alembic to run on my test database in a pytest fixture:
But when I run tests, I get the error that the relation I'm testing against doesn't exist, which seems to indicate alembic never ran? Also, I don't love that this will modify my logging settings. I'm not sure if there's a better way to address this problem.
I've moved the alembic code to a separate script, to just test it on it's own, but while the script takes a second or two to run, there's no output, and no changes to my db.
I was originally looking at how to get alembic to run against multiple databases at once (or choose the database) via the cli, but no luck there.
All reactions