Skip to content

Commit 7b371f6

Browse files
committed
db: still use the old check_db function
1 parent a57a126 commit 7b371f6

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/ota_proxy/db.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import logging
1818
import sqlite3
1919
from contextlib import closing
20+
from pathlib import Path
2021
from typing import Optional
2122

2223
from multidict import CIMultiDict
@@ -30,7 +31,6 @@
3031
gen_sql_stmt,
3132
utils,
3233
)
33-
from simple_sqlite3_orm.utils import check_db_integrity
3434
from typing_extensions import Annotated
3535

3636
from otaclient_common._typing import StrOrPath
@@ -184,8 +184,23 @@ def init_db(db_f: StrOrPath, table_name: str) -> None:
184184

185185

186186
def check_db(db_f: StrOrPath, table_name: str) -> bool:
187-
with closing(sqlite3.connect(db_f)) as con:
188-
try:
189-
return check_db_integrity(con, table_name)
190-
except Exception:
187+
"""Check whether specific db is normal or not."""
188+
if not Path(db_f).is_file():
189+
logger.warning(f"{db_f} not found, will init db")
190+
return False
191+
192+
# NOTE(20250624): For sqlite3 on Ubuntu 20.04, the PRAGMA integrity_check
193+
# has an unexpected side_effect, which if the db file doesn't
194+
# exist, the integrity_check will CREATE a db file for it!
195+
# This unexpected behavior doesn't occur in Ubuntu 22.04.
196+
con = sqlite3.connect(f"file:{db_f}?mode=ro", uri=True)
197+
try:
198+
if not utils.check_db_integrity(con):
199+
logger.warning(f"{db_f} fails integrity check")
200+
return False
201+
if not utils.lookup_table(con, table_name):
202+
logger.warning(f"{table_name} not found in {db_f}")
191203
return False
204+
finally:
205+
con.close()
206+
return True

0 commit comments

Comments
 (0)