Dropping alembic_version
table inside migration script fails
#998
-
Hi, I'm in the process of writing a wrapper around The target I have that represents the base at the moment, does not contain the from alembic import op
•••
def downgrade():
with op.batch_alter_table("comparisons") as batch_op:
batch_op.drop_column("kmersize")
batch_op.drop_column("minmatch")
batch_op.drop_constraint("fastani_reqs")
batch_op.create_unique_constraint(
"base_reqs",
["query_id", "subject_id", "program", "version", "fragsize", "maxmatch"],
)
op.drop_table("alembic_version") With this version, I get the traceback and error shown below. This is similar to what I have gotten before when I tried to remove constrained elements improperly, so I think it may have to do with that. Traceback (most recent call last):
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1802, in _execute_context
self.dialect.do_execute(
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
cursor.execute(statement, parameters)
sqlite3.OperationalError: no such table: alembic_version
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/bin/alembic", line 8, in <module>
sys.exit(main())
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/config.py", line 588, in main
CommandLine(prog=prog).main(argv=argv)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/config.py", line 582, in main
self.run_cmd(cfg, options)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/config.py", line 559, in run_cmd
fn(
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/command.py", line 366, in downgrade
script.run_env()
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/script/base.py", line 563, in run_env
util.load_python_file(self.dir, "env.py")
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/util/pyfiles.py", line 92, in load_python_file
module = load_module_py(module_id, path)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/util/pyfiles.py", line 108, in load_module_py
spec.loader.exec_module(module) # type: ignore
File "<frozen importlib._bootstrap_external>", line 843, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "alembic/env.py", line 104, in <module>
run_migrations_online()
File "alembic/env.py", line 79, in run_migrations_online
context.run_migrations()
File "<string>", line 8, in run_migrations
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/runtime/environment.py", line 851, in run_migrations
self.get_context().run_migrations(**kw)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/runtime/migration.py", line 627, in run_migrations
head_maintainer.update_to_step(step)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/runtime/migration.py", line 812, in update_to_step
self._delete_version(vers)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/runtime/migration.py", line 765, in _delete_version
ret = self.context.impl._exec(
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/ddl/impl.py", line 197, in _exec
return conn.execute(construct, multiparams)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1289, in execute
return meth(self, multiparams, params, _EMPTY_EXECUTION_OPTS)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/sql/elements.py", line 325, in _execute_on_connection
return connection._execute_clauseelement(
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1481, in _execute_clauseelement
ret = self._execute_context(
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1845, in _execute_context
self._handle_dbapi_exception(
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2026, in _handle_dbapi_exception
util.raise_(
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1802, in _execute_context
self.dialect.do_execute(
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: alembic_version
[SQL: DELETE FROM alembic_version WHERE alembic_version.version_num = '92f7f6b1626e']
(Background on this error at: https://sqlalche.me/e/14/e3q8) However, I also alter a constraint in another table in this migration (see above code), and I tried using the commented-out lines below to recreate what I did there, to no avail. from alembic import op
•••
def downgrade():
with op.batch_alter_table("comparisons") as batch_op:
batch_op.drop_column("kmersize")
batch_op.drop_column("minmatch")
batch_op.drop_constraint("fastani_reqs")
batch_op.create_unique_constraint(
"base_reqs",
["query_id", "subject_id", "program", "version", "fragsize", "maxmatch"],
)
# with op.batch_alter_table("alembic_version") as batch_op:
# batch_op.drop_column("version_num")
# batch_op.drop_constraint("alembic_version_pkc")
op.drop_table("alembic_version") This, instead, gives me a different error: Traceback (most recent call last):
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1802, in _execute_context
self.dialect.do_execute(
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
cursor.execute(statement, parameters)
sqlite3.OperationalError: near ")": syntax error
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/bin/alembic", line 8, in <module>
sys.exit(main())
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/config.py", line 588, in main
CommandLine(prog=prog).main(argv=argv)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/config.py", line 582, in main
self.run_cmd(cfg, options)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/config.py", line 559, in run_cmd
fn(
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/command.py", line 366, in downgrade
script.run_env()
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/script/base.py", line 563, in run_env
util.load_python_file(self.dir, "env.py")
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/util/pyfiles.py", line 92, in load_python_file
module = load_module_py(module_id, path)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/util/pyfiles.py", line 108, in load_module_py
spec.loader.exec_module(module) # type: ignore
File "<frozen importlib._bootstrap_external>", line 843, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "alembic/env.py", line 104, in <module>
run_migrations_online()
File "alembic/env.py", line 79, in run_migrations_online
context.run_migrations()
File "<string>", line 8, in run_migrations
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/runtime/environment.py", line 851, in run_migrations
self.get_context().run_migrations(**kw)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/runtime/migration.py", line 620, in run_migrations
step.migration_fn(**kw)
File "/Users/baileythegreen/Software/pyani/alembic/versions/92f7f6b1626e_add_fastani_columns.py", line 126, in downgrade
batch_op.drop_constraint("alembic_version_pkc")
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/contextlib.py", line 120, in __exit__
next(self.gen)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/operations/base.py", line 374, in batch_alter_table
impl.flush()
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/operations/batch.py", line 140, in flush
batch_impl._create(self.impl)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/operations/batch.py", line 411, in _create
op_impl.create_table(self.new_table)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/ddl/impl.py", line 356, in create_table
self._exec(schema.CreateTable(table))
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/alembic/ddl/impl.py", line 197, in _exec
return conn.execute(construct, multiparams)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1289, in execute
return meth(self, multiparams, params, _EMPTY_EXECUTION_OPTS)
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/sql/ddl.py", line 80, in _execute_on_connection
return connection._execute_ddl(
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1381, in _execute_ddl
ret = self._execute_context(
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1845, in _execute_context
self._handle_dbapi_exception(
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2026, in _handle_dbapi_exception
util.raise_(
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1802, in _execute_context
self.dialect.do_execute(
File "/Users/baileythegreen/Software/miniconda3/envs/pyani_dev/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) near ")": syntax error
[SQL:
CREATE TABLE _alembic_tmp_alembic_version (
)
]
(Background on this error at: https://sqlalche.me/e/14/e3q8) It may be relevant to note that I have modified the ORM (see below) used to generate new databases so that the class Alembic(Base):
__tablename__ = "alembic_version"
__table_args__ = (PrimaryKeyConstraint("version_num", name="alembic_version_pkc"),)
version_num = Column(types.VARCHAR(32), primary_key=True, nullable=False) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Alembic relies upon the configured For Alembic's management of the "alembic_version" table, on a clean database Alembic will create this table first if it does not exist, before it runs migrations. Then, if you run a downgrade operation all the way to the "base", Alembic automatically deletes the table. So if you were to downgrade a datbase all the way down, the table is deleted for you. Beyond that, the problem being solved isn't clear to me (specifically "The target I have that represents the base at the moment, does not contain the alembic_version table, so I am trying to ensure that table is dropped in the migration." - if you are running Alembic, it will automatically drop the alembic_version table when the database is downgraded all the way. I'm assuming these are downgrade operations). If this is a test suite that aims to have a clean database when the suite finishes, I would add a "drop" phase to the test suite's teardown, external to the migrations that are being tested. This could be either a direct drop of the alembic_version table or for a more comprehensive approach you could use a recipe like dropalltables that will locate all tables within a schema and drop them. |
Beta Was this translation helpful? Give feedback.
Alembic relies upon the configured
alembic_version
table in order to track what migrations it needs to run. It's not possible to have a migration that deletes the table itself.For Alembic's management of the "alembic_version" table, on a clean database Alembic will create this table first if it does not exist, before it runs migrations. Then, if you run a downgrade operation all the way to the "base", Alembic automatically deletes the table. So if you were to downgrade a datbase all the way down, the table is deleted for you.
Beyond that, the problem being solved isn't clear to me (specifically "The target I have that represents the base at the moment, does not contain the alembic_versio…