Adding a DB Column
How to add a column to an existing table.
-
In
src/db/schema.sql, add the column to theCREATE TABLEstatement and add an idempotentadd_column_if_not_exists()call. Existing databases never rerunCREATE TABLE IF NOT EXISTS, so without the call the column reaches only fresh installs:SELECT add_column_if_not_exists('table_name', 'column_name', 'BOOLEAN', 'false'); -
If existing rows need a backfill, or a column moves or is dropped, add a numbered migration
src/db/migrations/NNN_name.sqlwith a matching.down.sql. ChooseNNNafter checking every branch (git ls-tree), becausebun run check-migrationsonly sees the working tree. -
Add the field to the Zod schema and types in
src/types/db/schema.ts. -
Read and write it through the owning repository in
src/utils/db/repositories/(see Raw SQL Boundary). -
After a successful write, invalidate the affected caches in the same code path; never before the write and never on failure.
-
Runtime config columns: a column on a
server_*_configstable that the bot reads throughtomoriState.configmust also be added to both config SELECTs insrc/utils/db/repositories/PersonaRepository.ts(loadTomoriStateandloadAllForServer; search forscaps.tool_use_enabled).assembledServerConfigSchemagives each field a.default(), so a column missing from the SELECT is silently replaced by its default and a check likeconfig.flag === falsenever fires, with no type or test failure. -
Descriptions: model and preset descriptions use the existing
descriptionsJSONB locale map. Do not add a column per language; add translations to the seed catalog’si18nfield and keep English indesc.
Verify
Section titled “Verify”bun run checkbun run lintbun run check-migrations # if you added a migrationbun run db:lifecycle # needs a local PostgreSQL user that can create databasesSchema reference: Database Schema. Cache map: Caching.