2009-01-25 12:52:33 +00:00
|
|
|
"""
|
|
|
|
MySQL database specific implementations of changeset classes.
|
|
|
|
"""
|
|
|
|
|
2014-03-02 14:03:51 +08:00
|
|
|
import sqlalchemy
|
2008-02-06 18:39:07 +00:00
|
|
|
from sqlalchemy.databases import mysql as sa_base
|
2010-01-07 20:10:47 +01:00
|
|
|
from sqlalchemy import types as sqltypes
|
2009-06-12 22:43:02 +00:00
|
|
|
|
2010-09-07 02:25:29 +02:00
|
|
|
from migrate import exceptions
|
2011-10-28 10:59:10 +02:00
|
|
|
from migrate.changeset import ansisql
|
2014-03-02 14:03:51 +08:00
|
|
|
from migrate.changeset import util
|
|
|
|
|
2010-09-07 02:25:29 +02:00
|
|
|
|
|
|
|
|
2011-10-28 10:59:10 +02:00
|
|
|
MySQLSchemaGenerator = sa_base.MySQLDDLCompiler
|
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):
|
|
|
|
|
2009-06-27 14:13:27 +00:00
|
|
|
def visit_column(self, delta):
|
|
|
|
table = delta.table
|
|
|
|
colspec = self.get_column_specification(delta.result_column)
|
2010-01-07 20:10:47 +01:00
|
|
|
if delta.result_column.autoincrement:
|
2010-05-02 20:31:50 +02:00
|
|
|
primary_keys = [c for c in table.primary_key.columns
|
2010-01-07 20:10:47 +01:00
|
|
|
if (c.autoincrement and
|
|
|
|
isinstance(c.type, sqltypes.Integer) and
|
2010-05-02 20:31:50 +02:00
|
|
|
not c.foreign_keys)]
|
2010-01-07 20:10:47 +01:00
|
|
|
|
2010-05-02 20:31:50 +02:00
|
|
|
if primary_keys:
|
|
|
|
first = primary_keys.pop(0)
|
|
|
|
if first.name == delta.current_name:
|
|
|
|
colspec += " AUTO_INCREMENT"
|
2019-01-16 15:54:39 -05:00
|
|
|
old_col_name = self.preparer.quote(delta.current_name)
|
2009-01-25 12:52:33 +00:00
|
|
|
|
2009-06-20 22:33:03 +00:00
|
|
|
self.start_alter_table(table)
|
|
|
|
|
|
|
|
self.append("CHANGE COLUMN %s " % old_col_name)
|
2008-02-06 18:39:07 +00:00
|
|
|
self.append(colspec)
|
2009-06-20 22:33:03 +00:00
|
|
|
self.execute()
|
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
|
|
|
|
2009-06-20 22:33:03 +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
|
|
|
|
2011-10-28 10:59:10 +02:00
|
|
|
|
|
|
|
class MySQLConstraintDropper(MySQLSchemaGenerator, ansisql.ANSIConstraintDropper):
|
|
|
|
def visit_migrate_check_constraint(self, *p, **k):
|
|
|
|
raise exceptions.NotSupportedError("MySQL does not support CHECK"
|
|
|
|
" constraints, use triggers instead.")
|
2009-06-20 22:33:03 +00:00
|
|
|
|
|
|
|
|
2008-02-06 18:39:07 +00:00
|
|
|
class MySQLDialect(ansisql.ANSIDialect):
|
|
|
|
columngenerator = MySQLColumnGenerator
|
|
|
|
columndropper = MySQLColumnDropper
|
|
|
|
schemachanger = MySQLSchemaChanger
|
|
|
|
constraintgenerator = MySQLConstraintGenerator
|
|
|
|
constraintdropper = MySQLConstraintDropper
|