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

On Fri, Nov 17, 2017 at 12:39 PM,  <jens.troe...@gmail.com> wrote:
> Sure.
>
> I'm working with two Pyramid/SQLAlchemy web servers, and in order to have a
> more convenient way of looking at the db data I wrote a small tool which
> essentially creates a db session, loads the server orm helper functions and
> sets up an environment much like a view handler functions has. Then the
> tools calls code.interact() and I have a terminal.
>
> From that terminal I can look at tables, use the server's helper functions
> to read data, but also to write objects.
>
> When I exit interactive mode, I just rolled back the transaction and ended
> the session. However, I'd now like to check: if during that terminal session
> some objects were modified, give the user the choice to either commit() or
> rollback().
>
> To do that, I checked with session.dirty/deleted/new and that's when the
> initial questions arose.
>
> If there are better ways of checking, curious to learn :-)
> Thank you!
>
>
> On Thursday, November 16, 2017 at 11:39:14 PM UTC+10, Simon King wrote:
>>
>> Can you explain why you actually want to do this? There might be
>> better options than before_flush, but we'd need to know exactly what
>> you're trying to do first.
>>
>> 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.

-- 
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.

Reply via email to