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:
draftis for development and testingliveis 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.
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:
stubrefstatesubjectprogram_templateuuidprogram_branchprogram_context- metrics counters
created_atupdated_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
stubstable - 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_casetoo, 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
stubstablestubref, so foreign keys and joins work neatly. - If a column refers to a particular kind of stub, name it
<qualifier>_stubref, for examplecustomer_stubref. - Add
created_atandupdated_atcolumns. - Index
stubrefcolumns, as joining to thestubstable 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_tableadd_customer_status_columnadd_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
draftfirst. - Apply the same migrations to
liveonly after they have been applied successfully todraft.
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:
readonlyforSELECTqueries and inspection (the default)readwritefor statements that change data, such asINSERT,UPDATEandDELETE
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_sqltask 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
defaultcontext: 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 astubberdb_run_sqltask that inserts the booking._managecontext: put administration and housekeeping actions here, so you can run them from the_managecontext stub, for example to insert test data. Anadd_resourceaction could add a row to a resources table, and could also be webhook-enabled so external software can insert rows.
Workflow
- Inspect the existing tables with SQL to understand the structure.
- When new tables or columns are needed, add them with a migration.
- Apply the migration to
draftand test against it. - Once it works, apply the migration to
liveand publish the template.