Skip to content

Commit

Permalink
webapp: handle read-only database
Browse files Browse the repository at this point in the history
  • Loading branch information
aiooss-anssi committed Dec 17, 2023
1 parent 6716c64 commit 02f3c45
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 9 additions & 0 deletions webapp/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ async def connect(self):
await self.con.execute("PRAGMA synchronous=normal")
await self.init_database_structure()

async def is_readonly(self) -> bool:
assert self.con is not None, "database connection closed"
try:
# This statement has no effects on a writable database
await self.con.execute("pragma user_version=0")
return False
except aiosqlite.OperationalError:
return True

async def close(self):
assert self.con is not None, "database connection closed"
await self.con.close()
Expand Down
12 changes: 8 additions & 4 deletions webapp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,14 @@ async def lifespan(app):
Close database on exit.
"""
await database.connect()
await database.update_ctf_config(CTF_CONFIG)
db_task = asyncio.create_task(database.importer_task())
yield
db_task.cancel()
if await database.is_readonly():
print("SQLite database opened in read-only mode", flush=True)
yield
else:
await database.update_ctf_config(CTF_CONFIG)
db_task = asyncio.create_task(database.importer_task())
yield
db_task.cancel()
await database.close()


Expand Down

0 comments on commit 02f3c45

Please sign in to comment.