summaryrefslogtreecommitdiff
path: root/oslo_db/sqlalchemy/compat
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2017-08-10 17:21:46 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2017-08-10 17:23:59 -0400
commitc4843bcf1479f1a4b1438af5a4a314ce1aaaae72 (patch)
tree42670c5953b904d24d0253550acfe5108b939d49 /oslo_db/sqlalchemy/compat
parent92c5091d4c199ef18a8a3572e22ac6870af17c0c (diff)
downloadoslo-db-c4843bcf1479f1a4b1438af5a4a314ce1aaaae72.tar.gz
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
Diffstat (limited to 'oslo_db/sqlalchemy/compat')
-rw-r--r--oslo_db/sqlalchemy/compat/utils.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/oslo_db/sqlalchemy/compat/utils.py b/oslo_db/sqlalchemy/compat/utils.py
index e34e70f..1d27a56 100644
--- a/oslo_db/sqlalchemy/compat/utils.py
+++ b/oslo_db/sqlalchemy/compat/utils.py
@@ -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)