How to prevent a temporary table remaining in batch operation mode with SQLite3. #1092
-
Problem detailsNot sure if this is an alembic's issue, but I encounter the problem that after performing a schema migration in batch operation mode in SQLite3, if something goes wrong and an exception occurs, only the temporary table remains halfway at optuna/optuna#4015. I created a repository to reproduce the problem here. In this repository, I added following script to alter table schema and then raise an exception. def upgrade() -> None:
# Schema Migration
with op.batch_alter_table("foo") as batch_op:
batch_op.alter_column("bar", nullable=True)
# Write a data migration here like the following pattern.
# https://alembic.sqlalchemy.org/en/latest/cookbook.html#conditional-migration-elements
...
raise Exception("An exception is raised during the data migration") When an exception is raised, Alembic will issue a
Possible Solutions and WorkaroundsAutomatically issues
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
well without transactional DDL (Which note, SQLite actually has, it's just prevented by a bug in the pysqlite driver), alembic doesn't have a generalized route to "clean up " a failed migration, this implies the migration is only halfway through and there could be any number of tables and column changes that succeeded, and now would have to be "rolled back" - without transactional DDL, there's no path to that. The scenario you describe where a SQLite batch migration fails midway, yet the only issue is the temporary table remaining, seems contrived. what about all the tables/columns/changes that are now present and would have to be rolled back also in order to run the migration again? all of that said your best bet is to just use transactional DDL in sqlite. the recipe at https://docs.sqlalchemy.org/en/14/dialects/sqlite.html#pysqlite-serializable will allow transactional DDL to work with sqlite. then, in your env.py script you will want to set transactional_ddl to true. |
Beta Was this translation helpful? Give feedback.
well without transactional DDL (Which note, SQLite actually has, it's just prevented by a bug in the pysqlite driver), alembic doesn't have a generalized route to "clean up " a failed migration, this implies the migration is only halfway through and there could be any number of tables and column changes that succeeded, and now would have to be "rolled back" - without transactional DDL, there's no path to that.
The scenario you describe where a SQLite batch migration fails midway, yet the only issue is the temporary table remaining, seems contrived. what about all the tables/columns/changes that are now present and would have to be rolled back also in order to run the migration again?
all …