Skip to content

Bulk asynchronous downloader for multiple file hosts

perf: only apply db updates if schema is outdated (#1927)

NTFSvolumed5a1464d285a
diff --git a/cyberdrop_dl/database/__init__.py b/cyberdrop_dl/database/__init__.py
index 327401b96..7edb1e89c 100644
--- a/cyberdrop_dl/database/__init__.py
+++ b/cyberdrop_dl/database/__init__.py
@@ -36,9 +36,13 @@ class Database:
async def __aenter__(self) -> Self:
self._db_conn = await connect(self._db_path)
await self._pre_allocate()
+ await self.schema.create()
await self.history.create()
await self.hash.create()
- await self.schema.create()
+ if not self.schema.up_to_date:
+ await self.history.apply_updates()
+ await self.schema.update()
+
return self
async def fetchone(self, query: str, parameters: Iterable[Any] | None = None) -> aiosqlite.Row | None:
diff --git a/cyberdrop_dl/database/tables/history.py b/cyberdrop_dl/database/tables/history.py
index 3e12fe113..4469b2be1 100644
--- a/cyberdrop_dl/database/tables/history.py
+++ b/cyberdrop_dl/database/tables/history.py
@@ -38,12 +38,11 @@ class HistoryTable:
async def create(self) -> None:
await self.db_conn.execute(create_history)
await self.db_conn.commit()
+
+ async def apply_updates(self) -> None:
await self.fix_primary_keys()
await self.add_columns_media()
- await fix_domains(self.db_conn)
- await fix_referers(self.db_conn)
- await self.db_conn.executescript(create_media_index)
- await self.db_conn.commit()
+ await apply_fixes(self.db_conn)
async def delete_invalid_rows(self) -> None:
query = "DELETE FROM media WHERE download_filename = '' "
@@ -296,7 +295,14 @@ class HistoryTable:
await self.db_conn.commit()
-async def fix_domains(db_conn: aiosqlite.Connection) -> None:
+async def apply_fixes(db_conn: aiosqlite.Connection) -> None:
+ await _fix_domains(db_conn)
+ await _fix_referers(db_conn)
+ await db_conn.executescript(create_media_index)
+ await db_conn.commit()
+
+
+async def _fix_domains(db_conn: aiosqlite.Connection) -> None:
with _timed_update("old database domains"):
updates = "\n".join(
f"UPDATE OR REPLACE media SET domain = '{current}' WHERE domain = '{old}';" # noqa: S608
@@ -311,7 +317,7 @@ async def fix_domains(db_conn: aiosqlite.Connection) -> None:
await db_conn.commit()
-async def fix_referers(db_conn: aiosqlite.Connection) -> None:
+async def _fix_referers(db_conn: aiosqlite.Connection) -> None:
from cyberdrop_dl.crawlers import bunkr, cyberdrop, jpg5, redgifs, turbovid
def try_wrap(fn: Callable[..., _T]) -> Callable[..., _T]:
diff --git a/cyberdrop_dl/database/tables/schema.py b/cyberdrop_dl/database/tables/schema.py
index 8041f76ed..9eba722b1 100644
--- a/cyberdrop_dl/database/tables/schema.py
+++ b/cyberdrop_dl/database/tables/schema.py
@@ -32,9 +32,14 @@ CURRENT_APP_SCHEMA_VERSION = Version(9, 13, 0)
logger = logging.getLogger(__name__)
[email protected](slots=True, frozen=True)
[email protected](slots=True)
class SchemaVersionTable:
_database: Database
+ _up_to_date: bool = False
+
+ @property
+ def up_to_date(self) -> bool:
+ return self._up_to_date
@property
def db_conn(self) -> aiosqlite.Connection:
@@ -69,9 +74,11 @@ class SchemaVersionTable:
logger.info(f"Expected database schema version: {CURRENT_APP_SCHEMA_VERSION!s}")
version = await self.get_version()
logger.info(f"Database reports installed version: {version!s}")
+ # TODO: on v10, raise SystemExit if db version is None or older than 8.0.0
if version is not None and version >= CURRENT_APP_SCHEMA_VERSION:
- return
+ self._up_to_date = True
- # TODO: on v9, raise SystemExit if db version is None or older than 8.0.0
+ async def update(self) -> None:
logger.info(f"Updating database version to {CURRENT_APP_SCHEMA_VERSION!s}")
await self.__update_schema_version()
+ self._up_to_date = True
diff --git a/cyberdrop_dl/database/transfer.py b/cyberdrop_dl/database/transfer.py
index 0092e3b7f..51b4e25b2 100644
--- a/cyberdrop_dl/database/transfer.py
+++ b/cyberdrop_dl/database/transfer.py
@@ -8,7 +8,7 @@ from pathlib import Path
from typing import LiteralString
from cyberdrop_dl.database import Database, connect
-from cyberdrop_dl.database.tables.history import fix_domains, fix_referers
+from cyberdrop_dl.database.tables.history import apply_fixes
from cyberdrop_dl.database.tables.schema import CURRENT_APP_SCHEMA_VERSION, Version
from cyberdrop_dl.logs import setup_console_logging
from cyberdrop_dl.utils import dates
@@ -197,8 +197,7 @@ def _apply_fixes(db_path: Path) -> None:
async def fix() -> None:
conn = await connect(db_path)
try:
- await fix_domains(conn)
- await fix_referers(conn)
+ await apply_fixes(conn)
finally:
await conn.close()
diff --git a/pyproject.toml b/pyproject.toml
index 3b1b2b334..f3526fc1d 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -54,7 +54,7 @@ license-files = ["LICENSE"]
maintainers = [{name = "NTFSvolume", email = "[email protected]"}]
readme = "README.md"
requires-python = ">=3.11"
-version = "9.12.0"
+version = "9.13.0.dev0"
[project.optional-dependencies]
apprise = [
diff --git a/uv.lock b/uv.lock
index 78465598f..a4df2b4a2 100644
--- a/uv.lock
+++ b/uv.lock
@@ -691,7 +691,7 @@ wheels = [
[[package]]
name = "cyberdrop-dl-patched"
-version = "9.12.0"
+version = "9.13.0.dev0"
source = { editable = "." }
dependencies = [
{ name = "aiodns", marker = "sys_platform != 'android' and sys_platform != 'win32'" },