sqlcipher_migration
Converts one plaintext SQLite file to SQLCipher, crash-safely.
The steps, under an exclusive cross-process lock for the file:
- Checkpoint the plaintext file's WAL into it and leave WAL mode, so the main file holds everything. SQLite refuses to leave WAL mode while another connection has the file open, which is how a store in use is told apart.
- Export it with
sqlcipher_exportinto<file>.encrypting, under the cache key with SQLCipher 4's stock settings, and copy the header values the export doesn't (user_versionandapplication_id). The copy is in WAL mode. - Verify the copy: it opens with the key, both integrity checks pass, it has the same tables with the same row counts, and its header isn't plaintext.
fsyncthe copy, delete the original's-waland-shm, replace the original with the copy, andfsyncthe directory.
The original is never changed before the replace, apart from steps that lose
nothing (the checkpoint and leaving WAL mode). A failure before then deletes
the copy and leaves a working plaintext store. A crash leaves at most a
<file>.encrypting copy, which remove_leftovers deletes before the next try.
The -wal and -shm go before the replace so that SQLite never applies a
plaintext WAL to the encrypted file.
Logs above DEBUG never name the file; nothing logs the key.
Module
Functions
migrate_to_sqlcipher
def migrate_to_sqlcipher( path: Path, key: CacheKey,) ‑> MigrationResult:Converts the plaintext SQLite file at path to SQLCipher under key.
Holds <path>.encrypt.lock throughout, without waiting for it. Leftovers
from an earlier crash are removed first.
Arguments
path: The store.key: The cache key to encrypt it under.
Returns
What happened. FAILED leaves the store plaintext and working, to be
tried again next time. This never raises an Exception.
remove_leftovers
def remove_leftovers(path: Path) ‑> None:Deletes an encrypted copy of path left by an earlier crash.
Removes <path>.encrypting and its -journal, -wal and -shm, if
present. The store itself is never touched.
Arguments
path: The store.
Classes
MigrationFailure
class MigrationFailure(*args, **kwds):Why a store wasn't migrated. Short codes, safe for telemetry.
Ancestors
Variables
- static
CIPHER_INTEGRITY_CHECK
- static
EXPORT_FAILED
- static
HEADER_PLAINTEXT
- static
INTEGRITY_CHECK
- static
IN_USE
- static
IO_ERROR
- static
LOCKED
- static
REPLACE_FAILED
- static
VERIFY_ERROR
- static
VERIFY_ROW_COUNT
- static
VERIFY_TABLES
MigrationOutcome
class MigrationOutcome(*args, **kwds):What happened to one store.
Ancestors
Variables
- static
ALREADY_ENCRYPTED
- static
FAILED
- static
MIGRATED
- static
NOT_FOUND
- static
SKIPPED_LOW_DISK
MigrationResult
class MigrationResult( outcome: MigrationOutcome, reason: MigrationFailure | None, bytes: int, duration_seconds: float,):The outcome for one store.
Attributes
outcome: What happened.reason: Why, when the outcome isFAILED; otherwiseNone.bytes: The original file's size, 0 when there was none.duration_seconds: How long it took, waiting for the lock included.
Variables
-
bytes : int
-
duration_seconds : float
-
outcome : MigrationOutcome
-
reason : MigrationFailure | None