Commits

Moriyoshi Koizumi  committed 9fe8d03

Support autoincrement and existing_autoincrement in alter_column for MySQL dialect.

It'd be impossible to alter an autoincremented column without this extension.

  • Participants
  • Parent commits cd36d5e
  • Branches bugfix/autoincrement_for_mysql_alter_column

Comments (0)

Files changed (4)

File alembic/ddl/impl.py

                         name=None,
                         type_=None,
                         schema=None,
+                        autoincrement=None,
                         existing_type=None,
                         existing_server_default=None,
-                        existing_nullable=None
+                        existing_nullable=None,
+                        existing_autoincrement=None
                     ):
-
+        if autoincrement is not None or existing_autoincrement is not None:
+            util.warn("nautoincrement and existing_autoincrement only make sense for MySQL")
         if nullable is not None:
             self._exec(base.ColumnNullable(table_name, column_name, 
                                 nullable, schema=schema,

File alembic/ddl/mssql.py

                         name=None,
                         type_=None,
                         schema=None,
+                        autoincrement=None,
                         existing_type=None,
                         existing_server_default=None,
-                        existing_nullable=None
+                        existing_nullable=None,
+                        existing_autoincrement=None
                     ):
 
         if nullable is not None and existing_type is None:
                         nullable=nullable,
                         type_=type_,
                         schema=schema,
+                        autoincrement=autoincrement,
                         existing_type=existing_type,
-                        existing_nullable=existing_nullable
+                        existing_nullable=existing_nullable,
+                        existing_autoincrement=existing_autoincrement
         )
 
         if server_default is not False:

File alembic/ddl/mysql.py

                         name=None,
                         type_=None,
                         schema=None,
+                        autoincrement=None,
                         existing_type=None,
                         existing_server_default=None,
-                        existing_nullable=None
+                        existing_nullable=None,
+                        existing_autoincrement=None
                     ):
         self._exec(
             MySQLAlterColumn(
                                 else True,
                 type_=type_ if type_ is not None else existing_type,
                 default=server_default if server_default is not False else existing_server_default,
+                autoincrement=autoincrement if autoincrement is not None else existing_autoincrement
             )
         )
 
                         newname=None,
                         type_=None,
                         nullable=None,
-                        default=False):
+                        default=False,
+                        autoincrement=None):
         super(AlterColumn, self).__init__(name, schema=schema)
         self.column_name = column_name
         self.nullable = nullable
         self.newname = newname
         self.default = default
+        self.autoincrement = autoincrement
         if type_ is None:
             raise util.CommandError(
                 "All MySQL ALTER COLUMN operations "
             name=element.newname,
             nullable=element.nullable,
             server_default=element.default,
-            type_=element.type_
+            type_=element.type_,
+            autoincrement=element.autoincrement
         ),
     )
 
     else:
         return compiler.sql_compiler.process(expr)
 
-def _mysql_colspec(compiler, name, nullable, server_default, type_):
+def _mysql_colspec(compiler, name, nullable, server_default, type_, autoincrement):
     spec = "%s %s %s" % (
         name,
         compiler.dialect.type_compiler.process(type_),
         "NULL" if nullable else "NOT NULL"
     )
+    if autoincrement is not None:
+        spec += " AUTO_INCREMENT"
     if server_default != False:
         spec += " DEFAULT %s" % render_value(compiler, server_default)
 

File alembic/operations.py

                         server_default=False,
                         name=None,
                         type_=None,
+                        autoincrement=None,
                         existing_type=None,
                         existing_server_default=False,
                         existing_nullable=None,
+                        existing_autoincrement=None
     ):
         """Issue an "alter column" instruction using the
         current migration context.
         :param existing_nullable: Optional; the existing nullability
          of the column.  Required on MySQL if the existing nullability
          is not being changed; else MySQL sets this to NULL.
+        :param existing_autoincrement: Optional; the 
         """
 
         if existing_type:
             server_default=server_default,
             name=name,
             type_=type_,
+            autoincrement=autoincrement,
             existing_type=existing_type,
             existing_server_default=existing_server_default,
             existing_nullable=existing_nullable,
+            existing_autoincrement=existing_autoincrement
         )
 
         if type_: