【问题标题】:Python and Snowflake rollback is not executed on specific condition within an iteration of a loopPython 和 Snowflake 回滚不在循环迭代内的特定条件下执行
【发布时间】: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


    【解决方案1】:

    Snowflake DDL 语句隐式地提交打开的事务,因此您可能会考虑以不同的方式处理此问题,可能会在进程之前克隆对象,然后如果在此过程中出现任何故障,您可以在异常子句中执行交换操作。

    相关文档链接:

    https://docs.snowflake.com/en/sql-reference/transactions.html#label-transactions-ddl

    https://docs.snowflake.com/en/sql-reference/sql/alter-table.html

    我希望这会有所帮助...丰富

    附言如果这个(或另一个)答案对您有帮助,请花点时间“接受”有帮助的答案,方法是单击答案旁边的复选标记,将其从“灰色”切换为“已填写”。

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多