Untitled

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

for index, row in primary_df.iterrows():
    table_name = row['eprm_table_name'].lower()
    eprm_table_col_pk = row['eprm_table_col_pk']

    # Fetch primary key column name and table name
    pk = eprm_table_col_pk.lower()

    try:
        source_query = f"SELECT * FROM {schema_source}.{table_name}"  # Where op_id , bu_id
        source_df = pd.read_sql(source_query, connection_source)
        target_query = f"SELECT * FROM {schema_ext}.{table_name}"  # Where op_id , bu_id
        target_df = pd.read_sql(target_query, connection_ext)
    except Exception as e:
        continue

    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:
            continue  # Skip if the primary key is not present in the target DataFrame

        # 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):
            # Generate an update query dynamically
            update_query = f"UPDATE {schema_ext}.{table_name} SET "
            column_updates = []

            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:
                        column_updates.append(f"{column_name} = '{source_val}'")

            update_query += ", ".join(column_updates)
            update_query += f" where {eprm_table_col_pk} = '{pk_value}'"
            #print(update_query)
            try:
                print(update_query)
                #cursor_ext.execute(update_query)
            except Exception as e:
                continue

# ... (rest of the code)