Untitled

mail@pastecode.io avatarunknown
plain_text
2 months ago
1.6 kB
1
Indexable
Never
# Loop through rows and insert data
for _, row in df.iterrows():
    eprm_table_name = row['eprm_table_name'].lower()  # Convert table name to lowercase
    if eprm_table_name != 'pkg_prd_fed_ext_attrs':
        source_query = f"SELECT * FROM {schema_source}.{eprm_table_name} WHERE release_id='{releaseId}' AND op_id='{opId}' AND bu_id='{buId}'"
        try:
            source_df = pd_read_sql(source_query, con=conn_source)
            if not source_df.empty:
                source_df.to_sql(eprm_table_name, con=target_engine, if_exists='append', index=False)
                # After inserting, update the 'created_by' column value
                update_query = f"UPDATE {schema_target}.{eprm_table_name} SET created_by='REP_990_234' WHERE release_id='{releaseId}' AND op_id='{opId}' AND bu_id='{buId}'"
                with target_engine.begin() as connection:
                    connection.execute(update_query)
                write_sql(f"-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ STATUS|{replicationTarget}| TABLE| {eprm_table_name}\n")
                write_sql(f"-- #Query: Inserted {len(source_df)} record(s) and updated 'created_by'\n")
                print(f"Inserting records into {eprm_table_name}")
                logging.info(f"Inserted {len(source_df)} record(s) into {eprm_table_name} and updated 'created_by'")
        except mysql.connector.Error as err:
            print(f"Error occurred while executing the query: {source_query}: {err}")
            logging.info(f"Error occurred while executing the query: {source_query}: {err}")