summaryrefslogtreecommitdiff
path: root/alembic/batch.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2014-11-09 16:16:09 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2014-11-09 16:16:09 -0500
commit36fa6e4603ba19d6c27b4e4c728a30effa72fdc0 (patch)
tree8201a795140abe5cccdab1abb6847d385a3408dc /alembic/batch.py
parenta9c58f1ca6b3adb7869aba241f3fd5f64ddd279c (diff)
downloadalembic-36fa6e4603ba19d6c27b4e4c728a30effa72fdc0.tar.gz
- test fixup
- get batch mode to fail gracefully, dropping the temp table if the operation fails - finish tutorial
Diffstat (limited to 'alembic/batch.py')
-rw-r--r--alembic/batch.py42
1 files changed, 25 insertions, 17 deletions
diff --git a/alembic/batch.py b/alembic/batch.py
index 8470404..7bfcf22 100644
--- a/alembic/batch.py
+++ b/alembic/batch.py
@@ -2,11 +2,15 @@ from sqlalchemy import Table, MetaData, Index, select, Column, \
ForeignKeyConstraint, cast
from sqlalchemy import types as sqltypes
from sqlalchemy.util import OrderedDict
+from . import util
class BatchOperationsImpl(object):
def __init__(self, operations, table_name, schema, recreate,
copy_from, table_args, table_kwargs):
+ if not util.sqla_08:
+ raise NotImplementedError(
+ "batch mode requires SQLAlchemy 0.8 or greater.")
self.operations = operations
self.table_name = table_name
self.schema = schema
@@ -174,24 +178,28 @@ class ApplyBatchImpl(object):
op_impl.prep_table_for_batch(self.table)
op_impl.create_table(self.new_table)
- op_impl._exec(
- self.new_table.insert(inline=True).from_select(
- list(k for k, transfer in
- self.column_transfers.items() if 'expr' in transfer),
- select([
- transfer['expr']
- for transfer in self.column_transfers.values()
- if 'expr' in transfer
- ])
+ try:
+ op_impl._exec(
+ self.new_table.insert(inline=True).from_select(
+ list(k for k, transfer in
+ self.column_transfers.items() if 'expr' in transfer),
+ select([
+ transfer['expr']
+ for transfer in self.column_transfers.values()
+ if 'expr' in transfer
+ ])
+ )
+ )
+ op_impl.drop_table(self.table)
+ except:
+ op_impl.drop_table(self.new_table)
+ raise
+ else:
+ op_impl.rename_table(
+ "_alembic_batch_temp",
+ self.table.name,
+ schema=self.table.schema
)
- )
-
- op_impl.drop_table(self.table)
- op_impl.rename_table(
- "_alembic_batch_temp",
- self.table.name,
- schema=self.table.schema
- )
def alter_column(self, table_name, column_name,
nullable=None,