merlemcdurrrmott commited on
Commit
b62867a
·
verified ·
1 Parent(s): aedfa52

Upload app/assets/database/queries/asset.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app/assets/database/queries/asset.py +152 -0
app/assets/database/queries/asset.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlalchemy as sa
2
+ from sqlalchemy import select
3
+ from sqlalchemy.dialects import sqlite
4
+ from sqlalchemy.orm import Session
5
+
6
+ from app.assets.database.models import Asset, AssetReference
7
+ from app.assets.database.queries.common import MAX_BIND_PARAMS, calculate_rows_per_statement, iter_chunks
8
+
9
+
10
+ def asset_exists_by_hash(
11
+ session: Session,
12
+ asset_hash: str,
13
+ ) -> bool:
14
+ """
15
+ Check if an asset with a given hash exists in database.
16
+ """
17
+ row = (
18
+ session.execute(
19
+ select(sa.literal(True))
20
+ .select_from(Asset)
21
+ .where(Asset.hash == asset_hash)
22
+ .limit(1)
23
+ )
24
+ ).first()
25
+ return row is not None
26
+
27
+
28
+ def get_asset_by_hash(
29
+ session: Session,
30
+ asset_hash: str,
31
+ ) -> Asset | None:
32
+ return (
33
+ (session.execute(select(Asset).where(Asset.hash == asset_hash).limit(1)))
34
+ .scalars()
35
+ .first()
36
+ )
37
+
38
+
39
+ def upsert_asset(
40
+ session: Session,
41
+ asset_hash: str,
42
+ size_bytes: int,
43
+ mime_type: str | None = None,
44
+ ) -> tuple[Asset, bool, bool]:
45
+ """Upsert an Asset by hash. Returns (asset, created, updated)."""
46
+ vals = {"hash": asset_hash, "size_bytes": int(size_bytes)}
47
+ if mime_type:
48
+ vals["mime_type"] = mime_type
49
+
50
+ ins = (
51
+ sqlite.insert(Asset)
52
+ .values(**vals)
53
+ .on_conflict_do_nothing(index_elements=[Asset.hash])
54
+ )
55
+ res = session.execute(ins)
56
+ created = int(res.rowcount or 0) > 0
57
+
58
+ asset = (
59
+ session.execute(select(Asset).where(Asset.hash == asset_hash).limit(1))
60
+ .scalars()
61
+ .first()
62
+ )
63
+ if not asset:
64
+ raise RuntimeError("Asset row not found after upsert.")
65
+
66
+ updated = False
67
+ if not created:
68
+ changed = False
69
+ if asset.size_bytes != int(size_bytes) and int(size_bytes) > 0:
70
+ asset.size_bytes = int(size_bytes)
71
+ changed = True
72
+ if mime_type and not asset.mime_type:
73
+ asset.mime_type = mime_type
74
+ changed = True
75
+ if changed:
76
+ updated = True
77
+
78
+ return asset, created, updated
79
+
80
+
81
+ def create_stub_asset(
82
+ session: Session,
83
+ size_bytes: int,
84
+ mime_type: str | None = None,
85
+ ) -> Asset:
86
+ """Create a new asset with no hash (stub for later enrichment)."""
87
+ asset = Asset(size_bytes=size_bytes, mime_type=mime_type, hash=None)
88
+ session.add(asset)
89
+ session.flush()
90
+ return asset
91
+
92
+
93
+ def bulk_insert_assets(
94
+ session: Session,
95
+ rows: list[dict],
96
+ ) -> None:
97
+ """Bulk insert Asset rows with ON CONFLICT DO NOTHING on hash."""
98
+ if not rows:
99
+ return
100
+ ins = sqlite.insert(Asset).on_conflict_do_nothing(index_elements=[Asset.hash])
101
+ for chunk in iter_chunks(rows, calculate_rows_per_statement(5)):
102
+ session.execute(ins, chunk)
103
+
104
+
105
+ def get_existing_asset_ids(
106
+ session: Session,
107
+ asset_ids: list[str],
108
+ ) -> set[str]:
109
+ """Return the subset of asset_ids that exist in the database."""
110
+ if not asset_ids:
111
+ return set()
112
+ found: set[str] = set()
113
+ for chunk in iter_chunks(asset_ids, MAX_BIND_PARAMS):
114
+ rows = session.execute(
115
+ select(Asset.id).where(Asset.id.in_(chunk))
116
+ ).fetchall()
117
+ found.update(row[0] for row in rows)
118
+ return found
119
+
120
+
121
+ def update_asset_hash_and_mime(
122
+ session: Session,
123
+ asset_id: str,
124
+ asset_hash: str | None = None,
125
+ mime_type: str | None = None,
126
+ ) -> bool:
127
+ """Update asset hash and/or mime_type. Returns True if asset was found."""
128
+ asset = session.get(Asset, asset_id)
129
+ if not asset:
130
+ return False
131
+ if asset_hash is not None:
132
+ asset.hash = asset_hash
133
+ if mime_type is not None and not asset.mime_type:
134
+ asset.mime_type = mime_type
135
+ return True
136
+
137
+
138
+ def reassign_asset_references(
139
+ session: Session,
140
+ from_asset_id: str,
141
+ to_asset_id: str,
142
+ reference_id: str,
143
+ ) -> None:
144
+ """Reassign a reference from one asset to another.
145
+
146
+ Used when merging a stub asset into an existing asset with the same hash.
147
+ """
148
+ ref = session.get(AssetReference, reference_id)
149
+ if ref and ref.asset_id == from_asset_id:
150
+ ref.asset_id = to_asset_id
151
+
152
+ session.flush()