1. Template Guidelines
  2. StubberDB

Template Guidelines

StubberDB

Explains StubberDB, the org's managed PostgreSQL database: naming standards, migrations, running SQL and using it from templates.

StubberDB gives your org a managed PostgreSQL database that stays synchronized with stub data. Use it to query operational data, for analytics and reporting, and to store additional relational structures that support a template.

Each org has separate draft and live schemas, mirroring the template branch model:

  • draft is for development and testing
  • live is for production use

Migrations are the only way to define and change the database structure. Within a branch, use schemas to keep the tables of different templates apart.

NOTE

StubberDB has its own tasks, such as stubberdb_run_sql. The general query_database task is for connecting to external databases.

Automatic sync

When StubberDB is enabled for your org, stubs are automatically synchronized to it as they run.

The built-in public.stubs table has one row per stub, with values such as:

  • stubref
  • state
  • subject
  • program_templateuuid
  • program_branch
  • program_context
  • metrics counters
  • created_at
  • updated_at

stubs is the only table that is created and synchronized automatically. Create and manage all other tables with migrations and SQL, and link them to stubs with foreign keys for stub-level information. A row is inserted for each new stub automatically, and is available by the time the _create lifecycle action runs.

When to use StubberDB

Use StubberDB when you need:

  • SQL queries across many stubs
  • reporting and analytics
  • relational tables beyond the synchronized stubs table
  • deterministic reads and writes using SQL
  • schema-managed storage via migrations

Naming standards

  • Table names are always snake_case.
  • Table names are plural (for example customers, items, vehicles).
  • Table names use a prefix or several keywords to create a pseudo-namespace related to the template, for example demo_insurance_customers.
  • Column names are preferably snake_case too, except when data must map to a user's system or externally defined columns.
  • Schema names are snake_case.
  • Tables specific to a template go in a separate schema named after the template, in snake_case.

The database is shared by all templates in your org, so name new tables explicitly and include a reference to the template where needed, for example sales_interaction_lead_listings.

Table columns

  • Name columns that link to the stubs table stubref, so foreign keys and joins work neatly.
  • If a column refers to a particular kind of stub, name it <qualifier>_stubref, for example customer_stubref.
  • Add created_at and updated_at columns.
  • Index stubref columns, as joining to the stubs table on them is common.

Migrations

Use migrations for structural changes: creating tables, adding columns, adding indexes and evolving relational structures over time. See StubberDB on the platform and the create migration and apply migrations tasks.

Creating migrations

Give migrations concise, descriptive names, for example:

  • create_orders_table
  • add_customer_status_column
  • add_customers_email_idx

Migration names are normalized to snake_case when saved.

Don't add ON DELETE CASCADE on foreign keys to the stubs table. That table isn't accessible to the StubberDB tasks.

Applying migrations

Pending migrations are applied to a branch:

  • Always apply migrations to draft first.
  • Apply the same migrations to live only after they have been applied successfully to draft.

Don't skip straight to live with new schema changes.

Running SQL

Use SQL to read and write data, not to change the structure; use migrations for that.

Roles

The stubberdb_run_sql task supports two roles:

  • readonly for SELECT queries and inspection (the default)
  • readwrite for statements that change data, such as INSERT, UPDATE and DELETE

Prefer readonly for inspection, and use readwrite only when a write is intended. Be careful with destructive or broad write queries, especially on the live branch.

Default StubberDB

Every org has a default StubberDB. The SQL task uses it when stubberdbuuid is left empty; supply a stubberdbuuid only to use a different configuration.

Postgres features

  • PostGIS is enabled for GIS queries and columns.
  • pgvector is enabled for vector search. The stubberdb_run_sql task has a built-in resolver that turns text into embeddings and inserts them into vector columns.
  • btree_gist is enabled, so scheduling use cases can use indexes that prevent overlapping bookings.

Using StubberDB from template contexts

  • default context: usually the main agent context, representing a single conversation. Put operational SQL tasks on its actions. For example, an action that makes a booking would have a stubberdb_run_sql task that inserts the booking.
  • _manage context: put administration and housekeeping actions here, so you can run them from the _manage context stub, for example to insert test data. An add_resource action could add a row to a resources table, and could also be webhook-enabled so external software can insert rows.

Workflow

  1. Inspect the existing tables with SQL to understand the structure.
  2. When new tables or columns are needed, add them with a migration.
  3. Apply the migration to draft and test against it.
  4. Once it works, apply the migration to live and publish the template.