summaryrefslogtreecommitdiff
path: root/alembic/ddl
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2011-11-29 16:09:13 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2011-11-29 16:09:13 -0500
commit2370cdf0f8decab38419aa28175e6be5cf35ac25 (patch)
tree17ccb8228108ce99aff610edff91c83651df12e8 /alembic/ddl
parent43f3a667fa1b75e75f23c11a50e35d7433ceab4f (diff)
downloadalembic-2370cdf0f8decab38419aa28175e6be5cf35ac25.tar.gz
- implement "start migrations" event for impls
- implement counter logic for SQL server constraint/default drop so that variables are declared uniquely within a full migration run, #12
Diffstat (limited to 'alembic/ddl')
-rw-r--r--alembic/ddl/impl.py8
-rw-r--r--alembic/ddl/mssql.py32
2 files changed, 31 insertions, 9 deletions
diff --git a/alembic/ddl/impl.py b/alembic/ddl/impl.py
index 8306fcb..a109ed4 100644
--- a/alembic/ddl/impl.py
+++ b/alembic/ddl/impl.py
@@ -187,6 +187,14 @@ class DefaultImpl(object):
conn_col_default = inspector_column['default']
return conn_col_default != rendered_metadata_default
+ def start_migrations(self):
+ """A hook called when :meth:`.Context.run_migrations`
+ is called.
+
+ Implementations can set up per-migration-run state here.
+
+ """
+
def emit_begin(self):
"""Emit the string ``BEGIN``, or the backend-specific
equivalent, on the current connection context.
diff --git a/alembic/ddl/mssql.py b/alembic/ddl/mssql.py
index 400f614..44d72f1 100644
--- a/alembic/ddl/mssql.py
+++ b/alembic/ddl/mssql.py
@@ -1,12 +1,20 @@
from alembic.ddl.impl import DefaultImpl
from alembic.ddl.base import alter_table, AddColumn, ColumnName, \
format_table_name, format_column_name, ColumnNullable, alter_column
+from alembic import util
from sqlalchemy.ext.compiler import compiles
class MSSQLImpl(DefaultImpl):
__dialect__ = 'mssql'
transactional_ddl = True
+ def start_migrations(self):
+ self.__dict__.pop('const_sym_counter', None)
+
+ @util.memoized_property
+ def const_sym_counter(self):
+ return 1
+
def emit_begin(self):
self._exec("BEGIN TRANSACTION")
@@ -29,30 +37,36 @@ class MSSQLImpl(DefaultImpl):
drop_default = kw.pop('mssql_drop_default', False)
if drop_default:
self._exec(
- _exec_drop_col_constraint(table_name, column, 'sys.default_constraints')
+ _exec_drop_col_constraint(self,
+ table_name, column,
+ 'sys.default_constraints')
)
drop_check = kw.pop('mssql_drop_check', False)
if drop_check:
self._exec(
- _exec_drop_col_constraint(table_name, column, 'sys.check_constraints')
+ _exec_drop_col_constraint(self,
+ table_name, column,
+ 'sys.check_constraints')
)
super(MSSQLImpl, self).drop_column(table_name, column)
-
-def _exec_drop_col_constraint(tname, colname, type_):
+def _exec_drop_col_constraint(impl, tname, colname, type_):
# from http://www.mssqltips.com/sqlservertip/1425/working-with-default-constraints-in-sql-server/
# TODO: needs table formatting, etc.
- return """declare @const_name varchar(256)
-select @const_name = [name] from %(type)s
+ counter = impl.const_sym_counter
+ impl.const_sym_counter += 1
+
+ return """declare @const_name_%(sym)s varchar(256)
+select @const_name_%(sym)s = [name] from %(type)s
where parent_object_id = object_id('%(tname)s')
and col_name(parent_object_id, parent_column_id) = '%(colname)s'
-exec('alter table %(tname)s drop constraint ' + @const_name)""" % {
+exec('alter table %(tname)s drop constraint ' + @const_name_%(sym)s)""" % {
'type':type_,
'tname':tname,
- 'colname':colname
+ 'colname':colname,
+ 'sym':counter
}
-
@compiles(AddColumn, 'mssql')
def visit_add_column(element, compiler, **kw):
return "%s %s" % (