Skip to content

feat(postgres): allow migration-managed schemas - #913

Merged
bensabic merged 4 commits into
mainfrom
feat/postgres-schema-management
Sep 9, 2026
Merged

bensabic merged 4 commits into
mainfrom
feat/postgres-schema-management

Conversation

@bensabic

@bensabic bensabic commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

Adds autoCreateSchema: false so applications can manage PostgreSQL tables and indexes through migrations and run the bot with restricted database permissions.

createPostgresState({
  url: process.env.POSTGRES_URL,
  autoCreateSchema: false,
});

Automatic schema creation remains enabled by default. The docs include the migration SQL and required grants. The expired-cache behavior reported in the issue is already fixed in the base branch.

Closes #722

Signed-off-by: Ben Sabic <bensabic@users.noreply.github.com>
@bensabic
bensabic requested a review from a team as a code owner September 8, 2026 05:22
@vercel

vercel Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

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

Project Deployment Actions Updated
chat Ready Ready Preview, v0 Sep 9, 2026 5:13pm UTC
chat-sdk-nextjs-chat Ready Ready Preview, v0 Sep 9, 2026 5:13pm UTC

- Probe tables, privileges, and sequence usage when autoCreateSchema is
  false, so connect() rejects with a descriptive error instead of the
  first message failing
- Export postgresSchemaStatements as the single source of the adapter
  DDL, used by ensureSchema(), the tests, and migration tooling
- Compare the README and docs migration SQL to postgresSchemaStatements
  in the unit tests
- Run the integration suite as a non-superuser admin by granting
  membership in the runtime role, and pin search_path on the admin pool
- Declare POSTGRES_TEST_URL in turbo.json globalEnv
- List autoCreateSchema in the chat/adapters catalog entry
- Document ALTER ROLE ... SET search_path as the pooler-safe way to
  target a dedicated schema, and the postgresSchemaStatements export
- Update state-pg AGENTS.md test prerequisites and schema release rules

Signed-off-by: Ben Sabic <bensabic@users.noreply.github.com>
- Check SELECT, INSERT, and DELETE on every table, and UPDATE only on
  locks, cache, and lists, since subscriptions and queues are never
  updated
- Accept nextval through either USAGE or UPDATE on the list and queue
  sequences, and skip the sequence check for identity columns
- Run the integration suite under least-privilege grants and add an
  identity-column schema test
- Assert the probe's per-table privileges in the unit tests
- Describe the exact privileges connect() verifies in the README, docs,
  and AGENTS.md

Signed-off-by: Ben Sabic <bensabic@users.noreply.github.com>
@bensabic
bensabic merged commit ea025af into main Sep 9, 2026
19 checks passed
@bensabic
bensabic deleted the feat/postgres-schema-management branch September 9, 2026 17:18
patrick-chinchill added a commit to Chinchill-AI/chat-sdk-python that referenced this pull request Sep 30, 2026
…ion-owned schemas (#240)

PostgresStateAdapter.set_if_not_exists now uses upstream's conditional upsert (ON CONFLICT DO UPDATE ... WHERE expires_at <= now() RETURNING), so expired dedupe/lease rows are reclaimed atomically; live and permanent rows are never overwritten.
Ports upstream d88789c9 (vercel/chat#636, chat@4.35.0) and ea025af7 (vercel/chat#913, chat@4.41.0).
New opt-in auto_create_schema=False (keyword-only; default True) skips DDL and runs a read-only table/privilege/sequence probe, raising chat_sdk.StateSchemaError on a missing table or grant. New exports: POSTGRES_SCHEMA_STATEMENTS, StateSchemaError; README documents the migration SQL.
Divergences (docs/UPSTREAM_SYNC.md): StateSchemaError(ChatError) instead of plain Error; connect() serialized on an asyncio.Lock (queued callers retry); adapter-owned asyncpg pool closed on a failed connect() to avoid leaking 10 connections per retry.
Consumer impact: Postgres-backed dedupe and leases recover after expiry without cleanup; no schema change; defaults unchanged.
Tests: mock pool no longer reclaims under DO NOTHING; injectable mock clock; opt-in live suite via POSTGRES_TEST_URL.
Closes #240

This branch was successfully deployed

2 active deployments
Preview – chat — 836a0583 Deployed Sep 9, 2026 by vercel[bot]
Preview – chat-sdk-nextjs-chat — 836a0583 Deployed Sep 9, 2026 by vercel[bot]
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.

state-pg: support migration-owned schema and renew expired setIfNotExists entries

2 participants