IBM / dse-decision-optimization-utilities

A Python package with various utilities that speed-up development of Decision Optimization projects in Watson Studio. Implements common tasks and provides best-practice guidelines to architect DO projects.
Apache License 2.0
4 stars 7 forks source link

ScenarioDbManager - delete using session/cursor #22

Closed victorterpstra closed 2 years ago

victorterpstra commented 2 years ago

Delete all tables within one session/cursor, so we don't have to worry about the order of the delete?

See: https://docs.sqlalchemy.org/en/14/orm/session.html

victorterpstra commented 2 years ago

To use a session and execute RAW SQL, see: https://stackoverflow.com/questions/17972020/how-to-execute-raw-sql-in-flask-sqlalchemy-app

Combining the 2 references (?):

with Session(engine) as session, session.begin():
    session.execute(some_sql)
    session.execute(some_other_sql)
victorterpstra commented 2 years ago

Added support for Transactions in v0.5.3.0b Based on https://docs.sqlalchemy.org/en/14/core/connections.html (i.e. NOT using Session)

Basic usage in code:

with self.engine.begin() as connection:
    connection.execute(sql)

Unfortunately, using transactions does NOT avoid FK constraint violations when:

The effects are different in SQLite vs DB2:

There is a flag 'deferrable' in ForeignKeyConstraint that is supposed to tell the DB to defer FK constraint checking to after the transaction is complete. But that doesn't seem to work in SQLite. Worse in DB2: this flag causes an exception creating the schema.

victorterpstra commented 2 years ago

Validated in v0.5.3.0b that FK violations (or any other exception) will cause a rollback for replace_scenario_in_db The following APIs on ScenarioDbManager support transactions (and rollbacks):