Untitled

 avatar
unknown
plain_text
2 years ago
1.7 kB
4
Indexable
# ... (previous code)

for index, source_row in source_df.iterrows():
    pk_value = source_row[pk]

    # Check if the primary key exists in the target DataFrame
    if pk_value not in target_df[pk].values:

        # ... (code for constructing the INSERT query)

        # Execute the INSERT query
        if insert_columns:  # Check if there are columns to insert
            try:
                print(insert_query)
                cursor_ext.execute(insert_query)
                # connection_ext.commit()
            except Exception as e:
                logging.error(
                    "Error - {} . Line No - {} ".format(str(e), str(sys.exc_info()[-1].tb_lineno)))
    else:
        # Check if there are columns to update
        columns_to_update = []

        for column_name, source_val in source_row.items():
            if column_name not in ('created_by', 'created_on', 'updated_on', 'start_date', 'end_date'):
                target_val = target_row[column_name]
                if source_val != target_val:
                    columns_to_update.append(f"{column_name} = '{source_val}'")

        # Generate an update query dynamically
        if columns_to_update:
            update_query = f"UPDATE {schema_ext}.{table_name} SET "
            update_query += ", ".join(columns_to_update)
            update_query += f" where {eprm_table_col_pk} = '{pk_value}'"

            try:
                print(update_query)
                cursor_ext.execute(update_query)
                # connection_ext.commit()
            except Exception as e:
                logging.error(
                    "Error - {} . Line No - {} ".format(str(e), str(sys.exc_info()[-1].tb_lineno)))

# ... (remaining code)
Editor is loading...