【发布时间】:2020-12-10 07:40:28
【问题描述】:
我正在使用以下方式连接到雪花云:
try:
conn = snowCtx.connect(
user=snowflake_user,
password=password,
account=account,
database=database,
schema=schema,
warehouse='COMPUTE_WH',
role='SYSADMIN',
autocommit = False
)
conn.autocommit = False
except (RuntimeError, TypeError, NameError, snowCtx.connection.errors.Error) as e:
print("******* Main error on connection: *******\n"+ str(e))
print("******* Main error on connection: *******\n"+ str(e), file=logfile)
conn.rollback()
我正在循环进入一个数组,并调用一些函数。如果发生错误,我需要回滚特定循环内发生的所有事情
这是循环脚本:
for main_survey_id in survey_ids_list:
print('--- Starting with Data related to the main data.csv survey: '+main_survey+' ---\n')
print('--- Starting with Data related to the main data.csv survey: '+main_survey+' ---\n', file=logfile)
# Other variables sent with the functions:
...
data['SURVEY_ID'] = main_survey_id
add_dataframe = add_dataframe_as_table(conn, cursor, data, survey_new_title)
nested_foreign_key = add_foreign_key(conn, cursor, main_survey, survey_new_title, '_parent_index', '_index')
add_columns = add_survey_columns(conn, cursor, a, survey_new_title, data, main_survey_id)
if((add_columns == True) and (nested_foreign_key==True) and (add_dataframe)==True):
new_row = {'ONA Survey ID': ona_survey_id, 'Snowflake Survey ID': next_val,
'Survey Title': survey_new_title, 'Upload Status': 'SUCCESS', 'ONA id exists': 'True',
'Survey Type': 'Nested Table',
'Survey title exists?': 'False',
'Survey have nested values': have_nested, 'Table Added': 'True',
'Primary Key Added': '', 'Foreign Key Added': nested_foreign_key,
'Number of rows of table': len(data), 'Comments': 'Success: Nested table added to Snowflake'}
log_frame = log_frame.append(new_row, ignore_index=True)
conn.commit()
else:
conn.rollback()
您可以在这里查看是否所有调用的函数都返回了True,那么我需要提交数据库上发生的更改,否则rollback() 并继续进行下一次迭代。
这里还有一个函数脚本:
def add_foreign_key(conn, cursor, reference_table_name, child_table_name, child_table_key_name, reference_table_key_name):
print('Add_Foreign_Key')
try:
print('--- Adding a foreign key between the child table: '+child_table_name+ ' and the reference table: '+reference_table_name+' with foreign key as: '+child_table_key_name+' ---\n')
print('--- Adding a foreign key between the child table: '+child_table_name+ ' and the reference table: '+reference_table_name+' with foreign key as: '+child_table_key_name+' ---\n', file=logfile
)
child_table = "\""+database+"\".\""+schema+"\".\""+child_table_name+"\""
reference_table_name = "\""+database+"\".\""+schema+"\".\""+reference_table_name+"\""
print(child_table)
query = """ALTER TABLE {0} ADD FOREIGN KEY ({1}) REFERENCES {2}({3})"""\
.format(child_table.replace('\'', '\'\''), child_table_key_name, reference_table_name.replace('\'', '\'\''), reference_table_key_name)
print("Foreign Key Query: "+query+"\n")
print("Foreign Key Query: "+query+"\n", file=logfile)
if(cursor.execute(query)):
print("--- "+child_table+ " FOREIGN KEY added successfully"+" ---\n")
print("--- "+child_table+ " FOREIGN KEY added successfully"+" ---\n", file=logfile)
# conn.commit()
return True
else:
return False
except (RuntimeError, TypeError, NameError, snowCtx.connection.errors.Error) as e:
print("******* Error when adding foreign key: "+ str(e)+" *******\n")
print("******* Error when adding foreign key: "+ str(e)+" *******\n", file=logfile)
return False
回滚() 不起作用。我在控制台收到如下 SQL 错误,但没有进行回滚。
【问题讨论】:
标签: python snowflake-cloud-data-platform rollback