Use native quote attribute introduced in sqla 0.9

In SQLA 0.9 there is now a native .quote attribute on many objects.
Conditionally use this instead of the old method if the attribute
exists, to remove deprecation messages (and prepare for when the
other way will be fully removed).

Change-Id: I3c5fada13e044c1c4102acc0455226ce1524f2e2
This commit is contained in:
Thomas Goirand 2014-03-02 14:03:51 +08:00 committed by Sean Dague
parent bcb6991615
commit 8d6ce64cd0
4 changed files with 43 additions and 24 deletions

View File

@ -19,6 +19,7 @@ from sqlalchemy.schema import (ForeignKeyConstraint,
from migrate import exceptions
import sqlalchemy.sql.compiler
from migrate.changeset import constraint
from migrate.changeset import util
from sqlalchemy.schema import AddConstraint, DropConstraint
from sqlalchemy.sql.compiler import DDLCompiler
@ -157,8 +158,8 @@ class ANSISchemaChanger(AlterTableVisitor, SchemaGenerator):
def visit_table(self, table):
"""Rename a table. Other ops aren't supported."""
self.start_alter_table(table)
self.append("RENAME TO %s" % self.preparer.quote(table.new_name,
table.quote))
q = util.safe_quote(table)
self.append("RENAME TO %s" % self.preparer.quote(table.new_name, q))
self.execute()
def visit_index(self, index):
@ -227,7 +228,8 @@ class ANSISchemaChanger(AlterTableVisitor, SchemaGenerator):
def start_alter_column(self, table, col_name):
"""Starts ALTER COLUMN"""
self.start_alter_table(table)
self.append("ALTER COLUMN %s " % self.preparer.quote(col_name, table.quote))
q = util.safe_quote(table)
self.append("ALTER COLUMN %s " % self.preparer.quote(col_name, q))
def _visit_column_nullable(self, table, column, delta):
nullable = delta['nullable']
@ -250,7 +252,8 @@ class ANSISchemaChanger(AlterTableVisitor, SchemaGenerator):
def _visit_column_name(self, table, column, delta):
self.start_alter_table(table)
col_name = self.preparer.quote(delta.current_name, table.quote)
q = util.safe_quote(table)
col_name = self.preparer.quote(delta.current_name, q)
new_name = self.preparer.format_column(delta.result_column)
self.append('RENAME COLUMN %s TO %s' % (col_name, new_name))

View File

@ -2,11 +2,14 @@
MySQL database specific implementations of changeset classes.
"""
import sqlalchemy
from sqlalchemy.databases import mysql as sa_base
from sqlalchemy import types as sqltypes
from migrate import exceptions
from migrate.changeset import ansisql
from migrate.changeset import util
MySQLSchemaGenerator = sa_base.MySQLDDLCompiler
@ -34,7 +37,8 @@ class MySQLSchemaChanger(MySQLSchemaGenerator, ansisql.ANSISchemaChanger):
first = primary_keys.pop(0)
if first.name == delta.current_name:
colspec += " AUTO_INCREMENT"
old_col_name = self.preparer.quote(delta.current_name, table.quote)
q = util.safe_quote(table)
old_col_name = self.preparer.quote(delta.current_name, q)
self.start_alter_table(table)

View File

@ -12,6 +12,7 @@ from sqlalchemy.schema import UniqueConstraint
from migrate.exceptions import *
from migrate.changeset import SQLA_07, SQLA_08
from migrate.changeset import util
from migrate.changeset.databases.visitor import (get_engine_visitor,
run_single_visitor)
@ -601,11 +602,12 @@ populated with defaults
# TODO: this is fixed in 0.6
def copy_fixed(self, **kw):
"""Create a copy of this ``Column``, with all attributes."""
q = util.safe_quote(self)
return sqlalchemy.Column(self.name, self.type, self.default,
key=self.key,
primary_key=self.primary_key,
nullable=self.nullable,
quote=self.quote,
quote=q,
index=self.index,
unique=self.unique,
onupdate=self.onupdate,

10
migrate/changeset/util.py Normal file
View File

@ -0,0 +1,10 @@
"""
Safe quoting method
"""
def safe_quote(obj):
# this is the SQLA 0.9 approach
if hasattr(obj, 'name') and hasattr(obj.name, 'quote'):
return obj.name.quote
else:
return obj.quote