From 660cc7e978be22c81752dd814aa21d9f56586e46 Mon Sep 17 00:00:00 2001 From: Adam Glasgall Date: Thu, 21 Mar 2013 21:58:26 -0700 Subject: [PATCH] work around removal of deprecated SQLAlchemy APIs --- debian/changelog | 6 ++++++ python/database/models.py | 51 ++++++++++++++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/debian/changelog b/debian/changelog index f9a0efd..9776bf4 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +invirt-database (0.2.8~glasgall7) precise; urgency=low + + * Add compatibility hack to work around removal of deprecated APIs + + -- Adam Glasgall Thu, 21 Mar 2013 21:56:13 -0700 + invirt-database (0.2.8~glasgall6) precise; urgency=low * Remove reference to sqlalchemy.ext.assignmapper, which was removed diff --git a/python/database/models.py b/python/database/models.py index f3c1862..236598a 100644 --- a/python/database/models.py +++ b/python/database/models.py @@ -6,6 +6,22 @@ import datetime from invirt.database import record +# Compatibility hack from +# http://www.sqlalchemy.org/trac/wiki/UsageRecipes/SessionAwareMapper +from sqlalchemy.orm import mapper as sqla_mapper + +def session_mapper(scoped_session): + def mapper(cls, *arg, **kw): + if cls.__init__ is object.__init__: + def __init__(self, **kwargs): + for key, value in kwargs.items(): + setattr(self, key, value) + cls.__init__ = __init__ + cls.query = scoped_session.query_property() + return sqla_mapper(cls, *arg, **kw) + return mapper + + __all__ = ['meta', 'session', 'clear_cache', @@ -151,23 +167,24 @@ class Build(record.Record): _identity_field = 'build_id' from invirt.database.owner import Owner - -session.mapper(Machine, machine_table, - properties={'nics': relation(NIC, backref="machine"), - 'disks': relation(Disk, backref="machine"), - 'type': relation(Type), - 'acl': relation(MachineAccess, backref="machine", passive_deletes=True, cascade="all, delete-orphan")}); -session.mapper(MachineAccess, machine_access_table) -session.mapper(NIC, nic_table) -session.mapper(Disk, disk_table) -session.mapper(Type, types_table) -session.mapper(Mirror, mirrors_table) -session.mapper(CDROM, cdroms_table, - properties={'mirror': relation(Mirror, backref="cdroms")}) -session.mapper(Autoinstall, autoinstalls_table) -session.mapper(Owner, owners_table) -session.mapper(Admin, admins_table) -session.mapper(Build, builds_table) +mapper = session_mapper(session) + +mapper(Machine, machine_table, + properties={'nics': relation(NIC, backref="machine"), + 'disks': relation(Disk, backref="machine"), + 'type': relation(Type), + 'acl': relation(MachineAccess, backref="machine", passive_deletes=True, cascade="all, delete-orphan")}); +mapper(MachineAccess, machine_access_table) +mapper(NIC, nic_table) +mapper(Disk, disk_table) +mapper(Type, types_table) +mapper(Mirror, mirrors_table) +mapper(CDROM, cdroms_table, + properties={'mirror': relation(Mirror, backref="cdroms")}) +mapper(Autoinstall, autoinstalls_table) +mapper(Owner, owners_table) +mapper(Admin, admins_table) +mapper(Build, builds_table) def clear_cache(): """Clear sqlalchemy's cache -- 1.7.9.5