I'm writing a Python script to move data from production db to dev db. I'm using vertica-python
(something very similar to pyodbc
) for db connection and airflow
for scheduling.
The script is divided into two files, one for DAG and one for the actual migration job. I use try-except-finally
block for all SQL execution functions in the migration job:
try:
# autocommit set to False
# Execute a SQL script
except DatabaseError:
# Logging information
# Rollback
finally:
# autocommit set to False
You can see that setting autocommit
and Rollback
needs to access the connection, and executing a SQL script needs to access the cursor. The current solution is to simply create two DB connections in DAG and pass them to the migration script. But I also read from a Stackoverflow post that I should pass only the cursor:
Python, sharing mysql connection in multiple functions - pass connection or cursor?
My question is: Is it possible to only pass the cursor
from the DAG to the migration script, and still retain the ability to rollback
and setting autocommit
?
question from:
https://stackoverflow.com/questions/65886146/should-i-pass-database-connection-or-cursor-to-a-class 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…