soem fixes
This commit is contained in:
@@ -264,10 +264,20 @@ class DataMigrationService:
|
||||
str(k): v for k, v in (serie.episodeDict or {}).items()
|
||||
}
|
||||
|
||||
# Use folder as fallback name if name is empty
|
||||
series_name = serie.name
|
||||
if not series_name or not series_name.strip():
|
||||
series_name = serie.folder
|
||||
logger.debug(
|
||||
"Using folder '%s' as name for series '%s'",
|
||||
series_name,
|
||||
serie.key
|
||||
)
|
||||
|
||||
await AnimeSeriesService.create(
|
||||
db,
|
||||
key=serie.key,
|
||||
name=serie.name,
|
||||
name=series_name,
|
||||
site=serie.site,
|
||||
folder=serie.folder,
|
||||
episode_dict=episode_dict_for_db,
|
||||
|
||||
@@ -22,6 +22,7 @@ from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from src.server.database.connection import get_db_session
|
||||
from src.server.services.auth_service import auth_service
|
||||
from src.server.services.config_service import ConfigService
|
||||
from src.server.services.data_migration_service import (
|
||||
MigrationResult,
|
||||
@@ -116,6 +117,37 @@ def _get_anime_directory_from_config() -> Optional[str]:
|
||||
return None
|
||||
|
||||
|
||||
def _is_setup_complete() -> bool:
|
||||
"""Check if the application setup is complete.
|
||||
|
||||
Setup is complete when:
|
||||
1. Master password is configured
|
||||
2. Configuration file exists and is valid
|
||||
|
||||
Returns:
|
||||
True if setup is complete, False otherwise
|
||||
"""
|
||||
# Check if master password is configured
|
||||
if not auth_service.is_configured():
|
||||
return False
|
||||
|
||||
# Check if config exists and is valid
|
||||
try:
|
||||
config_service = ConfigService()
|
||||
config = config_service.load_config()
|
||||
|
||||
# Validate the loaded config
|
||||
validation = config.validate()
|
||||
if not validation.valid:
|
||||
return False
|
||||
|
||||
except Exception:
|
||||
# If we can't load or validate config, setup is not complete
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def ensure_migration_on_startup() -> Optional[MigrationResult]:
|
||||
"""Ensure data file migration runs during application startup.
|
||||
|
||||
@@ -123,6 +155,9 @@ async def ensure_migration_on_startup() -> Optional[MigrationResult]:
|
||||
It loads the anime directory from configuration and runs the
|
||||
migration if the directory is configured and contains data files.
|
||||
|
||||
Migration will only run if setup is complete (master password
|
||||
configured and valid configuration exists).
|
||||
|
||||
Returns:
|
||||
MigrationResult if migration was run, None if skipped
|
||||
(e.g., when no anime directory is configured)
|
||||
@@ -157,6 +192,13 @@ async def ensure_migration_on_startup() -> Optional[MigrationResult]:
|
||||
yield
|
||||
await close_db()
|
||||
"""
|
||||
# Check if setup is complete before running migration
|
||||
if not _is_setup_complete():
|
||||
logger.debug(
|
||||
"Setup not complete, skipping startup migration"
|
||||
)
|
||||
return None
|
||||
|
||||
# Get anime directory from config
|
||||
anime_directory = _get_anime_directory_from_config()
|
||||
|
||||
@@ -203,3 +245,65 @@ async def ensure_migration_on_startup() -> Optional[MigrationResult]:
|
||||
failed=1,
|
||||
errors=[f"Migration failed: {str(e)}"]
|
||||
)
|
||||
|
||||
|
||||
async def run_migration_for_directory(
|
||||
anime_directory: str
|
||||
) -> Optional[MigrationResult]:
|
||||
"""Run data file migration for a specific directory.
|
||||
|
||||
This function can be called after setup is complete to migrate
|
||||
data files from the specified anime directory to the database.
|
||||
Unlike ensure_migration_on_startup, this does not check setup
|
||||
status as it's intended to be called after setup is complete.
|
||||
|
||||
Args:
|
||||
anime_directory: Path to the anime directory containing
|
||||
series folders with data files
|
||||
|
||||
Returns:
|
||||
MigrationResult if migration was run, None if directory invalid
|
||||
"""
|
||||
if not anime_directory or not anime_directory.strip():
|
||||
logger.debug("Empty anime directory provided, skipping migration")
|
||||
return None
|
||||
|
||||
anime_directory = anime_directory.strip()
|
||||
|
||||
# Validate directory exists
|
||||
anime_path = Path(anime_directory)
|
||||
if not anime_path.exists():
|
||||
logger.warning(
|
||||
"Anime directory does not exist: %s, skipping migration",
|
||||
anime_directory
|
||||
)
|
||||
return None
|
||||
|
||||
if not anime_path.is_dir():
|
||||
logger.warning(
|
||||
"Anime directory path is not a directory: %s",
|
||||
anime_directory
|
||||
)
|
||||
return None
|
||||
|
||||
logger.info(
|
||||
"Running migration for directory: %s",
|
||||
anime_directory
|
||||
)
|
||||
|
||||
try:
|
||||
result = await run_startup_migration(anime_directory)
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Data file migration failed for %s: %s",
|
||||
anime_directory,
|
||||
e,
|
||||
exc_info=True
|
||||
)
|
||||
return MigrationResult(
|
||||
total_found=0,
|
||||
failed=1,
|
||||
errors=[f"Migration failed: {str(e)}"]
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user