given a list of sql statements, that shall be executed as a unit. How can I achieve a rollback of all previously succesfully executed statements, if any statement fails?
Example:
from sqlalchemy import create_engine, text
engine = create_engine("sqlite:///foo.db")
s1 = text("CREATE TABLE mytable (x int, y int)")
s2 = text("ALTER TABLE myothertable ADD z int;")
with engine.connect() as conn:
conn.execute(s1)
conn.execute(s2)
conn.execute(s2) will abviously fail, because myothertable does not exist. In this case, s1 should also rolled back.
Thank you in advance.
If a single statement fails, then this statement will not be executed. But I can in an out of synch error, if I try something that should work, like this:
s12 = text("CREATE TABLE mytable (x int, y int); ALTER TABLE mytable ADD z int;")
with engine.connect() as conn:
conn.execute(s12)
But here it is stated, that sqlalchemy cannot handle this.