Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat: migrate legacy tier database state

jinzhenj891d900bb024
diff --git a/mineru/doclib/core/db.py b/mineru/doclib/core/db.py
index a31a129f..3e6e52ec 100644
--- a/mineru/doclib/core/db.py
+++ b/mineru/doclib/core/db.py
@@ -14,7 +14,7 @@ from ...config import SQLiteConfig
from ...errors import ServerBusyError
from ..config_defaults import CONFIG_DEFAULTS
-SCHEMA_VERSION = 1
+SCHEMA_VERSION = 2
_MIGRATIONS_DIR = Path(__file__).resolve().parent.parent / "migrations"
_T = TypeVar("_T")
diff --git a/mineru/doclib/migrations/001_init.sql b/mineru/doclib/migrations/001_init.sql
index f7e5b055..d5a90526 100644
--- a/mineru/doclib/migrations/001_init.sql
+++ b/mineru/doclib/migrations/001_init.sql
@@ -1,5 +1,5 @@
-- v001: Initial schema
--- Applied to new databases. For existing databases, see 002_add_parse_columns.sql.
+-- Applied to new databases. Existing databases advance through later numbered migrations.
-- STABLE
CREATE TABLE IF NOT EXISTS files (
diff --git a/mineru/doclib/migrations/002_invalidate_legacy_tiers.sql b/mineru/doclib/migrations/002_invalidate_legacy_tiers.sql
new file mode 100644
index 00000000..3af7703a
--- /dev/null
+++ b/mineru/doclib/migrations/002_invalidate_legacy_tiers.sql
@@ -0,0 +1,37 @@
+-- v002: Make databases containing retired Tier values safe for the renamed Tier system.
+-- Parsed artifacts are intentionally not migrated, so rows referencing legacy cache directories are removed.
+
+DELETE FROM parses
+WHERE tier NOT IN ('flash', 'basic', 'standard', 'advanced');
+
+DELETE FROM fts_contents
+WHERE tier IS NOT NULL
+ AND tier NOT IN ('flash', 'basic', 'standard', 'advanced');
+
+UPDATE docs
+SET meta_tier = NULL
+WHERE meta_tier IS NOT NULL
+ AND meta_tier NOT IN ('flash', 'basic', 'standard', 'advanced');
+
+UPDATE parsing_rules
+SET tier = CASE tier
+ WHEN 'medium' THEN 'basic'
+ WHEN 'high' THEN 'standard'
+ WHEN 'xhigh' THEN 'advanced'
+ ELSE NULL
+END
+WHERE tier IS NOT NULL
+ AND tier NOT IN ('flash', 'basic', 'standard', 'advanced');
+
+UPDATE config
+SET value = CASE value
+ WHEN 'medium' THEN 'basic'
+ WHEN 'high' THEN 'standard'
+ WHEN 'xhigh' THEN 'standard'
+END
+WHERE key = 'parse_server.local.managed_tier'
+ AND value IN ('medium', 'high', 'xhigh');
+
+DELETE FROM config
+WHERE key = 'parse_server.local.managed_tier'
+ AND value NOT IN ('basic', 'standard');
diff --git a/tests/unittest/test_doclib_migrations.py b/tests/unittest/test_doclib_migrations.py
new file mode 100644
index 00000000..0fdd5032
--- /dev/null
+++ b/tests/unittest/test_doclib_migrations.py
@@ -0,0 +1,160 @@
+from __future__ import annotations
+
+import asyncio
+from pathlib import Path
+
+import pytest
+
+from mineru.doclib.core import db as db_module
+from mineru.doclib.core.db import DatabaseManager
+from mineru.doclib.server import _doc_info, _parse_info, _parsing_rule_info
+from mineru.doclib.services.config_svc import ConfigService
+
+
+def test_fresh_database_records_all_schema_migrations(tmp_path: Path) -> None:
+ async def _run() -> None:
+ db = DatabaseManager(str(tmp_path / "doclib.db"))
+
+ await db.initialize()
+
+ rows = await db.fetchall("SELECT version FROM _migrations ORDER BY version")
+ assert rows == [{"version": 1}, {"version": 2}]
+
+ asyncio.run(_run())
+
+
+def test_v2_migration_invalidates_legacy_cached_data_and_maps_user_settings(
+ monkeypatch: pytest.MonkeyPatch,
+ tmp_path: Path,
+) -> None:
+ async def _run() -> None:
+ db = DatabaseManager(str(tmp_path / "doclib.db"))
+ monkeypatch.setattr(db_module, "SCHEMA_VERSION", 1)
+ await db.initialize()
+
+ now = 1
+ legacy_sha = "a" * 64
+ current_sha = "b" * 64
+ await db.execute(
+ "INSERT INTO docs (sha256, short_id, size_bytes, meta_tier, first_seen_at, updated_at) "
+ "VALUES (?, ?, ?, ?, ?, ?)",
+ (legacy_sha, "aaaaaaa", 1, "xhigh", now, now),
+ )
+ await db.execute(
+ "INSERT INTO docs (sha256, short_id, size_bytes, meta_tier, first_seen_at, updated_at) "
+ "VALUES (?, ?, ?, ?, ?, ?)",
+ (current_sha, "bbbbbbb", 1, "standard", now, now),
+ )
+ for tier, status in (("medium", "done"), ("high", "pending"), ("xhigh", "done"), ("unknown", "done")):
+ await db.execute(
+ "INSERT INTO parses (sha256, tier, page_range, status, privacy, created_at, updated_at) "
+ "VALUES (?, ?, '1', ?, 'local', ?, ?)",
+ (legacy_sha, tier, status, now, now),
+ )
+ await db.execute(
+ "INSERT INTO parses (sha256, tier, page_range, status, privacy, created_at, updated_at) "
+ "VALUES (?, 'standard', '1', 'done', 'local', ?, ?)",
+ (current_sha, now, now),
+ )
+ for tier, sha256 in (("medium", legacy_sha), ("unknown", legacy_sha), ("standard", current_sha)):
+ await db.execute(
+ "INSERT INTO fts_contents (sha256, tier, text, title, author) VALUES (?, ?, 'text', '', '')",
+ (sha256, tier),
+ )
+ for index, tier in enumerate(("medium", "high", "xhigh", "unknown", "standard"), start=1):
+ await db.execute(
+ "INSERT INTO parsing_rules (id, pattern, tier, created_at, updated_at) VALUES (?, ?, ?, ?, ?)",
+ (index, f"*.{index}.pdf", tier, now, now),
+ )
+ await db.execute(
+ "INSERT INTO config (key, value) VALUES ('parse_server.local.managed_tier', 'xhigh')"
+ )
+
+ monkeypatch.setattr(db_module, "SCHEMA_VERSION", 2)
+ await db.initialize()
+
+ assert await db.fetchall("SELECT tier FROM parses ORDER BY id") == [{"tier": "standard"}]
+ assert await db.fetchall("SELECT tier FROM fts_contents ORDER BY rowid") == [{"tier": "standard"}]
+ assert await db.fetchall("SELECT short_id, meta_tier FROM docs ORDER BY short_id") == [
+ {"short_id": "aaaaaaa", "meta_tier": None},
+ {"short_id": "bbbbbbb", "meta_tier": "standard"},
+ ]
+ assert await db.fetchall("SELECT id, tier FROM parsing_rules ORDER BY id") == [
+ {"id": 1, "tier": "basic"},
+ {"id": 2, "tier": "standard"},
+ {"id": 3, "tier": "advanced"},
+ {"id": 4, "tier": None},
+ {"id": 5, "tier": "standard"},
+ ]
+ assert await db.fetchone("SELECT value FROM config WHERE key='parse_server.local.managed_tier'") is None
+ assert await ConfigService(db).get("parse_server.local.managed_tier") == "standard"
+ assert await db.fetchall("SELECT version FROM _migrations ORDER BY version") == [
+ {"version": 1},
+ {"version": 2},
+ ]
+
+ doc_rows = await db.fetchall("SELECT * FROM docs ORDER BY short_id")
+ parse_rows = await db.fetchall(
+ "SELECT p.*, d.short_id FROM parses p JOIN docs d ON d.sha256=p.sha256 ORDER BY p.id"
+ )
+ rule_rows = await db.fetchall("SELECT * FROM parsing_rules ORDER BY id")
+ assert [_doc_info(row).meta_tier for row in doc_rows] == [None, "standard"]
+ assert [_parse_info(row).tier for row in parse_rows] == ["standard"]
+ assert [_parsing_rule_info(row).tier for row in rule_rows] == [
+ "basic",
+ "standard",
+ "advanced",
+ None,
+ "standard",
+ ]
+
+ snapshot = {
+ "parses": await db.fetchall("SELECT * FROM parses ORDER BY id"),
+ "fts": await db.fetchall("SELECT * FROM fts_contents ORDER BY rowid"),
+ "docs": await db.fetchall("SELECT * FROM docs ORDER BY short_id"),
+ "rules": await db.fetchall("SELECT * FROM parsing_rules ORDER BY id"),
+ "config": await db.fetchall("SELECT * FROM config ORDER BY key"),
+ }
+ await db.initialize()
+ assert await db.fetchall("SELECT * FROM parses ORDER BY id") == snapshot["parses"]
+ assert await db.fetchall("SELECT * FROM fts_contents ORDER BY rowid") == snapshot["fts"]
+ assert await db.fetchall("SELECT * FROM docs ORDER BY short_id") == snapshot["docs"]
+ assert await db.fetchall("SELECT * FROM parsing_rules ORDER BY id") == snapshot["rules"]
+ assert await db.fetchall("SELECT * FROM config ORDER BY key") == snapshot["config"]
+
+ asyncio.run(_run())
+
+
+ ("legacy_tier", "expected_override", "expected_tier"),
+ (
+ ("medium", "basic", "basic"),
+ ("high", None, "standard"),
+ ("xhigh", None, "standard"),
+ ("unknown", None, "standard"),
+ ),
+)
+def test_v2_migration_normalizes_managed_server_tier(
+ legacy_tier: str,
+ expected_override: str | None,
+ expected_tier: str,
+ monkeypatch: pytest.MonkeyPatch,
+ tmp_path: Path,
+) -> None:
+ async def _run() -> None:
+ db = DatabaseManager(str(tmp_path / "doclib.db"))
+ monkeypatch.setattr(db_module, "SCHEMA_VERSION", 1)
+ await db.initialize()
+ await db.execute(
+ "INSERT INTO config (key, value) VALUES ('parse_server.local.managed_tier', ?)",
+ (legacy_tier,),
+ )
+
+ monkeypatch.setattr(db_module, "SCHEMA_VERSION", 2)
+ await db.initialize()
+
+ row = await db.fetchone("SELECT value FROM config WHERE key='parse_server.local.managed_tier'")
+ assert (row["value"] if row else None) == expected_override
+ assert await ConfigService(db).get("parse_server.local.managed_tier") == expected_tier
+
+ asyncio.run(_run())