I’d like to pick up this topic once more briefly. Suppose I get the “new”, “dirty”, and “deleted” sets as per discussion below, and I’m especially interested in the “dirty” set: is there a way to find out which properties of an object were modified, or only that the object was modified?
Thanks! Jens On Wednesday, November 22, 2017 at 9:46:54 AM UTC+10, jens.t...@gmail.com wrote: > > Thank you, the event worked like a charm :-) Though I think that I don't > need the commit events, because the application terminates anyway. > > I modified your approach to gather which objects were flushed so that in > the end I can give the user more precise information: > > dbsession.info["new"] = set() > > dbsession.info["dirty"] = set() > > dbsession.info["deleted"] = set() > > > > def update_session_info(session): > > new = session.info["new"] > > new |= set(session.new) > > dirty = session.info["dirty"] > > dirty |= set(session.dirty) > > deleted = session.info["deleted"] > > deleted |= set(session.deleted) > > return new, dirty, deleted > > > > @event.listens_for(dbsession, "before_flush") > > def on_before_flush(session, _, _): > > update_session_info(session) > > ... > code.interact(local=locals()) > > ... > > new, dirty, deleted = update_session_info(dbsession) > > if new or dirty or deleted: > > if new: > > print("The following objects were created: ", new) > > if dirty: > > print("The following objects were modified: ", dirty) > > if deleted: > > print("The following objects were deleted: ", deleted) > > yesno = input("Would you like to commit this transaction? > [y/N] ") > if yesno == "y": > > print("Committing transaction...") > > else: > > print("Rolling back transaction...") > > raise _SessionRollbackException() > > > # ...this is where the context closes and the transaction commits > and the dbsession ends. > > Cheers, > Jens > > > > On Saturday, November 18, 2017 at 12:03:05 AM UTC+10, Simon King wrote: >> >> OK, I think tracking session events seems reasonable. You could do >> something like this (completely untested): >> >> from sqalchemy.event import event >> >> @event.listens_for(YourSessionOrSessionMaker, 'before_flush') >> def on_before_flush(session, flush_context, instances): >> session.info['flushed'] = True >> >> >> # You'd probably also want to reset the 'flushed' flag >> # after a commit or rollback >> @event.listens_for(YourSessionOrSessionMaker, 'after_commit') >> @event.listens_for(YourSessionOrSessionMaker, 'after_rollback') >> def on_session_reset(session): >> session.info['flushed'] = False >> >> >> # when user exits interactive session: >> modified = ( >> session.info.get('flushed', False) >> or session.deleted >> or session.new >> or session.dirty >> ) >> if modified: >> raw_input('do you want to commit?') >> >> >> ...but note that if you ever execute raw SQL (ie. >> session.execute('UPDATE x WHERE y')), that will not be noticed by >> those events. >> >> Hope that helps, >> >> Simon >> > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.