Skip to content

feat: keep Coder Agents tables in a chatd schema - #1

Merged
ibetitsmike merged 3 commits into
mainfrom
mike/fork-chatd-schema
Nov 7, 2026
Merged

ibetitsmike merged 3 commits into
mainfrom
mike/fork-chatd-schema

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 7, 2026 •

Copy link
Copy Markdown
Owner

Xum opened this PR for Mike.

Keeps the Coder Agents tables in their own chatd Postgres schema, with fork migrations named by UUID and ordered by their parents. Upstream's numbered migrations still run in public, so main can merge upstream every day. Design, sync flow and how to add a fork migration: coderd/database/migrations/chatdmig/README.md.

Changes

  • coderd/database/migrations/chatdmig: the graph (routes.txt, nodes/, override/), the runner and its ledger chatd.migration_nodes, the route classifier, and the chatdmig tool (route, new, check).
  • The adopt node moves the chat tables, views, sequences, enums and functions from public to chatd with SET SCHEMA. No row is copied.
  • coder server migrates with migrations.ServerMigrate: core moves one version at a time, and every ready node runs before each step, under one advisory lock. EnsureCleanAll also checks the graph.
  • Every connection puts chatd on the search path (WithChatdSearchPath). Upstream files change in 7 lines: 3 in cli/server.go, 3 in dbtestutil/postgres.go and 1 in its test.
  • .github/workflows/fork-ci.yaml (graph, schema, chat-tests, build) and fork-sync.yaml (daily upstream merge through a self-merging PR; disables upstream workflows).

Migration locking

  • Each core version and each node commits on its own. A lock lasts for one migration, not the whole upgrade, and a restart continues where it stopped. A failed upgrade can stop between versions, so a rollback restores the deploy's snapshot, as coder-local rollback does.
  • Every migration transaction, core or node, sets lock_timeout (5 s) and retries after a lock timeout or deadlock, up to 10 attempts. A migration that waits for a long transaction no longer stalls every query on the table.
  • Fork node options: -- phase: background runs after the server starts, -- repeat: until-no-rows runs a bounded batch per transaction until it changes nothing, and -- transaction: none runs one statement outside a transaction for CREATE INDEX CONCURRENTLY, with a check for the invalid index that a failed build leaves.

Validation

  • go test ./coderd/database/migrations/chatdmig/ with Postgres: the routed graph gives upstream's schema apart from the schema name, and an upgrade from 634 or from the split equals a fresh database.
  • Lock tests on a real database: a held lock makes a core step and a routed node give up cleanly (core version kept, not dirty); while a migration retries, queries on the locked table finish; after the lock goes, the upgrade completes. Run-mode tests cover the background backfill (3 batches, 3 transactions), the concurrent index, and the invalid-index stop. Removing each mechanism makes its test fail.
  • Five simulated upstream migrations (a users column, a chats column and index, a new chat table, one that touches both, and one that creates tables in both schemas) route as core, chatd, chatd, core and mixed. With an override for the mixed one, every chatdmig test passes and each object lands in the expected schema.
  • Upstream suites on Linux with Postgres 17: ./coderd/database/... and ./coderd/x/... pass, apart from TestPubsub_Disconnect, which needs the Docker socket. In ./cli, all 57 subtests of TestServer and TestConnectToPostgres pass (built-in Postgres skipped, since the container runs as root). The package then fails a goleak check on HTTP/2 client goroutines; base b798061108 fails the same way. The 242 chat, skill and MCP tests of ./coderd and ./enterprise/coderd passed on the first commit.
  • A copy of the live database (migration 634, 20,592 chat messages): UpAll took 54 ms, kept every row, and its schema dump equals a fresh UpAll.
  • actionlint and shellcheck are clean.

Before merge

  1. Add the FORK_SYNC_TOKEN secret.
  2. fork-ci must pass. The main ruleset requires its four checks.

Merging deploys nothing until the autodeploy LaunchAgent is loaded.

Fork migrations are nodes named by UUID with explicit parents. UpAll runs
upstream's numbered migrations one version at a time and applies every
ready node before each step. The adopt node moves the Coder Agents tables,
views, sequences, enums and functions from public to chatd. Every
connection adds chatd to the search path.

Adds fork-ci and fork-sync workflows. See
coderd/database/migrations/chatdmig/README.md.
Every migration transaction, core or chatd node, sets lock_timeout (5s by
default) and retries after a lock timeout or deadlock, so a migration that
waits for a long transaction no longer stalls every query on the table.
UpAll commits each core version and node on its own, on purpose now: locks
last for one migration, and a restart continues where it stopped.

Fork nodes get three header options: "-- phase: background" runs a node
after the server starts, "-- repeat: until-no-rows" runs a bounded batch in
its own transaction until it changes nothing, and "-- transaction: none"
runs one statement outside a transaction for CREATE INDEX CONCURRENTLY,
with a check for invalid indexes. coder server calls ServerMigrate, which
starts the background runner after the startup migrations.

fork-ci starts Postgres with max_connections=1000 and accepts a pg_dump
newer than 16.
TestRoutingMatchesUpstream routes every migration after 634 again. Without
the override files, a synced mixed migration fails the schema job even after
its override is written.
@ibetitsmike
ibetitsmike merged commit d2ea65d into main Nov 7, 2026
4 checks passed
@ibetitsmike
ibetitsmike deleted the mike/fork-chatd-schema branch November 7, 2026 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant