|
| 1 | +-- MeshHook core tables, Postgres. |
| 2 | +-- |
| 3 | +-- Generated from migrations/0001_core_tables.sql with `npx libsql-pg |
| 4 | +-- convert-schema`, then reviewed and hand-fixed. Applied by scripts/db-migrate.js |
| 5 | +-- when DATABASE_URL is postgres:// (the only URL the app accepts now). |
| 6 | +-- |
| 7 | +-- Decisions, so the SQLite files and the app's queries keep working unchanged: |
| 8 | +-- |
| 9 | +-- * Every timestamp column stays TEXT holding ISO-8601 UTC, exactly the value |
| 10 | +-- the app writes (new Date().toISOString()) and compares. The SQLite |
| 11 | +-- default strftime('%Y-%m-%dT%H:%M:%fZ', 'now') becomes meshhook_now_iso(), |
| 12 | +-- which produces the same 24-character string, and the libsql-pg rewriter |
| 13 | +-- turns the app's inline strftime(...) into the same to_char() expression. |
| 14 | +-- Promoting the columns to timestamptz (what the converter suggested) would |
| 15 | +-- break every `set updated_at = strftime(...)` the app issues, because |
| 16 | +-- Postgres will not assign text to a timestamptz column. |
| 17 | +-- * uuid ids default to gen_random_uuid()::text (Postgres 13+). The SQLite |
| 18 | +-- randomblob()/random() expression the converter carried over does not run |
| 19 | +-- on Postgres (random() is a double there, so `% 4` has no operator). |
| 20 | +-- * `integer primary key` (a rowid alias) becomes an identity bigint, so |
| 21 | +-- lastInsertRowid keeps working through libsql-pg (it appends RETURNING). |
| 22 | +-- * The AFTER UPDATE triggers become one BEFORE UPDATE trigger function. |
| 23 | +-- * 0/1 flag columns stay bigint with their CHECK (the app compares = 1). |
| 24 | + |
| 25 | +create extension if not exists pgcrypto; |
| 26 | + |
| 27 | +-- The stored timestamp format: 2026-08-11T12:00:00.000Z. |
| 28 | +create or replace function meshhook_now_iso() returns text |
| 29 | +language sql stable as $$ |
| 30 | + select to_char(now() at time zone 'utc', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"') |
| 31 | +$$; |
| 32 | + |
| 33 | +-- Replaces the per-table AFTER UPDATE triggers: stamp updated_at on the row |
| 34 | +-- being written. workflow_runs adds a WHEN guard below so a statement that sets |
| 35 | +-- updated_at itself is left alone (the orchestrator sets finished_at and |
| 36 | +-- updated_at together). |
| 37 | +create or replace function meshhook_set_updated_at() returns trigger |
| 38 | +language plpgsql as $$ |
| 39 | +begin |
| 40 | + new.updated_at := meshhook_now_iso(); |
| 41 | + return new; |
| 42 | +end |
| 43 | +$$; |
| 44 | + |
| 45 | +create table if not exists projects ( |
| 46 | + id text primary key default (gen_random_uuid()::text), |
| 47 | + owner text not null, |
| 48 | + name text not null, |
| 49 | + created_at text not null default (meshhook_now_iso()), |
| 50 | + updated_at text not null default (meshhook_now_iso()) |
| 51 | +); |
| 52 | + |
| 53 | +create index if not exists idx_projects_owner on projects(owner); |
| 54 | + |
| 55 | +drop trigger if exists update_projects_updated_at on projects; |
| 56 | +create trigger update_projects_updated_at |
| 57 | +before update on projects for each row |
| 58 | +execute function meshhook_set_updated_at(); |
| 59 | + |
| 60 | +-- Encrypted secrets vault. value_encrypted is AES-GCM ciphertext, bytea. |
| 61 | +create table if not exists secrets ( |
| 62 | + id text primary key default (gen_random_uuid()::text), |
| 63 | + project_id text not null references projects(id) on delete cascade, |
| 64 | + "key" text not null, |
| 65 | + value_encrypted bytea not null, |
| 66 | + created_at text not null default (meshhook_now_iso()), |
| 67 | + updated_at text not null default (meshhook_now_iso()), |
| 68 | + unique(project_id, "key") |
| 69 | +); |
| 70 | + |
| 71 | +create index if not exists idx_secrets_project_id on secrets(project_id); |
| 72 | + |
| 73 | +drop trigger if exists update_secrets_updated_at on secrets; |
| 74 | +create trigger update_secrets_updated_at |
| 75 | +before update on secrets for each row |
| 76 | +execute function meshhook_set_updated_at(); |
| 77 | + |
| 78 | +create table if not exists workflow_definitions ( |
| 79 | + id text primary key default (gen_random_uuid()::text), |
| 80 | + project_id text not null references projects(id) on delete cascade, |
| 81 | + slug text not null, |
| 82 | + name text not null, |
| 83 | + description text, |
| 84 | + status text default 'draft' check (status in ('draft', 'published', 'archived')), |
| 85 | + user_id text, |
| 86 | + version bigint not null default 1, |
| 87 | + definition text not null, |
| 88 | + created_at text not null default (meshhook_now_iso()), |
| 89 | + updated_at text not null default (meshhook_now_iso()), |
| 90 | + unique(project_id, slug, version) |
| 91 | +); |
| 92 | + |
| 93 | +create index if not exists idx_workflow_definitions_project_id on workflow_definitions(project_id); |
| 94 | +create index if not exists idx_workflow_definitions_slug on workflow_definitions(project_id, slug); |
| 95 | +create index if not exists idx_workflow_definitions_status on workflow_definitions(status); |
| 96 | +create index if not exists idx_workflow_definitions_user_id on workflow_definitions(user_id); |
| 97 | + |
| 98 | +drop trigger if exists update_workflow_definitions_updated_at on workflow_definitions; |
| 99 | +create trigger update_workflow_definitions_updated_at |
| 100 | +before update on workflow_definitions for each row |
| 101 | +execute function meshhook_set_updated_at(); |
| 102 | + |
| 103 | +create table if not exists workflow_runs ( |
| 104 | + id text primary key default (gen_random_uuid()::text), |
| 105 | + project_id text not null references projects(id) on delete cascade, |
| 106 | + workflow_id text not null references workflow_definitions(id) on delete cascade, |
| 107 | + status text not null check (status in ('running', 'succeeded', 'failed', 'paused', 'canceled')), |
| 108 | + started_at text not null default (meshhook_now_iso()), |
| 109 | + finished_at text, |
| 110 | + created_at text not null default (meshhook_now_iso()), |
| 111 | + updated_at text not null default (meshhook_now_iso()) |
| 112 | +); |
| 113 | + |
| 114 | +create index if not exists idx_workflow_runs_project_id on workflow_runs(project_id); |
| 115 | +create index if not exists idx_workflow_runs_workflow_id on workflow_runs(workflow_id); |
| 116 | +create index if not exists idx_workflow_runs_status on workflow_runs(status); |
| 117 | +create index if not exists idx_workflow_runs_project_started on workflow_runs(project_id, started_at desc); |
| 118 | + |
| 119 | +-- Deliberately guarded: the orchestrator sets finished_at and updated_at in one |
| 120 | +-- statement, and the trigger must not rewrite the value it just wrote. |
| 121 | +drop trigger if exists update_workflow_runs_updated_at on workflow_runs; |
| 122 | +create trigger update_workflow_runs_updated_at |
| 123 | +before update of status, finished_at on workflow_runs for each row |
| 124 | +when (new.updated_at is not distinct from old.updated_at) |
| 125 | +execute function meshhook_set_updated_at(); |
| 126 | + |
| 127 | +-- Event-sourcing log. The (run_id, ts) index is what every replay query uses; |
| 128 | +-- retention is scripts/prune-events.js. |
| 129 | +create table if not exists workflow_events ( |
| 130 | + id bigint generated by default as identity primary key, |
| 131 | + run_id text not null references workflow_runs(id) on delete cascade, |
| 132 | + ts text not null default (meshhook_now_iso()), |
| 133 | + type text not null, |
| 134 | + payload text not null, |
| 135 | + created_at text not null default (meshhook_now_iso()) |
| 136 | +); |
| 137 | + |
| 138 | +create index if not exists idx_workflow_events_run_id on workflow_events(run_id); |
| 139 | +create index if not exists idx_workflow_events_run_ts on workflow_events(run_id, ts); |
| 140 | +create index if not exists idx_workflow_events_type on workflow_events(type); |
| 141 | +create index if not exists idx_workflow_events_ts on workflow_events(ts); |
| 142 | + |
| 143 | +create table if not exists audit_log ( |
| 144 | + id bigint generated by default as identity primary key, |
| 145 | + project_id text references projects(id) on delete cascade, |
| 146 | + user_id text not null, |
| 147 | + action text not null, |
| 148 | + resource_type text not null, |
| 149 | + resource_id text, |
| 150 | + metadata text, |
| 151 | + ip_address text, |
| 152 | + user_agent text, |
| 153 | + created_at text not null default (meshhook_now_iso()) |
| 154 | +); |
| 155 | + |
| 156 | +create index if not exists idx_audit_log_project_id on audit_log(project_id); |
| 157 | +create index if not exists idx_audit_log_user_id on audit_log(user_id); |
| 158 | +create index if not exists idx_audit_log_created_at on audit_log(created_at desc); |
| 159 | +create index if not exists idx_audit_log_action on audit_log(action); |
0 commit comments