Untitled

 avatar
unknown
plain_text
2 years ago
1.2 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:

        # Create a DataFrame with the data to insert
        data_to_insert = pd.DataFrame([source_row])
        data_to_insert.to_sql(name=table_name, con=connection_ext, schema=schema_ext, if_exists='append', index=False)

    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({column_name: source_val})

        # Create a DataFrame with the data to update
        if columns_to_update:
            data_to_update = pd.DataFrame(columns_to_update)
            data_to_update[pk] = pk_value  # Add the primary key column
            data_to_update.to_sql(name=table_name, con=connection_ext, schema=schema_ext, if_exists='replace', index=False)

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