fix(postgres): let every org read a built-in mapping so a non-system org can arm a schedule trigger

This commit is contained in:
sam committed 2026-10-27 10:13:03 +11:00
1 parent 4304454b9d
commit 5495c5f6ec
4 files changed
+28 -16

No files matched your search

@@ -0,0 +1,9 @@
-- A built-in mapping ships with the instance and is readable by every org, so
-- any org can bind to it. Writes stay confined to the owning org.
-- +goose Up
ALTER TABLE mappings ADD COLUMN built_in boolean NOT NULL DEFAULT false;
UPDATE mappings SET built_in = true WHERE id = 'schedule-fired' AND org_id = 'org-sys';
-- +goose Down
ALTER TABLE mappings DROP COLUMN built_in;
@@ -387,15 +387,15 @@ func (q *Queries) GetEventType(ctx context.Context, arg GetEventTypeParams) (Eve
}
const getMapping = `-- name: GetMapping :one
SELECT mappings.id, mappings.name, mappings.source_hint, mappings.fields, mappings.created_at, mappings.updated_at, mappings.event_type, mappings.org_id, mappings.workspace_id,
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
FROM mappings WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
SELECT mappings.id, mappings.name, mappings.source_hint, mappings.fields, mappings.created_at, mappings.updated_at, mappings.event_type, mappings.org_id, mappings.workspace_id, mappings.built_in,
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id AND ($1::text IS NULL OR EXISTS (SELECT 1 FROM captures c WHERE c.id = mm.capture AND c.org_id = $1)))::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id AND ($1::text IS NULL OR EXISTS (SELECT 1 FROM captures c WHERE c.id = mm.capture AND c.org_id = $1))), 'epoch')::timestamptz AS last_matched_at
FROM mappings WHERE mappings.id = $2 AND ($1::text IS NULL OR mappings.org_id = $1 OR mappings.built_in)
`
type GetMappingParams struct {
ID string
ScopeOrg pgtype.Text
ID string
}
type GetMappingRow struct {
@@ -405,7 +405,7 @@ type GetMappingRow struct {
}
func (q *Queries) GetMapping(ctx context.Context, arg GetMappingParams) (GetMappingRow, error) {
row := q.db.QueryRow(ctx, getMapping, arg.ID, arg.ScopeOrg)
row := q.db.QueryRow(ctx, getMapping, arg.ScopeOrg, arg.ID)
var i GetMappingRow
err := row.Scan(
&i.Mapping.ID,
@@ -417,6 +417,7 @@ func (q *Queries) GetMapping(ctx context.Context, arg GetMappingParams) (GetMapp
&i.Mapping.EventType,
&i.Mapping.OrgID,
&i.Mapping.WorkspaceID,
&i.Mapping.BuiltIn,
&i.MatchCount,
&i.LastMatchedAt,
)
@@ -937,10 +938,10 @@ func (q *Queries) ListEventTypes(ctx context.Context, scopeOrg pgtype.Text) ([]E
}
const listMappings = `-- name: ListMappings :many
SELECT mappings.id, mappings.name, mappings.source_hint, mappings.fields, mappings.created_at, mappings.updated_at, mappings.event_type, mappings.org_id, mappings.workspace_id,
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
FROM mappings WHERE ($1::text IS NULL OR org_id = $1) ORDER BY created_at DESC
SELECT mappings.id, mappings.name, mappings.source_hint, mappings.fields, mappings.created_at, mappings.updated_at, mappings.event_type, mappings.org_id, mappings.workspace_id, mappings.built_in,
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id AND ($1::text IS NULL OR EXISTS (SELECT 1 FROM captures c WHERE c.id = mm.capture AND c.org_id = $1)))::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id AND ($1::text IS NULL OR EXISTS (SELECT 1 FROM captures c WHERE c.id = mm.capture AND c.org_id = $1))), 'epoch')::timestamptz AS last_matched_at
FROM mappings WHERE ($1::text IS NULL OR mappings.org_id = $1 OR mappings.built_in) ORDER BY created_at DESC
`
type ListMappingsRow struct {
@@ -968,6 +969,7 @@ func (q *Queries) ListMappings(ctx context.Context, scopeOrg pgtype.Text) ([]Lis
&i.Mapping.EventType,
&i.Mapping.OrgID,
&i.Mapping.WorkspaceID,
&i.Mapping.BuiltIn,
&i.MatchCount,
&i.LastMatchedAt,
); err != nil {
@@ -310,6 +310,7 @@ type Mapping struct {
EventType string
OrgID string
WorkspaceID string
BuiltIn bool
}
type MappingMatch struct {
@@ -77,15 +77,15 @@ VALUES (sqlc.arg(id), sqlc.arg(name), sqlc.arg(source_hint), sqlc.arg(event_type
-- name: GetMapping :one
SELECT sqlc.embed(mappings),
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
FROM mappings WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id AND (sqlc.narg(scope_org)::text IS NULL OR EXISTS (SELECT 1 FROM captures c WHERE c.id = mm.capture AND c.org_id = sqlc.narg(scope_org))))::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id AND (sqlc.narg(scope_org)::text IS NULL OR EXISTS (SELECT 1 FROM captures c WHERE c.id = mm.capture AND c.org_id = sqlc.narg(scope_org)))), 'epoch')::timestamptz AS last_matched_at
FROM mappings WHERE mappings.id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR mappings.org_id = sqlc.narg(scope_org) OR mappings.built_in);
-- name: ListMappings :many
SELECT sqlc.embed(mappings),
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
FROM mappings WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC;
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id AND (sqlc.narg(scope_org)::text IS NULL OR EXISTS (SELECT 1 FROM captures c WHERE c.id = mm.capture AND c.org_id = sqlc.narg(scope_org))))::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id AND (sqlc.narg(scope_org)::text IS NULL OR EXISTS (SELECT 1 FROM captures c WHERE c.id = mm.capture AND c.org_id = sqlc.narg(scope_org)))), 'epoch')::timestamptz AS last_matched_at
FROM mappings WHERE (sqlc.narg(scope_org)::text IS NULL OR mappings.org_id = sqlc.narg(scope_org) OR mappings.built_in) ORDER BY created_at DESC;
-- name: UpdateMapping :execrows
UPDATE mappings