New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when removing server_default on MySQL #103

Closed
sqlalchemy-bot opened this Issue Feb 5, 2013 · 7 comments

Comments

Projects
None yet
1 participant
@sqlalchemy-bot

sqlalchemy-bot commented Feb 5, 2013

Migrated issue, originally created by Robert Buchholz (@rbuchholz)

The following statement produces an exception when run against MySQL:

    op.alter_column('table', 'column', server_default=None, existing_type=sa.Boolean(), existing_nullable=False)

A unit test to extend tests/test_mysql.py follows:

def test_alter_column_drop_default():
    context = op_fixture('mysql')
    op.alter_column("t", "c", existing_type=Integer, server_default=None)
    context.assert_(
        'ALTER TABLE t ALTER COLUMN c DROP DEFAULT $MYSQL_MAGIC_YOU_KNOW_BETTER_THAN_ME'
    )

@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Feb 5, 2013

Changes by Robert Buchholz (@rbuchholz):

  • edited description
@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Feb 5, 2013

Michael Bayer (@zzzeek) wrote:

the whole of mysql.py needs to be expanded out with individual ALTER, CHANGE, and DEFAULT directives. part of a patch:

diff -r cc6a13c4f4cf9e635aedfe3c377e5b6789f3e8f8 alembic/ddl/mysql.py
--- a/alembic/ddl/mysql.py	Mon Feb 04 13:44:48 2013 -0500
+++ b/alembic/ddl/mysql.py	Tue Feb 05 11:00:56 2013 -0500
@@ -22,23 +22,57 @@
                         existing_nullable=None,
                         existing_autoincrement=None
                     ):
-        self._exec(
-            MySQLAlterColumn(
-                table_name, column_name,
-                schema=schema,
-                newname=name if name is not None else column_name,
-                nullable=nullable if nullable is not None else
-                                existing_nullable
-                                if existing_nullable is not None
-                                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
+        if server_default is not False:
+            self._exec(
+                MySQLAlterDefault(
+                    table_name, column_name,
+                    schema=schema,
+                )
+            )
+        if name is not None:
+            self._exec(
+                MySQLChangeColumn(
+                    table_name, column_name,
+                    schema=schema,
+                    newname=name,
+                    nullable=nullable if nullable is not None else
+                                    existing_nullable
+                                    if existing_nullable is not None
+                                    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
+                )
+            )
+        elif nullable is not None or \
+            type_ is not None or \
+            autoincrement is not None:
+            self._exec(
+                MySQLAlterColumn(
+                    table_name, column_name,
+                    schema=schema,
+                    newname=name if name is not None else column_name,
+                    nullable=nullable if nullable is not None else
+                                    existing_nullable
+                                    if existing_nullable is not None
+                                    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
             )
         )
 
+class MySQLAlterDefault(AlterColumn):
+    def __init__(self, name, column_name, default, schema=None):
+        super(AlterColumn, self).__init__(name, schema=schema)
+        self.column_name = column_name
+        self.default = default
+
+
 class MySQLAlterColumn(AlterColumn):
     def __init__(self, name, column_name, schema=None,
                         newname=None,
@@ -60,6 +94,10 @@
 
         self.type_ = sqltypes.to_instance(type_)
 
+class MySQLChangeColumn(MySQLAlterColumn):
+    pass
+
+
 @compiles(ColumnNullable, 'mysql')
 @compiles(ColumnName, 'mysql')
 @compiles(ColumnDefault, 'mysql')

@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Jun 7, 2013

Changes by Michael Bayer (@zzzeek):

  • added labels: op directives
@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Feb 21, 2014

Jeff Dairiki (@dairiki) wrote:

Possible fix in pull request #21

@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Mar 16, 2014

Michael Bayer (@zzzeek) wrote:

  • Added/fixed support for MySQL "SET DEFAULT" / "DROP DEFAULT" phrases,
    which will now be rendered if only the server default is changing
    or being dropped (e.g. specify None to alter_column() to indicate
    "DROP DEFAULT"). Also added support for rendering MODIFY rather than
    CHANGE when the column name isn't changing.
    fixes #103

b61eccc

@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Mar 16, 2014

Changes by Michael Bayer (@zzzeek):

  • changed status to closed
@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Mar 22, 2014

Michael Bayer (@zzzeek) wrote:

  • add an additional fix to the MySQL fixes for re: #103.

b297807

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment