ensureSchema

open fun ensureSchema()

Ensure the table schema is present and queryable before critical reads/writes. Background: the host app (LoginActivity) raw-deletes the SQLite files under databases/ on first login / account switch, AFTER this DB was already opened. That leaves our cached connection pointing at a wiped, table-less DB and SQLite never re-runs onCreate — so every query fails "no such table: PropertyData" for the rest of the process and SDK setup reports a misleading NETWORK_ERROR (9201). Fix: cheap fast-path when the schema is intact; otherwise rebuild the schema on a BRAND-NEW connection pool and atomically swap to it. A plain CREATE TABLE on the live connection is NOT enough under WAL (the create lands on one pooled connection but other pooled connections still see the wiped state / a different file), so we need a fresh pool that re-runs onCreate/onOpen on the current file. Concurrency: we deliberately do NOT close() the previous pool. Other threads (the engine's DatabaseCallback reads, prefetch workers, download writers) may be mid-query/transaction on its connections, and closing the shared pool under them throws IllegalStateException ("connection pool has been closed"). Instead we open a fresh DbHelper, publish it, and simply stop referencing the old one: its connections finish their in-flight work on the owning threads and the pool is reclaimed by GC once unreferenced (a one-time, bounded cost on the rare wipe path). Single-shot reads/writes take one getDb per op and are unaffected; multi-statement transactions capture the handle once (see SerializedDataTable) so a begin/commit pair never straddles the swap.