genenetwork / genenetwork2

GeneNetwork (2nd generation)
http://gn2.genenetwork.org/
GNU Affero General Public License v3.0
34 stars 24 forks source link

Feature/add support for db transactions #721

Closed BonfaceKilz closed 2 years ago

BonfaceKilz commented 2 years ago

Description

This PR enables GN2 to support roll-backs should anything wrong happen within a connection context (as opposed to the cursor context). For an overview of how context managers - the inspiration behind this PR - please look at: https://www.youtube.com/watch?v=ucGpcA9r4hU

To control roll-backs, we need to:

Regardless of what engine we use, this PR has no bad side-effect and there's no code changes else where that's required.

How should this be tested?

Example usage: Assuming InnoDB:

with database_connection() as conn:
    with conn.cursor() as cursor:
        cursor.execute(
            """
INSERT INTO CaseAttribute (Name, Description) VALUES (%s, %s)
            """, ("Test", "Random Description")
        )
        print(cursor.fetchall())
        print(0/0)

The above query will be rolled back. On MyISAM and other db engines that don't have transactional support, the above will still be executed and "connection.rollback()" will be silently ignored.