Uecko_ERP_FactuGES_sync/app/db/db_connection.py

70 lines
2.3 KiB
Python
Raw Normal View History

2025-11-30 21:31:09 +00:00
from typing import Any, Mapping
2025-08-28 08:51:05 +00:00
import fdb
import mysql.connector
2025-11-30 21:31:09 +00:00
2025-11-30 18:28:44 +00:00
from app.config import logger
2025-08-28 08:51:05 +00:00
2025-11-30 21:31:09 +00:00
def get_factuges_connection(config) -> fdb.Connection:
"""
Crea y devuelve una conexión a FactuGES (Firebird).
"""
2025-08-28 08:51:05 +00:00
try:
conn = fdb.connect(
host=config['FACTUGES_HOST'],
port=int(config['FACTUGES_PORT']),
database=config['FACTUGES_DATABASE'],
user=config['FACTUGES_USER'],
password=config['FACTUGES_PASSWORD'],
2025-11-30 21:31:09 +00:00
charset='UTF8',
2025-08-28 08:51:05 +00:00
)
2025-11-30 18:28:44 +00:00
logger.info(
2025-11-30 21:31:09 +00:00
"Conexión a la base de datos FactuGES establecida: %s with database:%s - using user:%s",
config['FACTUGES_HOST'],
config['FACTUGES_DATABASE'],
config['FACTUGES_USER'],
)
2025-08-28 08:51:05 +00:00
return conn
except Exception as e:
2025-11-30 18:28:44 +00:00
logger.error("Error al conectar a la base de datos FactuGES.")
logger.error(
2025-11-30 21:31:09 +00:00
"(ERROR) Failed to establish connection to: %s with database:%s - using user:%s",
config['FACTUGES_HOST'],
config['FACTUGES_DATABASE'],
config['FACTUGES_USER'],
)
2025-11-30 18:28:44 +00:00
logger.error(str(e))
2025-08-28 08:51:05 +00:00
raise e
2025-11-30 21:31:09 +00:00
def get_mysql_connection(config: Mapping[str, Any]):
"""
Crea y devuelve una conexión a MySQL.
"""
2025-08-28 08:51:05 +00:00
try:
conn = mysql.connector.connect(
2025-11-30 21:31:09 +00:00
host=config["FWEB_MYSQL_HOST"],
port=config["FWEB_MYSQL_PORT"],
database=config["FWEB_MYSQL_DATABASE"],
user=config["FWEB_MYSQL_USER"],
password=config["FWEB_MYSQL_PASSWORD"],
2025-08-28 08:51:05 +00:00
)
2025-11-30 18:28:44 +00:00
logger.info(
2025-11-30 21:31:09 +00:00
"Conexión a la base de datos MySQL establecida a: %s with database:%s - using user:%s",
config['FWEB_MYSQL_HOST'],
config['FWEB_MYSQL_DATABASE'],
config['FWEB_MYSQL_USER'],
)
2025-08-28 08:51:05 +00:00
return conn
except Exception as e:
2025-11-30 18:28:44 +00:00
logger.error("Error al conectar a la base de datos MySQL.")
logger.error(
2025-11-30 21:31:09 +00:00
"(ERROR) Failed to establish connection to: %s with database:%s - using user:%s",
config['FWEB_MYSQL_HOST'],
config['FWEB_MYSQL_DATABASE'],
config['FWEB_MYSQL_USER'],
)
2025-11-30 18:28:44 +00:00
logger.error(str(e))
2025-08-28 08:51:05 +00:00
raise e