Workaround non-compatible type.adapt() for SQLAlchemy < 1.1

The ndb change that makes use of String.adapt() to produce a new
object breaks on SQLAlchemy < 1.1 because adapt() there does
not allow for kwargs to override those already
present in the type.

Change-Id: I8cc79dc9bebcb3cb3accf76559bc969c2dae7ed9
This commit is contained in:
Mike Bayer 2017-08-10 17:21:46 -04:00
parent 92c5091d4c
commit c4843bcf14
2 changed files with 27 additions and 2 deletions

View File

@ -43,3 +43,27 @@ def get_postgresql_enums(conn):
return [e['name'] for e in sqlalchemy.inspect(conn).get_enums()]
else:
return conn.dialect._load_enums(conn).keys()
def adapt_type_object(type_object, target_class, *args, **kw):
"""Call the adapt() method on a type.
For SQLAlchemy 1.0, runs a local version of constructor_copy() that
allows keyword arguments to be overridden.
See https://github.com/zzzeek/sqlalchemy/commit/\
ceeb033054f09db3eccbde3fad1941ec42919a54
"""
if sqla_110:
return type_object.adapt(target_class, *args, **kw)
else:
# NOTE(zzzeek): this only works for basic types, won't work for
# schema types like Enum, Boolean
# NOTE(zzzeek): this code can be removed once requirements
# are at SQLAlchemy >= 1.1
names = sqlalchemy.util.get_cls_kwargs(target_class)
kw.update(
(k, type_object.__dict__[k]) for k in names.difference(kw)
if k in type_object.__dict__)
return target_class(*args, **kw)

View File

@ -17,6 +17,7 @@ import re
import debtcollector.removals
from oslo_db.sqlalchemy.compat import utils as compat_utils
from oslo_db.sqlalchemy.types import String
from sqlalchemy import event, schema
@ -103,8 +104,8 @@ def _compile_ndb_string(element, compiler, **kw):
return compiler.visit_string(element, **kw)
if element.mysql_ndb_length:
effective_type = element.adapt(
_String, length=element.mysql_ndb_length)
effective_type = compat_utils.adapt_type_object(
element, _String, length=element.mysql_ndb_length)
return compiler.visit_string(effective_type, **kw)
elif element.mysql_ndb_type:
effective_type = to_instance(element.mysql_ndb_type)