Skip to content

mig: add workos roles and sync tables migration#2515

Merged
tgmendes merged 1 commit intomainfrom
workos-sync-pr0-migrations
Apr 30, 2026
Merged

mig: add workos roles and sync tables migration#2515
tgmendes merged 1 commit intomainfrom
workos-sync-pr0-migrations

Conversation

@tgmendes
Copy link
Copy Markdown
Contributor

@tgmendes tgmendes commented Apr 30, 2026

Summary

First chunk of the WorkOS sync split (originally #2093). Schema only — no consumers, no behavior change. Safe to apply ahead of subsequent PRs.

Adds five tables that downstream PRs (handlers, activities, workflows, webhook) will populate from WorkOS events:

  • global_roles — environment-level WorkOS roles (e.g. admin, member).
  • organization_roles — per-org WorkOS roles.
  • organization_role_assignments — which roles each WorkOS user has within an org. role_urn encodes scope+id (role:global:<uuid> or role:organization:<uuid>); no FK on role_urn, deletes handled in app. user_id nullable so membership events can land before the Gram user exists.
  • workos_organization_syncs — per-org event cursor for organization.* / organization_role.* events.
  • workos_user_syncs — singleton cursor for organization_membership.* events. CHECK (id = 1) enforces single row.

Schema decisions

  • workos_organization_syncs.id is uuid DEFAULT generate_uuidv7() (multi-row, matches schema convention elsewhere).
  • workos_user_syncs.id stays bigint identity with CHECK (id = 1) — UUID adds nothing to a singleton.
  • organization_role_assignments: unique on (organization_id, workos_user_id, role_urn); partial index on (organization_id, user_id) WHERE user_id IS NOT NULL.
  • organization_roles: unique on (organization_id, workos_slug) — slugs are immutable in WorkOS.
  • global_roles: unique on workos_slug.
  • eventual consistency: every WorkOS-driven row tracks workos_updated_at separately from updated_at so re-replayed events don't clobber newer state.

Sample data (from local instance after webhook traffic)

Note: local rows show bigint ids on workos_organization_syncs because the dev DB pre-dates the UUID switch in this PR. New deployments / db:reset will produce UUIDs. Format of every other column is identical.

workos_organization_syncs:

 id |     workos_organization_id     |          last_event_id           |          created_at           |          updated_at
----+--------------------------------+----------------------------------+-------------------------------+-------------------------------
  1 | org_01KMD54G2B6NCWFCPSMK23NYGC | event_01KQEYMB4R8K0VX6M7CXB82NE7 | 2026-04-30 10:23:28.837205+00 | 2026-04-30 10:23:28.87477+00
 27 | org_01KMD54GVZ0JV6F9FRED9EZ4M0 | event_01KQEZS9Y1V7R08P223ZAW24TQ | 2026-04-30 10:30:00.776886+00 | 2026-04-30 10:43:47.095144+00
 69 | org_01KMK1435BT0KJC3P39FEQKF9K | event_01KQCZZEHZWY58XVT4N8J2SQZW | 2026-04-30 10:30:01.288056+00 | 2026-04-30 10:30:01.383115+00

workos_user_syncs (singleton):

 id |          last_event_id           |          created_at           |          updated_at
----+----------------------------------+-------------------------------+------------------------------
  1 | event_01KQF02TMBDVHY0WESGR3DPEAA | 2026-04-30 10:48:58.890745+00 | 2026-04-30 10:49:00.23692+00

global_roles:

                  id                  | workos_slug | workos_name |      workos_description      |     workos_created_at      | workos_deleted
--------------------------------------+-------------+-------------+------------------------------+----------------------------+----------------
 019ddddc-fa21-7297-af91-1bf9c2b6baea | member      | Member      | The default user role        | 2024-08-15 21:49:46.159+00 | f
 019ddddc-fa23-7995-9fd3-3a85d1f4fb05 | admin       | Admin       | The default super-admin role | 2026-03-24 15:24:49.149+00 | f

organization_roles:

                  id                  |           organization_id            |              workos_slug              |            workos_name            |                        workos_description                        |     workos_created_at      | workos_deleted
--------------------------------------+--------------------------------------+---------------------------------------+-----------------------------------+------------------------------------------------------------------+----------------------------+----------------
 019ddddc-fa25-7e39-b12a-b99ca05ab26a | 8f782a9f-2bbb-e641-80eb-6cf47c3ad3ac | org-api-key-role-test-20260401-181104 | API key role test 20260401-181104 | Fresh role created via API key                                   | 2026-04-01 17:11:04.449+00 | f
 019ddddc-fa27-7e3a-a7d0-f5c04c991ad1 | 8f782a9f-2bbb-e641-80eb-6cf47c3ad3ac | org-local-assign-test-20260402-1622   | Local Assign Test 20260402-1622   | Created to test assigning an existing local user to a fresh role | 2026-04-02 16:22:54.767+00 | f
 019ddddc-fa27-7048-8678-dceba84bbe29 | 8f782a9f-2bbb-e641-80eb-6cf47c3ad3ac | org-mcp-user                          | MCP User                          | An MCP user for ecommerce                                        | 2026-04-15 14:38:36.026+00 | f

organization_role_assignments:

                  id                  |           organization_id            |         workos_user_id          | user_id |                     role_urn                     |     workos_membership_id      |     workos_updated_at
--------------------------------------+--------------------------------------+---------------------------------+---------+--------------------------------------------------+-------------------------------+----------------------------
 019ddddc-fa2e-7027-931b-ed05fdd16759 | 8f782a9f-2bbb-e641-80eb-6cf47c3ad3ac | user_01KP5NTRW7XYKNRY17T8EWBJTG |         | role:global:019ddddc-fa21-7297-af91-1bf9c2b6baea | om_01KP5NWV0J33N8NXD86A4KRRGR | 2026-04-14 09:41:54.299+00
 019ddddc-fb7e-77f6-a162-3ddeadb39e18 | b90250a5-5be7-256d-2b45-cea2a855ed74 | user_01JTT0NGEGW48N4QMHGJ6YR512 |         | role:global:019ddddc-fa21-7297-af91-1bf9c2b6baea | om_01KPE0AZ4EDRSP1A7BE68PEB8B | 2026-04-17 15:18:18.502+00
 019ddddc-fb81-7dd0-8062-863ed5798559 | b90250a5-5be7-256d-2b45-cea2a855ed74 | user_01KPAZFT0HESVQ7Z9AFNQZ1SFX |         | role:global:019ddddc-fa21-7297-af91-1bf9c2b6baea | om_01KPAZFVQZ428ETN3SXQW0E2E6 | 2026-04-16 11:05:46.999+00

(user_id empty because membership events have so far arrived for users who are not yet in the local users table. Linked on first login or backfill.)

Follow-ups (subsequent PRs)

  • PR 1 — utilities (conv timestamptz helpers, attr keys, accesstest helper)
  • PR 2 — WorkOS client extensions + sqlc queries
  • PR 3 — pure event handlers + tests
  • PR 4 — Temporal activities
  • PR 5 — workflows + worker wiring (schedules off-by-default)
  • PR 6 — /rpc/external.receiveWorkOSWebhook endpoint

@tgmendes tgmendes requested a review from a team as a code owner April 30, 2026 11:20
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 30, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
gram-docs-redirect Ready Ready Preview, Comment Apr 30, 2026 11:20am

Request Review

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 30, 2026

⚠️ No Changeset found

Latest commit: 40b40ba

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions
Copy link
Copy Markdown
Contributor

atlas migrate lint on server/migrations

Status Step Result
1 new migration file detected 20260430111449_add-workos-roles-and-sync-tables.sql
ERD and visual diff generated View Visualization
No issues found View Report
Read the full linting report on Atlas Cloud

@github-actions
Copy link
Copy Markdown
Contributor

atlas migrate lint on server/clickhouse/migrations

Status Step Result
No migration files detected  
ERD and visual diff generated View Visualization
No issues found View Report
Read the full linting report on Atlas Cloud

@tgmendes tgmendes changed the title chore: add workos roles and sync tables migration mig: add workos roles and sync tables migration Apr 30, 2026
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 2 potential issues.

View 2 additional findings in Devin Review.

Open in Devin Review

Comment thread server/database/schema.sql
Comment thread server/database/schema.sql
@tgmendes tgmendes merged commit 5f8641c into main Apr 30, 2026
40 checks passed
@tgmendes tgmendes deleted the workos-sync-pr0-migrations branch April 30, 2026 12:06
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants