Skip to content

Database Schema Overview 🟑 BETA

The database schema is declared once in botschema and published through generated reference pages.

Where this topic sits in the Developer guide

Core Architecture

PostgreSQL is the primary database and Diesel is the ORM. botschema is the only crate allowed to declare Diesel table!, joinable! and allow_tables_to_appear_in_same_query! macros (root AGENTS.md, Diesel Schema Ownership #1497).

Architecture Diagram

Layer Source of truth
Table macros botschema/src/ grouped by domain and gated by Cargo features
Migrations botserver/migrations/<version>-<name>/{up.sql,down.sql}
Consumers use botschema::<table>; or pub use botschema::<table>;
Generated reference Schema reference and tables

Entity Relationship Overview

Tables are grouped by domain: core, drive, desktop, database manager and email integration are always on; other domains are behind matching features. The generated pages are authoritative for columns.

Core Tables Structure

The core module holds tenants, organizations, branches, bots, users and sessions. SaaS domains scope their rows by branch_id (root AGENTS.md, Cloud SaaS Product Architecture).

Detailed Schema

Read the generated table reference for the current column list of any domain. Do not copy column names from prose into queries.

Schema Reference Is Generated

gen_schema_reference.py --check verifies that the generated pages match botschema and the migrations. Generated pages are never edited by hand (botbook AGENTS.md, Guards).

Table Relationships

Declared relationships are indexed in Schema Relationships. A table with no declared relationship remains standalone.

Session Flow

A chat session resolves a bot through bots, a user through users and a session through user_sessions; identity resolution is described in root AGENTS.md (gbgov Edition).

Knowledge Base Access

Knowledge documents live in Drive buckets (.gbkb), not in PostgreSQL tables; retrieval configuration is under botserver/src/core/bot/kb_context/.

Primary Relationships

Core β†’ branch β†’ bot is the common scope chain; domain tables add their own foreign keys in botschema.

Data Types

Column types are defined once in botschema and must agree with every Queryable / Insertable struct. A disagreement is a build-time or review error.

Indexing Strategy

Indexes are created by migrations. A unique index on branch_id is added by migration 6.5.31 for cloud workspaces (root AGENTS.md, Deploy in Prod Gotchas).

Migration Management

  1. Write botserver/migrations/<version>-<name>/up.sql and down.sql.
  2. Run cd botserver && diesel migration run.
  3. Run diesel print-schema and update the matching botschema module.
  4. Commit the migration and the schema change together.

Performance Considerations

Use per-bot pools where the product requires isolation (Vibe projects), and never let a feature crate redefine a table with a different type.

Connection Pooling

Pool configuration is runtime-owned; schema pages do not publish pool sizes.

Query Optimization

Query with botschema DSL types rather than raw SQL strings; use sanitize_identifier when an identifier must be dynamic (root AGENTS.md, SQL guard).

Data Retention

Retention is an operator decision; no retention value is hardcoded in the schema.

Security Features

Encryption, RBAC and row scoping are enforced in the owning crates, not by the schema macros.

Data Protection

Message text is encrypted at rest in the desktop edition (content_encrypted); schema pages do not store plaintext.

Access Control

Authorization is resolved in botsecurity / RBAC code and scoped by branch_id for SaaS domains.

Backup Strategy

Take a custom-format pg_dump before a migration; restore into a throwaway stack first (see Backup and Recovery).

Backup Types

A full dump and a schema-only dump are both produced by pg_dump options; the archive format is the restore input.

Restore Procedures

pg_restore -U postgres -d <dbname> <file> restores a custom-format archive after the target database is recreated.

Monitoring

Schema drift is caught by struct_contract_tests.rs and schema_invariants_tests.rs in botschema, not by a documentation check.

Key Metrics

No schema-specific metric is published here; operational metrics belong to the monitoring crate.

Health Checks

/health on botserver reports service readiness, not schema version.

Best Practices

  • Change schema through a migration, never by editing an applied migration.
  • Keep Queryable structs and their table! types in agreement.
  • Re-export from botschema; never copy a table! block into a feature crate.
  • Regenerate the reference instead of editing generated pages.

Future Considerations

Roadmap items live as GitHub issues. This page records the mechanism in the tree today.

Limits

  • No automated guard enforces schema ownership; it is enforced in review (root AGENTS.md).
  • Migration directory names are compared as dash-stripped strings; date-prefixed names sort before 6.x migrations.
  • Generated reference files under generated/ and schema/ are not edited by hand.