|
17 | 17 | import logging
|
18 | 18 | import sqlite3
|
19 | 19 | from contextlib import closing
|
| 20 | +from pathlib import Path |
20 | 21 | from typing import Optional
|
21 | 22 |
|
22 | 23 | from multidict import CIMultiDict
|
|
30 | 31 | gen_sql_stmt,
|
31 | 32 | utils,
|
32 | 33 | )
|
33 |
| -from simple_sqlite3_orm.utils import check_db_integrity |
34 | 34 | from typing_extensions import Annotated
|
35 | 35 |
|
36 | 36 | from otaclient_common._typing import StrOrPath
|
@@ -184,8 +184,23 @@ def init_db(db_f: StrOrPath, table_name: str) -> None:
|
184 | 184 |
|
185 | 185 |
|
186 | 186 | 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}") |
191 | 203 | return False
|
| 204 | + finally: |
| 205 | + con.close() |
| 206 | + return True |
0 commit comments