MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / create_version

Function create_version

examples/versioned_history/history_meta.py:261–340  ·  view source on GitHub ↗
(obj, session, deleted=False)

Source from the content-addressed store, hash-verified

259
260
261def create_version(obj, session, deleted=False):
262 obj_mapper = object_mapper(obj)
263 history_mapper = obj.__history_mapper__
264 history_cls = history_mapper.class_
265
266 obj_state = attributes.instance_state(obj)
267
268 attr = {}
269
270 obj_changed = False
271
272 for om, hm in zip(
273 obj_mapper.iterate_to_root(), history_mapper.iterate_to_root()
274 ):
275 if hm.single:
276 continue
277
278 for hist_col in hm.local_table.c:
279 if _is_versioning_col(hist_col):
280 continue
281
282 obj_col = om.local_table.c[hist_col.key]
283
284 # get the value of the
285 # attribute based on the MapperProperty related to the
286 # mapped column. this will allow usage of MapperProperties
287 # that have a different keyname than that of the mapped column.
288 try:
289 prop = obj_mapper.get_property_by_column(obj_col)
290 except UnmappedColumnError:
291 # in the case of single table inheritance, there may be
292 # columns on the mapped table intended for the subclass only.
293 # the "unmapped" status of the subclass column on the
294 # base class is a feature of the declarative module.
295 continue
296
297 # expired object attributes and also deferred cols might not
298 # be in the dict. force it to load no matter what by
299 # using getattr().
300 if prop.key not in obj_state.dict:
301 getattr(obj, prop.key)
302
303 a, u, d = attributes.get_history(obj, prop.key)
304
305 if d:
306 attr[prop.key] = d[0]
307 obj_changed = True
308 elif u:
309 attr[prop.key] = u[0]
310 elif a:
311 # if the attribute had no value.
312 attr[prop.key] = a[0]
313 obj_changed = True
314
315 if not obj_changed:
316 # not changed, but we have relationships. OK
317 # check those too
318 for prop in obj_mapper.iterate_properties:

Callers 1

before_flushFunction · 0.85

Calls 8

object_mapperFunction · 0.90
_is_versioning_colFunction · 0.85
iterate_to_rootMethod · 0.80
has_changesMethod · 0.80
get_historyMethod · 0.45
itemsMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected