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:
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
|
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 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)::bigint AS match_count,
|
(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), 'epoch')::timestamptz AS last_matched_at
|
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 id = $1 AND ($2::text IS NULL OR org_id = $2)
|
FROM mappings WHERE mappings.id = $2 AND ($1::text IS NULL OR mappings.org_id = $1 OR mappings.built_in)
|
||||||
`
|
`
|
||||||
|
|
||||||
type GetMappingParams struct {
|
type GetMappingParams struct {
|
||||||
ID string
|
|
||||||
ScopeOrg pgtype.Text
|
ScopeOrg pgtype.Text
|
||||||
|
ID string
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetMappingRow struct {
|
type GetMappingRow struct {
|
||||||
@@ -405,7 +405,7 @@ type GetMappingRow struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) GetMapping(ctx context.Context, arg GetMappingParams) (GetMappingRow, error) {
|
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
|
var i GetMappingRow
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
&i.Mapping.ID,
|
&i.Mapping.ID,
|
||||||
@@ -417,6 +417,7 @@ func (q *Queries) GetMapping(ctx context.Context, arg GetMappingParams) (GetMapp
|
|||||||
&i.Mapping.EventType,
|
&i.Mapping.EventType,
|
||||||
&i.Mapping.OrgID,
|
&i.Mapping.OrgID,
|
||||||
&i.Mapping.WorkspaceID,
|
&i.Mapping.WorkspaceID,
|
||||||
|
&i.Mapping.BuiltIn,
|
||||||
&i.MatchCount,
|
&i.MatchCount,
|
||||||
&i.LastMatchedAt,
|
&i.LastMatchedAt,
|
||||||
)
|
)
|
||||||
@@ -937,10 +938,10 @@ func (q *Queries) ListEventTypes(ctx context.Context, scopeOrg pgtype.Text) ([]E
|
|||||||
}
|
}
|
||||||
|
|
||||||
const listMappings = `-- name: ListMappings :many
|
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 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)::bigint AS match_count,
|
(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), 'epoch')::timestamptz AS last_matched_at
|
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 org_id = $1) ORDER BY created_at DESC
|
FROM mappings WHERE ($1::text IS NULL OR mappings.org_id = $1 OR mappings.built_in) ORDER BY created_at DESC
|
||||||
`
|
`
|
||||||
|
|
||||||
type ListMappingsRow struct {
|
type ListMappingsRow struct {
|
||||||
@@ -968,6 +969,7 @@ func (q *Queries) ListMappings(ctx context.Context, scopeOrg pgtype.Text) ([]Lis
|
|||||||
&i.Mapping.EventType,
|
&i.Mapping.EventType,
|
||||||
&i.Mapping.OrgID,
|
&i.Mapping.OrgID,
|
||||||
&i.Mapping.WorkspaceID,
|
&i.Mapping.WorkspaceID,
|
||||||
|
&i.Mapping.BuiltIn,
|
||||||
&i.MatchCount,
|
&i.MatchCount,
|
||||||
&i.LastMatchedAt,
|
&i.LastMatchedAt,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
|
|||||||
@@ -310,6 +310,7 @@ type Mapping struct {
|
|||||||
EventType string
|
EventType string
|
||||||
OrgID string
|
OrgID string
|
||||||
WorkspaceID string
|
WorkspaceID string
|
||||||
|
BuiltIn bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type MappingMatch struct {
|
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
|
-- name: GetMapping :one
|
||||||
SELECT sqlc.embed(mappings),
|
SELECT sqlc.embed(mappings),
|
||||||
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
|
(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), 'epoch')::timestamptz AS last_matched_at
|
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 id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
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
|
-- name: ListMappings :many
|
||||||
SELECT sqlc.embed(mappings),
|
SELECT sqlc.embed(mappings),
|
||||||
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
|
(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), 'epoch')::timestamptz AS last_matched_at
|
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 org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC;
|
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
|
-- name: UpdateMapping :execrows
|
||||||
UPDATE mappings
|
UPDATE mappings
|
||||||
|
|||||||
Reference in new issue
Block a user