diff --git a/internal/infrastructure/postgres/migrations/0066_mapping_built_in.sql b/internal/infrastructure/postgres/migrations/0066_mapping_built_in.sql new file mode 100644 index 000000000..3d2030ab0 --- /dev/null +++ b/internal/infrastructure/postgres/migrations/0066_mapping_built_in.sql @@ -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; diff --git a/internal/infrastructure/postgres/postgresdb/edastore.sql.go b/internal/infrastructure/postgres/postgresdb/edastore.sql.go index a1759ca03..3503daae7 100644 --- a/internal/infrastructure/postgres/postgresdb/edastore.sql.go +++ b/internal/infrastructure/postgres/postgresdb/edastore.sql.go @@ -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 { diff --git a/internal/infrastructure/postgres/postgresdb/models.go b/internal/infrastructure/postgres/postgresdb/models.go index 4d9ab593c..d1dfa0f0a 100644 --- a/internal/infrastructure/postgres/postgresdb/models.go +++ b/internal/infrastructure/postgres/postgresdb/models.go @@ -310,6 +310,7 @@ type Mapping struct { EventType string OrgID string WorkspaceID string + BuiltIn bool } type MappingMatch struct { diff --git a/internal/infrastructure/postgres/queries/edastore.sql b/internal/infrastructure/postgres/queries/edastore.sql index 211affc46..e6c884532 100644 --- a/internal/infrastructure/postgres/queries/edastore.sql +++ b/internal/infrastructure/postgres/queries/edastore.sql @@ -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