summaryrefslogtreecommitdiff
path: root/alembic/operations
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2022-07-01 09:56:10 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2022-07-01 11:54:16 -0400
commitcabccf6d05aaf119414166b260f322c8661352ac (patch)
tree4dcda5a50a295cb11fc8a8666d824b69d45e74f9 /alembic/operations
parent852bea05b2665856cc5cc88652c0a66863747415 (diff)
downloadalembic-cabccf6d05aaf119414166b260f322c8661352ac.tar.gz
fail gracefully for batch_alter_table() called in --sql mode
Added an error raise for the condition where :meth:`.Operations.batch_alter_table` is used in ``--sql`` mode, where the operation requires table reflection, as is the case when running against SQLite without giving it a fixed ``Table`` object. Previously the operation would fail with an internal error. To get a "move and copy" batch operation as a SQL script without connecting to a database, a ``Table`` object should be passed to the :paramref:`.Operations.batch_alter_table.copy_from` parameter so that reflection may be skipped. Change-Id: I2d040e7e5771eefabba1649d71ed451567c25283 Fixes: #1021
Diffstat (limited to 'alembic/operations')
-rw-r--r--alembic/operations/batch.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/alembic/operations/batch.py b/alembic/operations/batch.py
index 7c7de9f..71d2681 100644
--- a/alembic/operations/batch.py
+++ b/alembic/operations/batch.py
@@ -119,6 +119,21 @@ class BatchOperationsImpl:
existing_table = self.copy_from
reflected = False
else:
+ if self.operations.migration_context.as_sql:
+ raise exc.CommandError(
+ f"This operation cannot proceed in --sql mode; "
+ f"batch mode with dialect "
+ f"{self.operations.migration_context.dialect.name} " # noqa: E501
+ f"requires a live database connection with which "
+ f'to reflect the table "{self.table_name}". '
+ f"To generate a batch SQL migration script using "
+ "table "
+ '"move and copy", a complete Table object '
+ f'should be passed to the "copy_from" argument '
+ "of the batch_alter_table() method so that table "
+ "reflection can be skipped."
+ )
+
existing_table = Table(
self.table_name,
m1,