2009-01-25 12:52:33 +00:00
|
|
|
"""
|
|
|
|
MySQL database specific implementations of changeset classes.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from migrate.changeset import ansisql, exceptions
|
2008-02-06 18:39:07 +00:00
|
|
|
from sqlalchemy.databases import mysql as sa_base
|
|
|
|
#import sqlalchemy as sa
|
|
|
|
|
|
|
|
MySQLSchemaGenerator = sa_base.MySQLSchemaGenerator
|
|
|
|
|
2009-01-25 12:52:33 +00:00
|
|
|
|
|
|
|
class MySQLColumnGenerator(MySQLSchemaGenerator, ansisql.ANSIColumnGenerator):
|
2008-02-06 18:39:07 +00:00
|
|
|
pass
|
2009-01-25 12:52:33 +00:00
|
|
|
|
|
|
|
|
2008-02-06 18:39:07 +00:00
|
|
|
class MySQLColumnDropper(ansisql.ANSIColumnDropper):
|
2009-06-11 22:27:38 +00:00
|
|
|
pass
|
2008-12-02 05:11:36 +00:00
|
|
|
|
2009-01-25 12:52:33 +00:00
|
|
|
|
|
|
|
class MySQLSchemaChanger(MySQLSchemaGenerator, ansisql.ANSISchemaChanger):
|
|
|
|
|
|
|
|
def visit_column(self, delta):
|
2008-02-06 18:39:07 +00:00
|
|
|
keys = delta.keys()
|
|
|
|
if 'type' in keys or 'nullable' in keys or 'name' in keys:
|
2009-01-25 12:52:33 +00:00
|
|
|
self._run_subvisit(delta, self._visit_column_change)
|
2008-08-28 01:17:44 +00:00
|
|
|
if 'server_default' in keys:
|
2008-02-06 18:39:07 +00:00
|
|
|
# Column name might have changed above
|
2009-01-25 12:52:33 +00:00
|
|
|
col_name = delta.get('name', delta.current_name)
|
|
|
|
self._run_subvisit(delta, self._visit_column_default,
|
|
|
|
col_name=col_name)
|
|
|
|
|
|
|
|
def _visit_column_change(self, table_name, col_name, delta):
|
|
|
|
if not hasattr(delta, 'result_column'):
|
|
|
|
# Mysql needs the whole column definition, not just a lone
|
|
|
|
# name/type
|
2008-02-06 18:39:07 +00:00
|
|
|
raise exceptions.NotSupportedError(
|
|
|
|
"A column object is required to do this")
|
2009-01-25 12:52:33 +00:00
|
|
|
|
2008-02-06 18:39:07 +00:00
|
|
|
column = delta.result_column
|
2009-01-25 12:52:33 +00:00
|
|
|
# needed by get_column_specification
|
|
|
|
if not column.table:
|
|
|
|
column.table = delta.table
|
2008-02-06 18:39:07 +00:00
|
|
|
colspec = self.get_column_specification(column)
|
2009-06-11 22:27:38 +00:00
|
|
|
# TODO: we need table formating here
|
|
|
|
self.start_alter_table(self.preparer.quote(table_name, True))
|
2008-02-06 18:39:07 +00:00
|
|
|
self.append("CHANGE COLUMN ")
|
2009-06-11 22:27:38 +00:00
|
|
|
self.append(self.preparer.quote(col_name, True))
|
2008-02-06 18:39:07 +00:00
|
|
|
self.append(' ')
|
|
|
|
self.append(colspec)
|
2009-01-25 12:52:33 +00:00
|
|
|
|
|
|
|
def visit_index(self, param):
|
2008-02-06 18:39:07 +00:00
|
|
|
# If MySQL can do this, I can't find how
|
|
|
|
raise exceptions.NotSupportedError("MySQL cannot rename indexes")
|
2009-01-25 12:52:33 +00:00
|
|
|
|
|
|
|
|
2008-02-06 18:39:07 +00:00
|
|
|
class MySQLConstraintGenerator(ansisql.ANSIConstraintGenerator):
|
2009-06-11 22:27:38 +00:00
|
|
|
pass
|
2009-01-25 12:52:33 +00:00
|
|
|
|
|
|
|
|
2008-02-06 18:39:07 +00:00
|
|
|
class MySQLConstraintDropper(ansisql.ANSIConstraintDropper):
|
|
|
|
#def visit_constraint(self,constraint):
|
|
|
|
# if isinstance(constraint,sqlalchemy.schema.PrimaryKeyConstraint):
|
|
|
|
# return self._visit_constraint_pk(constraint)
|
|
|
|
# elif isinstance(constraint,sqlalchemy.schema.ForeignKeyConstraint):
|
|
|
|
# return self._visit_constraint_fk(constraint)
|
|
|
|
# return super(MySQLConstraintDropper,self).visit_constraint(constraint)
|
2009-01-25 12:52:33 +00:00
|
|
|
|
|
|
|
def visit_migrate_primary_key_constraint(self, constraint):
|
2008-02-06 18:39:07 +00:00
|
|
|
self.start_alter_table(constraint)
|
|
|
|
self.append("DROP PRIMARY KEY")
|
|
|
|
self.execute()
|
|
|
|
|
2009-01-25 12:52:33 +00:00
|
|
|
def visit_migrate_foreign_key_constraint(self, constraint):
|
2008-02-06 18:39:07 +00:00
|
|
|
self.start_alter_table(constraint)
|
|
|
|
self.append("DROP FOREIGN KEY ")
|
2009-06-11 22:27:38 +00:00
|
|
|
self.append(self.preparer.format_constraint(constraint))
|
2008-02-06 18:39:07 +00:00
|
|
|
self.execute()
|
|
|
|
|
2009-01-25 12:52:33 +00:00
|
|
|
|
2008-02-06 18:39:07 +00:00
|
|
|
class MySQLDialect(ansisql.ANSIDialect):
|
|
|
|
columngenerator = MySQLColumnGenerator
|
|
|
|
columndropper = MySQLColumnDropper
|
|
|
|
schemachanger = MySQLSchemaChanger
|
|
|
|
constraintgenerator = MySQLConstraintGenerator
|
|
|
|
constraintdropper = MySQLConstraintDropper
|