90 lines
3.2 KiB
Python
90 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
# ==============================================================================
|
|
# Script de Diagnóstico de Conexión a SQL Server (videoman)
|
|
# Compatible con Windows 10/11, LXC (FreeTDS) y Docker
|
|
# ==============================================================================
|
|
import sys
|
|
import platform
|
|
from pathlib import Path
|
|
|
|
# Agregar directorio backend al path para importar configuración
|
|
BACKEND_DIR = Path(__file__).resolve().parent.parent / "backend"
|
|
if str(BACKEND_DIR) not in sys.path:
|
|
sys.path.insert(0, str(BACKEND_DIR))
|
|
|
|
try:
|
|
from app.core.config import settings
|
|
from app.__version__ import __version__
|
|
except ImportError as e:
|
|
print(f"[ERROR] No se pudo importar la configuracion de backend: {e}")
|
|
sys.exit(1)
|
|
|
|
|
|
def test_sql_connection():
|
|
print("=" * 70)
|
|
print(f" Diagnostico de Conexion SQL Server - OnEverControl v{__version__}")
|
|
print("=" * 70)
|
|
print(f"Sistema Operativo : {platform.system()} {platform.release()} ({platform.machine()})")
|
|
print(f"Python : {platform.python_version()}")
|
|
print(f"Servidor Destino : {settings.DB_SERVER}:{settings.DB_PORT}")
|
|
print(f"Base de Datos : {settings.DB_NAME}")
|
|
print(f"Usuario : {settings.DB_USER}")
|
|
print(f"Charset : {settings.DB_CHARSET}")
|
|
print("-" * 70)
|
|
|
|
try:
|
|
import pymssql
|
|
except ImportError:
|
|
print("[ERROR] pymssql no esta instalado en este entorno.")
|
|
print("Instalelo con: pip install pymssql")
|
|
return False
|
|
|
|
print(f"[INFO] Intentando conectar a '{settings.DB_SERVER}'...")
|
|
|
|
try:
|
|
conn = pymssql.connect(
|
|
server=settings.DB_SERVER,
|
|
port=settings.DB_PORT,
|
|
user=settings.DB_USER,
|
|
password=settings.DB_PASSWORD,
|
|
database=settings.DB_NAME,
|
|
charset=settings.DB_CHARSET,
|
|
login_timeout=settings.DB_TIMEOUT,
|
|
timeout=settings.DB_TIMEOUT,
|
|
)
|
|
cursor = conn.cursor()
|
|
cursor.execute("SELECT @@VERSION, DB_NAME(), GETDATE();")
|
|
row = cursor.fetchone()
|
|
|
|
print("[OK] CONEXION EXITOSA!")
|
|
print("-" * 70)
|
|
print(f"Base de Datos Activa: {row[1]}")
|
|
print(f"Fecha/Hora Servidor : {row[2]}")
|
|
print(f"Version SQL Server : {row[0].splitlines()[0]}")
|
|
print("=" * 70)
|
|
|
|
cursor.close()
|
|
conn.close()
|
|
return True
|
|
|
|
except pymssql.OperationalError as err:
|
|
print("[ERROR] Fallo operacional al conectar con SQL Server:")
|
|
print(f" -> {err}")
|
|
print("-" * 70)
|
|
print("POSIBLES CAUSAS Y SOLUCIONES:")
|
|
print(" 1. Verifique que el servicio SQL Server este en ejecucion.")
|
|
print(" 2. En SQL Server Configuration Manager, confirme que TCP/IP este HABILITADO.")
|
|
print(" 3. Verifique que el firewall de Windows permita el puerto 1433 TCP.")
|
|
print(" 4. Si usa nombre de instancia (ej: localhost\\SQLEXPRESS), verifique que SQL Browser este activo.")
|
|
print(" 5. Revise que las credenciales en .env sean correctas.")
|
|
print("=" * 70)
|
|
return False
|
|
except Exception as ex:
|
|
print(f"[ERROR] Error inesperado: {ex}")
|
|
return False
|
|
|
|
|
|
if __name__ == "__main__":
|
|
success = test_sql_connection()
|
|
sys.exit(0 if success else 1)
|