Fix circular dependency in database configuration

- Move db_type handling to environment variables to avoid circular dependency
- get_db_config() now uses environment variables for database type
- get_config() handles db_type specially to prevent recursion
- Fixes ImportError when running vidai.py
parent 3dfa46c4
Pipeline #192 canceled with stages
......@@ -41,16 +41,32 @@ except ImportError:
# Database configuration
def get_db_config() -> Dict[str, Any]:
"""Get database configuration."""
# Use environment variables or defaults to avoid circular dependency
# The database type should be set via environment or config file initially
db_type = os.environ.get('VIDAI_DB_TYPE', 'sqlite')
if db_type == 'mysql':
from .config import get_config
return {
'type': get_config('db_type', 'sqlite'),
'sqlite_path': os.path.join(get_user_config_dir(), get_config('db_sqlite_path', 'vidai.db')),
'mysql_host': get_config('db_mysql_host', 'localhost'),
'mysql_port': int(get_config('db_mysql_port', '3306')),
'mysql_user': get_config('db_mysql_user', 'vidai'),
'mysql_password': get_config('db_mysql_password', ''),
'mysql_database': get_config('db_mysql_database', 'vidai'),
'mysql_charset': get_config('db_mysql_charset', 'utf8mb4')
'type': 'mysql',
'sqlite_path': os.path.join(get_user_config_dir(), 'vidai.db'),
'mysql_host': os.environ.get('VIDAI_DB_MYSQL_HOST', 'localhost'),
'mysql_port': int(os.environ.get('VIDAI_DB_MYSQL_PORT', '3306')),
'mysql_user': os.environ.get('VIDAI_DB_MYSQL_USER', 'vidai'),
'mysql_password': os.environ.get('VIDAI_DB_MYSQL_PASSWORD', ''),
'mysql_database': os.environ.get('VIDAI_DB_MYSQL_DATABASE', 'vidai'),
'mysql_charset': os.environ.get('VIDAI_DB_MYSQL_CHARSET', 'utf8mb4')
}
else:
return {
'type': 'sqlite',
'sqlite_path': os.path.join(get_user_config_dir(), os.environ.get('VIDAI_DB_SQLITE_PATH', 'vidai.db')),
'mysql_host': 'localhost',
'mysql_port': 3306,
'mysql_user': 'vidai',
'mysql_password': '',
'mysql_database': 'vidai',
'mysql_charset': 'utf8mb4'
}
......@@ -279,6 +295,10 @@ def init_db(conn) -> None:
def get_config(key: str, default: str = '') -> str:
"""Get configuration value."""
# Handle database type separately to avoid circular dependency
if key == 'db_type':
return os.environ.get('VIDAI_DB_TYPE', default)
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('SELECT value FROM config WHERE key = ?', (key,))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment