Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.5 kB
1
Indexable
Never
import pandas as pd
import mysql.connector
import json
from base64 import b64decode as base_b64decode
from base64 import b64encode as base_b64encode
import logging
import datetime
import os
from sqlalchemy import create_engine as sqlalchemy_create_engine

releaseId = '275.2'
releaseType = 'Deployed'
replicationTarget = 'SOURCE'
catalogId = ''
opId = 'HOB'
buId = 'DEFAULT'
replicationJobId = 'REP_990_234'
json_file_path = "/app/scripts/PPM_Release_Management/Product_Catalog_ETL/config/ppm_reply.json"
sql_log_file = f"/app/scripts/PPM_Release_Management/Product_Catalog_ETL/logs/{replicationJobId}ppm_reply.sql"
log_file = f'/app/scripts/PPM_Release_Management/Product_Catalog_ETL/logs/{replicationJobId}ppm_reply.log'
#sql_log_file = os.get_path("PPM_PC_LOG") + "/" + replicationJobId + "_ppm_pc_replication_delete.sql"
#json_file_path = os.get_path("PPM_PC_CONFIG") + "/ppm_pc_replication.json"
#log_file = os.get_path("PPM_PC_LOG") + "/" + replicationJobId + "_ppm_pc_replication_delete.log"
logging.basicConfig(
    filename=log_file,
    level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
query_count = 0


def write_sql(query_info):
    with open(sql_log_file, "w") as log_file:
        log_file.write(query_info)
        log_file.write('\n')


try:
    def connect_to_database(encrypt, host, port, user, db_type, schema):
        if encrypt == 'Y':
            password = base_b64decode(json_data.get('PPM_PC', {}).get('DB_PASSWORD')).decode('utf-8')
        else:
            password = json_data.get('PPM_PC', {}).get('DB_PASSWORD')
        if db_type == 'MYSQL':
            cnx = mysql.connector.connect(user=user, password=password, host=host, port=port)
            cursor = cnx.cursor()
            logging.info(f"connected to database server PPM_PC: {host}:{port}")
            return cnx, cursor
        elif db_type == 'ORACLE':
            import oracledb
            oracle_mode = oracledb.is_thin_mode()
            print("Oracle mode: %s" % oracle_mode)
            if oracle_mode:
                oracledb.init_oracle_client()
                print("Enabled python-oracledb Thick mode")
            else:
                print("Default python-oracledb Thick mode")

            cnx = ('oracle://%s:%s@%s:%s/?service_name=%s' % (user, password, host, port, schema))
            connection = sqlalchemy_create_engine(cnx, encoding="utf8")
            connection = connection.raw_connection()
            cursor = connection.cursor()

        return cnx, cursor, schema


    with open(json_file_path) as json_file:
        json_data = json.load(json_file)
    encrypt = json_data.get('PPM_PC', {}).get('ENCRYPT')
    host = json_data.get('PPM_PC', {}).get('DB_HOST')
    port = json_data.get('PPM_PC', {}).get('DB_PORT')
    user = json_data.get('PPM_PC', {}).get('DB_USER')
    db_type = json_data.get('PPM_PC', {}).get('DB_TYPE')
    schema = json_data.get(replicationTarget, {}).get('DB_SCHEMA')
    logging.info("DATABASE - HOST FOR PPM_PC - " + host)
    logging.info("DATABASE - PORT FOR PPM_PC - " + port)
    logging.info("DATABASE - USER - PPM_PC" + user)
    connection, cursor = connect_to_database(encrypt, host, port, user, db_type, schema)
    # conn_ppm, cursor_ppm, schema_ppm = connect_to_database(json_data,'PPM_PC')
    # conn_tar, cursor_tar, schema_tar = connect_to_database(json_data,replicationTarget)
    cursor = connection.cursor()
    primary_query = f"SELECT * FROM {schema}.etl_ppm_replication_master"
    cursor.execute(primary_query)
    logging.info(f"executed primary query - {primary_query}")
    query_info = f"-- ++++++++++++++++++++|PRIMARY_QUERY\n"
    query_info += f"-- #Query:\n{primary_query};\n"
    write_sql(query_info)

    rows = cursor.fetchall()
    columns = [desc[0] for desc in cursor.description]
    df = pd.DataFrame(rows, columns=columns)

so in the above code im passing connection, cursor = connect_to_database(encrypt, host, port, user, db_type, schema) insetad of that i want to pass complete json_data in which i have like below
"PPM_PC": {
		"DB_PASSWORD": "tibtcare_ppm",
		"DB_TYPE": "MYSQL",
		"DB_PORT": "3308",
		"DB_SCHEMA": "tibtcare_ppm",
		"DB_USER": "tibtcare_ppm",
		"DB_HOST": "10.16.16.128",
		"ENCRYPT": "N"
	},
so all the arguments must be passed and want to return connection,cursor,schema from the function connect_to_database