automations

Trigger-based automations with action sequences and run history

Automations

Trigger-based automations with ordered action sequences and execution history. Supports event, schedule, webhook, and manual triggers.

Tables

automations

ColumnTypeDescription
iduuidPrimary key, auto-generated
org_iduuidReferences organizations(id), cascade delete
creator_iduuidReferences users(id), set null on delete
nametextName of the automation
descriptiontextOptional description
trigger_typeautomation_trigger_typeWhat starts this automation
trigger_configjsonbCron expression, event filter, webhook URL, etc.
statusautomation_statusCurrent lifecycle status, defaults to draft
last_run_attimestamptzTimestamp of the most recent run
run_countintegerTotal number of runs, defaults to 0
created_attimestamptzRow creation timestamp
updated_attimestamptzAuto-updated on modification
metadatajsonbArbitrary key-value data

automation_actions

ColumnTypeDescription
iduuidPrimary key, auto-generated
org_iduuidReferences organizations(id), cascade delete
automation_iduuidReferences automations(id), cascade delete
action_typetextAction kind: send_email, update_field, create_task, webhook, etc.
action_configjsonbConfiguration for the action
positionintegerExecution order within the automation, defaults to 0
created_attimestamptzRow creation timestamp
updated_attimestamptzAuto-updated on modification
metadatajsonbArbitrary key-value data

automation_runs

ColumnTypeDescription
iduuidPrimary key, auto-generated
org_iduuidReferences organizations(id), cascade delete
automation_iduuidReferences automations(id), cascade delete
statusrun_statusCurrent run status, defaults to pending
started_attimestamptzWhen the run started executing
completed_attimestamptzWhen the run finished
error_messagetextError details if the run failed
resultjsonbOutput data from the run
created_attimestamptzRow creation timestamp
updated_attimestamptzAuto-updated on modification
metadatajsonbArbitrary key-value data

Enums

automation_trigger_type

ValueDescription
eventTriggered by a system event
scheduleTriggered on a cron schedule
webhookTriggered by an incoming webhook
manualTriggered by a user action

automation_status

ValueDescription
activeRunning and ready to trigger
pausedTemporarily disabled
draftNot yet activated
archivedRetired, no longer triggerable

run_status

ValueDescription
pendingQueued but not yet started
runningCurrently executing
completedFinished successfully
failedFinished with an error

Row-Level Security

All three tables are scoped to the current user's organization via get_user_org_id(). Select, insert, and update are allowed for any org member. Delete requires the is_admin() check.

Dependencies

  • identity -- organizations and users tables, get_user_org_id() and is_admin() functions, set_updated_at() trigger function

Example Queries

List all active automations for the current org:

SELECT id, name, trigger_type, trigger_config, last_run_at, run_count
FROM automations
WHERE status = 'active'
ORDER BY created_at DESC;

Get the ordered action steps for a specific automation:

SELECT id, action_type, action_config, position
FROM automation_actions
WHERE automation_id = '<automation_id>'
ORDER BY position ASC;

Recent runs across all automations (last 50):

SELECT
  r.id,
  a.name AS automation_name,
  r.status,
  r.started_at,
  r.completed_at,
  r.error_message
FROM automation_runs r
JOIN automations a ON a.id = r.automation_id
ORDER BY r.started_at DESC
LIMIT 50;

Failed runs in the last 7 days:

SELECT
  r.id,
  a.name AS automation_name,
  r.error_message,
  r.started_at,
  r.result
FROM automation_runs r
JOIN automations a ON a.id = r.automation_id
WHERE r.status = 'failed'
  AND r.started_at >= now() - interval '7 days'
ORDER BY r.started_at DESC;

Automations with their action count:

SELECT
  a.id,
  a.name,
  a.status,
  a.trigger_type,
  count(aa.id) AS action_count
FROM automations a
LEFT JOIN automation_actions aa ON aa.automation_id = a.id
GROUP BY a.id, a.name, a.status, a.trigger_type
ORDER BY a.name;

Run success rate per automation:

SELECT
  a.name,
  a.run_count,
  count(r.id) FILTER (WHERE r.status = 'completed') AS succeeded,
  count(r.id) FILTER (WHERE r.status = 'failed') AS failed
FROM automations a
LEFT JOIN automation_runs r ON r.automation_id = a.id
GROUP BY a.id, a.name, a.run_count
ORDER BY a.name;