Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.0 kB
1
Indexable
Never
# ... (previous code)

if pk_value not in target_df[pk].values:
    # ... (previous code)
else:
    # Fetch the corresponding row from the target DataFrame based on the primary key
    target_row = target_df[target_df[pk] == pk_value].iloc[0]
    if not source_row.equals(target_row):
        columns_to_update = []
        for column_name, source_val in source_row.items():
            target_val = target_row[column_name]
            if source_val != target_val:
                if is_datetime(source_val):
                    # Format datetime values using the appropriate function for the database type
                    if db_type_ext == 'ORACLE':
                        update_value = f"TO_DATE('{source_val}', 'YYYY-MM-DD HH24:MI:SS')"
                    elif db_type_ext in ('MYSQL', 'MARIA'):
                        # For MariaDB and MySQL, use STR_TO_DATE
                        update_value = f"STR_TO_DATE('{source_val}', '%Y-%m-%d %H:%i:%s')"
                    else:
                        # Enclose other values in single quotes
                        update_value = f"'{source_val}'"
                elif source_val == 'NaT':
                    # Replace 'NaT' with NULL without single quotes
                    update_value = 'NULL'
                elif column_name == 'extended_rule_code':
                    parts = source_val.split('==')
                    if len(parts) == 2:
                        extended_rule_code = f"'{parts[0]}=='{parts[1]}'"
                        update_value = extended_rule_code
                else:
                    if column_name == 'created_by':
                        # Update 'created_by' only when it's different from source and not 'None'
                        if source_val != 'None':
                            update_value = f"'{source_val}'"
                        else:
                            update_value = f"'{target_val}'"
                    else:
                        # Handle non-datetime columns (e.g., strings, numbers) here
                        update_value = f"'{source_val}'"
                
                # Add the column name and formatted value to the update statement
                columns_to_update.append(f"{column_name} = {update_value}")

        # 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)
                update_sucess_count += 1
                # connection_ext.commit()
            except Exception as e:
                logging.error(
                    "Error - {} . Line No - {} ".format(str(e), str(sys.exc_info()[-1].tb_lineno)))
                update_failed_count += 1

# ... (rest of the code)