Re: [pylons-discuss] Re: using pyramid without an ORM ?

2022-08-16 Thread Mike Orr
The SQLite database is pregenerated for a release and contains only
reference information. It's read only to the web application. So I'm
wondering if it's worth even hooking the session into the transaction
manager at all. I have a request subclass, and to open a session I use
a reified method:

@reify
def sa_session(self):
engine = self.registry.sa_engine   # Attribute set during startup
configuration.
info = {"request": self}# Because this was in the tutorial.
sess = sqlalchemy.orm.Session(engine, info=info)
zope.sqlalchemy.register(sess)# Is this worth doing for a
read-only database?
return sess

The transaction manager closes the session for me, so without it I
guess I'd have to have a subscriber that rolls back and closes the
request. I don't want to have to do it in every view because it's not
view-specific logic.

On Tue, Aug 16, 2022 at 9:19 AM 'Jonathan Vanasco' via pylons-discuss
 wrote:
>
> On Tuesday, August 16, 2022 at 11:45:24 AM UTC-4 Mike Orr wrote:
> > It is rolling back in some of my testing when there's no
> > insert/delete/update, but I want to make sure it always does, just in
> > case something somehow modifies the database when we didn't intend to.
> > It's not that big a deal but it's what I'd like. I'm not sure if
> > SQLAlchemy is issuing rollback if there were no changes, or if it will
> > always do so.
>
> That's from SQLAlchemy. It will rollback if there were no database writes.  
> SQLAlchemy is unaware of raw sql being a write operation, so you need to use 
> the `mark_changed` function from zope.sqlalchemy.   This is a weird 
> idiosyncrasy of SQLAlchemy and transaction - the transaction could be 
> completely successful, but SQLAlchemy will rollback because there was no 
> activity within it's scope.
>
> It sounds like you're trying to do the opposite of what the `transaction` 
> package is designed to do.
>
> The way I normally deal with situations like that is to control if SQLAlchemy 
> joins the transaction or not.  In most projects, I only use the transaction 
> on specific views that require this type of integration - such as anything 
> that sends an email (pyramid_mailer integrates with pyramid_tm).
>
> It also sounds like your concern is mostly in testing.  The approach I've 
> started to standardize on is to have a database snapshot for tests and just 
> recreate the database from that on every run.  If you just want to completely 
> disable database commits though, you could have your test harness set up a 
> SQLAlchemy event listener for "commit", and then issue a "rollback" within 
> the event.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to pylons-discuss+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/pylons-discuss/771e180a-ca5b-4625-baf7-972d237ea45an%40googlegroups.com.



-- 
Mike Orr 

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pylons-discuss+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3Duq9HZVsyVJir4QzG0v8AXs-MURjxNAZ6uqH2LPkdiEW1A%40mail.gmail.com.


Re: [pylons-discuss] Re: using pyramid without an ORM ?

2022-08-16 Thread Michael Merickel
transaction.doom() is a good way. Another is to register a commit_veto hook in 
pyramid_tm. It is a hook that is invoked any time it would be about to commit, 
giving you a chance to stop it. Advantage of the veto is that you can register 
it from settings globally. 

- Michael

> On Aug 16, 2022, at 10:42, Mike Orr  wrote:
> 
> On Mon, Aug 15, 2022 at 3:46 PM Jonathan Vanasco  wrote:
>> 
>> 
>> I second what Michael said.  The sqlalchemy starter template is the right 
>> way to go.
>> 
>> The major thing this template does, is provide you with the glue between a 
>> SQLAlchemy "Session" and the pyramid request.  See : 
>> https://github.com/Pylons/pyramid-cookiecutter-starter/blob/latest/%7B%7Bcookiecutter.repo_name%7D%7D/%7B%7Bcookiecutter.repo_name%7D%7D/sqlalchemy_models/__init__.py#L87-L127
>> 
>> If you run pyramid_tm (https://pypi.org/project/pyramid-tm/) you can then 
>> use zope.sqlalchemy (https://pypi.org/project/zope.sqlalchemy/) to bind the 
>> session to the transaction.
>> 
>> You don't need to use SQLAlchemy's ORM.  You can just use SQLAlchemy Core 
>> (https://docs.sqlalchemy.org/en/14/core/) to do everything.  You can also 
>> access the underlying psycopg2 connections through SQLAlchemy when you want.
> 
> Is there a way to make 'pyramid_tm' always roll back even on success?
> I looked in the code and there seemed to be no flag for that, and I'd
> either have to call 'transaction.doom()' in a subscriber or patch the
> obscure code several levels deep.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to pylons-discuss+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3DuqT58npmt6_sbfXJpVZ4hX7PNJdj1WqYXeXo7rJfhwPQQ%40mail.gmail.com.

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pylons-discuss+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/D770A14C-D365-42CC-9DCB-9D596E7B6B80%40gmail.com.


Re: [pylons-discuss] Re: using pyramid without an ORM ?

2022-08-16 Thread 'Jonathan Vanasco' via pylons-discuss
On Tuesday, August 16, 2022 at 11:45:24 AM UTC-4 Mike Orr wrote:
> It is rolling back in some of my testing when there's no
> insert/delete/update, but I want to make sure it always does, just in
> case something somehow modifies the database when we didn't intend to.
> It's not that big a deal but it's what I'd like. I'm not sure if
> SQLAlchemy is issuing rollback if there were no changes, or if it will
> always do so.

That's from SQLAlchemy. It will rollback if there were no database writes.  
SQLAlchemy is unaware of raw sql being a write operation, so you need to 
use the `mark_changed` function from zope.sqlalchemy.   This is a weird 
idiosyncrasy of SQLAlchemy and transaction - the transaction could be 
completely successful, but SQLAlchemy will rollback because there was no 
activity within it's scope. 

It sounds like you're trying to do the opposite of what the `transaction` 
package is designed to do.

The way I normally deal with situations like that is to control if 
SQLAlchemy joins the transaction or not.  In most projects, I only use the 
transaction on specific views that require this type of integration - such 
as anything that sends an email (pyramid_mailer integrates with pyramid_tm).

It also sounds like your concern is mostly in testing.  The approach I've 
started to standardize on is to have a database snapshot for tests and just 
recreate the database from that on every run.  If you just want to 
completely disable database commits though, you could have your test 
harness set up a SQLAlchemy event listener for "commit", and then issue a 
"rollback" within the event.

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pylons-discuss+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/771e180a-ca5b-4625-baf7-972d237ea45an%40googlegroups.com.


Re: [pylons-discuss] Re: using pyramid without an ORM ?

2022-08-16 Thread Mike Orr
It is rolling back in some of my testing when there's no
insert/delete/update, but I want to make sure it always does, just in
case something somehow modifies the database when we didn't intend to.
It's not that big a deal but it's what I'd like. I'm not sure if
SQLAlchemy is issuing rollback if there were no changes, or if it will
always do so.

On Tue, Aug 16, 2022 at 8:42 AM Mike Orr  wrote:
>
> On Mon, Aug 15, 2022 at 3:46 PM Jonathan Vanasco  wrote:
> >
> >
> > I second what Michael said.  The sqlalchemy starter template is the right 
> > way to go.
> >
> > The major thing this template does, is provide you with the glue between a 
> > SQLAlchemy "Session" and the pyramid request.  See : 
> > https://github.com/Pylons/pyramid-cookiecutter-starter/blob/latest/%7B%7Bcookiecutter.repo_name%7D%7D/%7B%7Bcookiecutter.repo_name%7D%7D/sqlalchemy_models/__init__.py#L87-L127
> >
> > If you run pyramid_tm (https://pypi.org/project/pyramid-tm/) you can then 
> > use zope.sqlalchemy (https://pypi.org/project/zope.sqlalchemy/) to bind the 
> > session to the transaction.
> >
> > You don't need to use SQLAlchemy's ORM.  You can just use SQLAlchemy Core 
> > (https://docs.sqlalchemy.org/en/14/core/) to do everything.  You can also 
> > access the underlying psycopg2 connections through SQLAlchemy when you want.
>
> Is there a way to make 'pyramid_tm' always roll back even on success?
> I looked in the code and there seemed to be no flag for that, and I'd
> either have to call 'transaction.doom()' in a subscriber or patch the
> obscure code several levels deep.



-- 
Mike Orr 

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pylons-discuss+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3Dup49Ag0ZZgTQ9j%3DRMAFqP-PCcF9AN8zSRiQqcefrwNVcw%40mail.gmail.com.


Re: [pylons-discuss] Re: using pyramid without an ORM ?

2022-08-16 Thread Mike Orr
On Mon, Aug 15, 2022 at 3:46 PM Jonathan Vanasco  wrote:
>
>
> I second what Michael said.  The sqlalchemy starter template is the right way 
> to go.
>
> The major thing this template does, is provide you with the glue between a 
> SQLAlchemy "Session" and the pyramid request.  See : 
> https://github.com/Pylons/pyramid-cookiecutter-starter/blob/latest/%7B%7Bcookiecutter.repo_name%7D%7D/%7B%7Bcookiecutter.repo_name%7D%7D/sqlalchemy_models/__init__.py#L87-L127
>
> If you run pyramid_tm (https://pypi.org/project/pyramid-tm/) you can then use 
> zope.sqlalchemy (https://pypi.org/project/zope.sqlalchemy/) to bind the 
> session to the transaction.
>
> You don't need to use SQLAlchemy's ORM.  You can just use SQLAlchemy Core 
> (https://docs.sqlalchemy.org/en/14/core/) to do everything.  You can also 
> access the underlying psycopg2 connections through SQLAlchemy when you want.

Is there a way to make 'pyramid_tm' always roll back even on success?
I looked in the code and there seemed to be no flag for that, and I'd
either have to call 'transaction.doom()' in a subscriber or patch the
obscure code several levels deep.

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pylons-discuss+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3DuqT58npmt6_sbfXJpVZ4hX7PNJdj1WqYXeXo7rJfhwPQQ%40mail.gmail.com.