gcp-hockey-results/motm_app/db_config.py
2025-09-29 20:06:07 +08:00

116 lines
3.3 KiB
Python

# encoding=utf-8
"""
Database configuration module with SQLAlchemy support.
This module provides backward compatibility with the old PyMySQL-based functions
while using SQLAlchemy for database operations.
"""
import os
import warnings
from database import (
db_config,
sql_write,
sql_write_static,
sql_read,
sql_read_static,
get_db_session,
execute_sql,
fetch_all,
fetch_one,
init_database
)
# Legacy constants for backward compatibility
CLOUDSQL_CONNECTION_NAME = os.getenv('CLOUDSQL_CONNECTION_NAME', "hk-hockey:asia-east2:hk-hockey-sql")
LOCAL_DB_SERVER = os.getenv('LOCAL_DB_SERVER', "mariadb.db.svc.cluster.local")
CLOUDSQL_USER = os.getenv('CLOUDSQL_USER', "root")
CLOUDSQL_WRITE_USER = os.getenv('CLOUDSQL_WRITE_USER', "hockeyWrite")
CLOUDSQL_READ_USER = os.getenv('CLOUDSQL_READ_USER', "hockeyRead")
CLOUDSQL_PASSWORD = os.getenv('CLOUDSQL_PASSWORD', "P8P1YopMlwg8TxhE")
CLOUDSQL_WRITE_PASSWORD = os.getenv('CLOUDSQL_WRITE_PASSWORD', "1URYcxXXlQ6xOWgj")
CLOUDSQL_READ_PASSWORD = os.getenv('CLOUDSQL_READ_PASSWORD', "o4GWrbbkBKy3oR6u")
CLOUDSQL_DATABASE = os.getenv('CLOUDSQL_DATABASE', "20209_hockeyResults")
LOCAL_DATABASE = os.getenv('LOCAL_DATABASE', "hockeyResults2021")
CLOUDSQL_DATABASE_STATIC = os.getenv('CLOUDSQL_DATABASE_STATIC', "hockeyResults")
CLOUDSQL_CHARSET = os.getenv('CLOUDSQL_CHARSET', "utf8")
# Legacy functions for backward compatibility
def write_cloudsql():
"""
Legacy function - now uses SQLAlchemy.
Returns a session object for compatibility.
"""
warnings.warn(
"write_cloudsql() is deprecated. Use get_db_session() instead.",
DeprecationWarning,
stacklevel=2
)
return get_db_session()
def write_cloudsql_static():
"""
Legacy function - now uses SQLAlchemy.
Returns a session object for compatibility.
"""
warnings.warn(
"write_cloudsql_static() is deprecated. Use get_db_session() instead.",
DeprecationWarning,
stacklevel=2
)
return get_db_session()
def read_cloudsql():
"""
Legacy function - now uses SQLAlchemy.
Returns a session object for compatibility.
"""
warnings.warn(
"read_cloudsql() is deprecated. Use get_db_session() instead.",
DeprecationWarning,
stacklevel=2
)
return get_db_session()
def read_cloudsql_static():
"""
Legacy function - now uses SQLAlchemy.
Returns a session object for compatibility.
"""
warnings.warn(
"read_cloudsql_static() is deprecated. Use get_db_session() instead.",
DeprecationWarning,
stacklevel=2
)
return get_db_session()
# These functions now use SQLAlchemy but maintain the same interface
__all__ = [
'sql_write',
'sql_write_static',
'sql_read',
'sql_read_static',
'get_db_session',
'execute_sql',
'fetch_all',
'fetch_one',
'init_database',
'db_config',
# Legacy constants
'CLOUDSQL_CONNECTION_NAME',
'LOCAL_DB_SERVER',
'CLOUDSQL_USER',
'CLOUDSQL_WRITE_USER',
'CLOUDSQL_READ_USER',
'CLOUDSQL_PASSWORD',
'CLOUDSQL_WRITE_PASSWORD',
'CLOUDSQL_READ_PASSWORD',
'CLOUDSQL_DATABASE',
'LOCAL_DATABASE',
'CLOUDSQL_DATABASE_STATIC',
'CLOUDSQL_CHARSET',
# Legacy functions
'write_cloudsql',
'write_cloudsql_static',
'read_cloudsql',
'read_cloudsql_static'
]