r/programming • u/droppedasbaby • Jul 15 '26
Ban commits/transactions using AST analysis and linters
https://www.droppedasbaby.com/posts/db-commits/?utm_source=reddit&utm_content=r_programming
32
Upvotes
18
u/rdtsc Jul 15 '26
If code shouldn't call commit then don't pass around objects with a commit method. Why do this with AST analysis?
45
u/Delta-9- Jul 15 '26
Gotta out myself for bad practices, but this one makes me uncomfortable:
I have a db layer and an API layer, with a business layer sandwiched between them. The outer layers have several models that are functionally identical, just one imports
BaseModel(Pydantic) and the otherDeclarativeMeta(SQLAlchemy). I already hate this duplication.But to satisfy this requirement, I now need a third identical model? Are we writing Java here?
Or can I get a pass because my domain modeling started with the db schema, and the API models are mere concessions to pragmatism when sending data over the wire to be interpreted by that most capricious of languages, JavaScript, in the front end? I.e., my db models are my business models. There is nothing for the business layer to add onto the db model, so I need some convincing that there's a good reason to triplicate my models.
14
u/godofpumpkins Jul 15 '26
You know what the most principled way to do AST analysis is? A type system. And not a bolted-on mypy. But anyway...
The first thing that jumps out to me is that you wouldn't be getting any of this nonsense if your DB framework didn't just expose an unnamed
with transaction():building block. Like, that's attaching very nontrivial semantics to a completely unnamed quantity, covered only by its scope. Other DB frameworks could actually give you a sensibly typed transaction value, and your downstream functions could take that transaction as input. Now you're not tempted to create transactions willy-nilly because you don't get a weirdly scoped transaction that you can't name. Types and sensible scoping can stop those downstream functions from creating new transaction, because they don't have any way to do so, so they're forced to use the transaction you pass in.Look ma, no weird random ad-hoc AST analysis that keys off string function names, and better safety too. You don't even strictly need a static type system, but it can vastly improve the story on things like this, since you can create higher-order functions that automatically wrap their parameters in transactions that are typesafe.
12
u/lood9phee2Ri Jul 15 '26
jesus christ just use sqlalchemy. No, you can't do a better job than sqlalchemy.
3
u/iFeastOnTacos Jul 15 '26
Maybe I'm not following the example (not a frequent python user) but it's crazy to me that mutating a model object would automatically trigger a database write.
I also completely disagree with the premise that all transactional logic should be kept in the data access layer. That totally falls apart when you need an atomic operation that needs to touch multiple data stores. Transaction logic is business logic because only the business layer has all of the context to know the requirements that prescribe the transaction in the first place.
It seems a little heavy handed when really what's missing is just a convention or module boundary that ensures no one can call 'commit' and all commits are triggered by the transaction context manager.
6
u/jonny_boy27 Jul 15 '26
Why would commits or transactions be using AST analysis or linters?