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,17 +41,33 @@ except ImportError: ...@@ -41,17 +41,33 @@ except ImportError:
# Database configuration # Database configuration
def get_db_config() -> Dict[str, Any]: def get_db_config() -> Dict[str, Any]:
"""Get database configuration.""" """Get database configuration."""
from .config import get_config # Use environment variables or defaults to avoid circular dependency
return { # The database type should be set via environment or config file initially
'type': get_config('db_type', 'sqlite'), db_type = os.environ.get('VIDAI_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'), if db_type == 'mysql':
'mysql_port': int(get_config('db_mysql_port', '3306')), from .config import get_config
'mysql_user': get_config('db_mysql_user', 'vidai'), return {
'mysql_password': get_config('db_mysql_password', ''), 'type': 'mysql',
'mysql_database': get_config('db_mysql_database', 'vidai'), 'sqlite_path': os.path.join(get_user_config_dir(), 'vidai.db'),
'mysql_charset': get_config('db_mysql_charset', 'utf8mb4') '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'
}
def get_db_connection(): def get_db_connection():
...@@ -279,6 +295,10 @@ def init_db(conn) -> None: ...@@ -279,6 +295,10 @@ def init_db(conn) -> None:
def get_config(key: str, default: str = '') -> str: def get_config(key: str, default: str = '') -> str:
"""Get configuration value.""" """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() conn = get_db_connection()
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute('SELECT value FROM config WHERE key = ?', (key,)) 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