Skip to content

Commit

Permalink
cleanup get_old_row, prepare for 0.5.1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
valhuber committed Dec 22, 2020
1 parent 4e2e27c commit de4a352
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 35 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ allocate a payment to a set of outstanding orders

0.5.0 - Support for `Referential Integrity <https://github.com/valhuber/LogicBank/wiki/Sample-Project---Allocation>`_,
with examples.

0.5.1 - Support domain object constructors with complex (side effects)
__init__ behavior; use row_mapper.column_attrs (not all_orm_descriptors)
to avoid 'flush already in progress' when using flask_sqlalchemy
47 changes: 13 additions & 34 deletions logic_bank/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,17 @@ def __str__(self):
def get_old_row(obj, session) -> ObjectView: # FIXME design verify approach
"""
obtain old_row (during before_flush) from sqlalchemy row
"""

thanks
https://docs.sqlalchemy.org/en/13/_modules/examples/versioned_history/history_meta.html
https://goodcode.io/articles/python-dict-object/
or https://stackoverflow.com/questions/28871406/how-to-clone-a-sqlalchemy-db-object-with-new-primary-key
product_obj = products.all()[0]
db.session.expunge(product_obj) # expunge the object from session
make_transient(product_obj) # http://docs.sqlalchemy.org/en/rel_1_1/orm/session_api.html#sqlalchemy.orm.session.make_transient
class DotDict(dict):
"""dot.notation access to dictionary attributes"""
# thanks: https://stackoverflow.com/questions/2352181/how-to-use-a-dot-to-access-members-of-dictionary/28463329
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__

product_obj.product_uid = 'something'
db.session.add(product_obj)
"""
use_transient = False # failed experiment - disabled, but keeping it around
if use_transient:
old_row = type(obj)()
# session.expunge(old_row) # causes Instance <Order at 0x10aedf490> is not present in this Session
else:
old_row = {}
use_dot_dict = True # failed experiment - disabled, but keeping it around
old_row = DotDict({})
obj_state = attributes.instance_state(obj)
obj_mapper = object_mapper(obj)
for each_map in obj_mapper.iterate_to_root():
Expand All @@ -84,26 +75,14 @@ def get_old_row(obj, session) -> ObjectView: # FIXME design verify approach
# todo prefers .AttributeState.history -- how to code??

if d: # changed, and this is the old value
if use_transient:
setattr(old_row, prop.key, d[0])
else:
old_row[prop.key] = d[0]
old_row[prop.key] = d[0]
obj_changed = True
elif u: # unchanged
if use_transient:
setattr(old_row, prop.key, u[0])
else:
old_row[prop.key] = u[0]
old_row[prop.key] = u[0]
elif a: # added (old value null) if the attribute had no value.
if use_transient:
setattr(old_row, prop.key, a[0])
else:
old_row[prop.key] = a[0]
old_row[prop.key] = a[0]
obj_changed = True
if use_transient:
return old_row # expunge me!!
else:
return ObjectView(old_row)
return old_row


def hydrate_row(a_row: base) -> base:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def desc():

setup(
name="logicbank",
version="0.5.0",
version="0.5.1",
url="https://github.com/valhuber/logicbank",
license="BSD",
author="Val Huber",
Expand Down

0 comments on commit de4a352

Please sign in to comment.