|
1 | 1 |
|
| 2 | +import sqlalchemy as sa |
| 3 | + |
2 | 4 | from bookworm.database import PinnedDocument, RecentDocument |
3 | 5 | from bookworm.document.hash_utils import get_persistent_content_hash |
4 | 6 | from bookworm.logger import logger |
@@ -42,6 +44,16 @@ def _get_legacy_documents_by_content_hash(model, content_hash, uri): |
42 | 44 | return matching_documents |
43 | 45 |
|
44 | 46 |
|
| 47 | +def _has_legacy_documents(model, uri): |
| 48 | + documents = model.query.filter( |
| 49 | + sa.or_( |
| 50 | + model.content_hash_version.is_(None), |
| 51 | + model.content_hash_version != CURRENT_CONTENT_HASH_VERSION, |
| 52 | + ) |
| 53 | + ) |
| 54 | + return any(doc.uri.format == uri.format for doc in documents) |
| 55 | + |
| 56 | + |
45 | 57 | def _merge_matching_documents(uri_document, hash_matching_documents): |
46 | 58 | matching_documents = [] |
47 | 59 | matching_ids = set() |
@@ -90,19 +102,25 @@ def get_document_unique(model, document): |
90 | 102 | ) |
91 | 103 | if content_hash is _CONTENT_HASH_UNSET: |
92 | 104 | content_hash = document.get_content_hash() |
93 | | - legacy_content_hash = document.get_legacy_content_hash() |
| 105 | + should_check_legacy_documents = content_hash is None or _has_legacy_documents( |
| 106 | + model, uri |
| 107 | + ) |
| 108 | + legacy_content_hash = ( |
| 109 | + document.get_legacy_content_hash() if should_check_legacy_documents else None |
| 110 | + ) |
94 | 111 | stored_content_hash, stored_content_hash_version = get_persistent_content_hash( |
95 | 112 | content_hash, |
96 | 113 | legacy_content_hash, |
97 | 114 | ) |
98 | 115 | hash_matching_documents = _get_documents_by_content_hash(model, content_hash, uri) |
99 | | - hash_matching_documents.extend( |
100 | | - _get_legacy_documents_by_content_hash( |
101 | | - model, |
102 | | - legacy_content_hash, |
103 | | - uri, |
| 116 | + if should_check_legacy_documents: |
| 117 | + hash_matching_documents.extend( |
| 118 | + _get_legacy_documents_by_content_hash( |
| 119 | + model, |
| 120 | + legacy_content_hash, |
| 121 | + uri, |
| 122 | + ) |
104 | 123 | ) |
105 | | - ) |
106 | 124 | matching_documents = _merge_matching_documents(doc, hash_matching_documents) |
107 | 125 | doc = doc or (matching_documents[0] if matching_documents else None) |
108 | 126 | if doc is not None: |
|
0 commit comments