migrations
migrations
¶
Credential-DB schema bootstrap + forward migrations.
Two functions, both idempotent, both called by every opener of the
sqlite3 file (CredentialDB
for writers, the vault daemon's read-only _TokenDB for readers):
ensure_credentials_schemadeclares the current shape viaCREATE TABLE IF NOT EXISTSso fresh installs land at the latest schema in one shot.migrate_credential_db_schemawalks legacy DBs forward step by step, gated byPRAGMA user_versionso already-upgraded files are a no-op.
Splitting these out of db.py keeps the data-access layer free of
ALTER TABLE machinery and gives schema changes a focused review
target — every future bump touches one file.
SCHEMA_VERSION = 2
module-attribute
¶
ensure_credentials_schema(conn)
¶
Create the credential / SSH-key / phantom-token tables if missing.
Idempotent — every statement is IF NOT EXISTS. Exposed at module
level so every opener of the DB file runs it before issuing queries.
Without this, a daemon that opens an empty DB on a fresh install
(before any CLI command has touched the file) hits no such table:
credentials on the first query and crashes the unit.
Source code in src/terok_sandbox/vault/store/migrations.py
migrate_credential_db_schema(conn)
¶
Walk legacy credential-DB rows forward to the current schema.
Tracked via PRAGMA user_version so the whole function is a no-op
on already-upgraded DBs. Each current < N branch handles one
forward step; the final PRAGMA user_version set commits the
whole upgrade in one go.
Exposed at module level so every opener of the DB file
(CredentialDB for
writers, _TokenDB in the vault daemon for readers) runs it
before issuing queries — otherwise a daemon that restarts before any
CLI command has touched the DB would hit "no such column: …" on a
freshly-upgraded host.
The cryptography import is scoped to the v0 → v1 branch so
already-migrated DBs (the common case) don't pay an import cost,
and the storage module keeps tach-clean at import time.