-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqla_copy.py
125 lines (98 loc) · 4.53 KB
/
sqla_copy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
FROM https://groups.google.com/d/msg/sqlalchemy/wb2M_oYkQdY/SsZL8Q1KBAAJ
"""
from sqlalchemy.inspection import inspect
from sqlalchemy.orm import class_mapper
import logging
log = logging.getLogger(__name__)
def relationships_of(entity):
return inspect(entity).mapper.relationships
def copy_sqla_object(obj, omit_fk=True):
"""
Given an SQLAlchemy object, creates a new object (FOR WHICH THE OBJECT
MUST SUPPORT CREATION USING __init__() WITH NO PARAMETERS), and copies
across all attributes, omitting PKs, FKs (by default), and relationship
attributes.
"""
cls = type(obj)
mapper = class_mapper(cls)
newobj = cls() # not: cls.__new__(cls)
pk_keys = set([c.key for c in mapper.primary_key])
rel_keys = set([c.key for c in mapper.relationships])
prohibited = pk_keys | rel_keys
if omit_fk:
fk_keys = set([c.key for c in mapper.columns if c.foreign_keys])
prohibited = prohibited | fk_keys
log.debug(f"copy_sqla_object: skipping: {prohibited}")
for k in [p.key for p in mapper.iterate_properties
if p.key not in prohibited]:
try:
value = getattr(obj, k)
log.debug(f"copy_sqla_object: processing attribute {k} = {value}")
setattr(newobj, k, value)
except AttributeError:
log.debug(f"copy_sqla_object: failed attribute {k}")
pass
return newobj
def deepcopy_sqla_object(startobj, session):
"""
For this to succeed, the object must take a __init__ call with no
arguments. (We can't specify the required args/kwargs, since we are copying
a tree of arbitrary objects.)
"""
objmap = {} # keys = old objects, values = new objects
log.debug("deepcopy_sqla_object: pass 1: create new objects")
# Pass 1: iterate through all objects. (Can't guarantee to get
# relationships correct until we've done this, since we don't know whether
# or where the "root" of the PK tree is.)
stack = [startobj]
while stack:
oldobj = stack.pop(0)
if oldobj in objmap: # already seen
continue
log.debug(f"deepcopy_sqla_object: copying {oldobj}")
newobj = copy_sqla_object(oldobj)
# Don't insert the new object into the session here; it may trigger
# an autoflush as the relationships are queried, and the new objects
# are not ready for insertion yet (as their relationships aren't set).
# Not also the session.no_autoflush option:
# "sqlalchemy.exc.OperationalError: (raised as a result of Query-
# invoked autoflush; consider using a session.no_autoflush block if
# this flush is occurring prematurely)..."
objmap[oldobj] = newobj
for relationship in relationships_of(oldobj):
log.debug(f"deepcopy_sqla_object: ... relationship: {relationship}")
related = getattr(oldobj, relationship.key)
if relationship.uselist:
stack.extend(related)
elif related is not None:
stack.append(related)
# Pass 2: set all relationship properties.
log.debug("deepcopy_sqla_object: pass 2: set relationships")
for oldobj, newobj in objmap.items():
log.debug(f"deepcopy_sqla_object: newobj: {newobj}")
# insp.mapper.relationships is of type
# sqlalchemy.utils._collections.ImmutableProperties, which is basically
# a sort of AttrDict.
for relationship in relationships_of(oldobj):
# The relationship is an abstract object (so getting the
# relationship from the old object and from the new, with e.g.
# newrel = newinsp.mapper.relationships[oldrel.key],
# yield the same object. All we need from it is the key name.
log.debug(f"deepcopy_sqla_object: ... relationship: {relationship.key}")
related_old = getattr(oldobj, relationship.key)
if relationship.uselist:
related_new = [objmap[r] for r in related_old]
elif related_old is not None:
related_new = objmap[related_old]
else:
related_new = None
log.debug(f"deepcopy_sqla_object: ... ... adding: {related_new}")
setattr(newobj, relationship.key, related_new)
# Now we can do session insert.
log.debug("deepcopy_sqla_object: pass 3: insert into session")
for newobj in objmap.values():
session.add(newobj)
# Done
log.debug("deepcopy_sqla_object: done")
return objmap[startobj] # returns the new object matching startobj