Skip to content

schemadiff: render a sequence default that is not serial as the server prints it - #655

Open
aparajon wants to merge 4 commits into
mainfrom
armand/render-live-sequence-defaults
Open

aparajon wants to merge 4 commits into
mainfrom
armand/render-live-sequence-defaults

Conversation

@aparajon

Copy link
Copy Markdown
Collaborator

Why

Render refused every sequence-backed default except an owned serial whose sequence is named <table>_<column>_seq. Two common shapes fail that test, and neither table could be pulled:

  • A table renamed after it was created. PostgreSQL does not rename the sequence with the table, so purchases.id keeps nextval('orders_old_id_seq'::regclass).
  • A sequence that several tables share.

A consumer that adopts a database from pull then has no file for the table.

What

  • A sequence-backed default that is not the serial form now renders as the server prints it, for example "id" bigint DEFAULT nextval('orders_old_id_seq'::regclass) NOT NULL. It is never rendered as serial, which would create a sequence the column does not have.
  • A serial whose owned sequence is declared away from the defaults is still refused with ErrUnrenderableSequenceOptions, and so is a non-serial owned sequence declared that way.
  • ErrUnrenderableDefault is deprecated, because Render no longer returns it.
  • docs/pull.md describes the export.

Before and after

orders_old (id bigserial PRIMARY KEY) was renamed to purchases, so its default still reads nextval('orders_old_id_seq'::regclass).

Before                                   After

┌───────────────────────────────┐        ┌───────────────────────────────┐
│ Render purchases              │        │ Render purchases              │
└───────────────┬───────────────┘        └───────────────┬───────────────┘
                │                                        │
                ▼                                        ▼
┌───────────────────────────────┐        ┌───────────────────────────────┐
│ ✗ sequence-backed default     │        │ "id" bigint DEFAULT nextval(  │
│ cannot be rendered            │        │ 'orders_old_id_seq'::regclass)│
│ no file for the table         │        │ NOT NULL                      │
└───────────────────────────────┘        └───────────────┬───────────────┘
                                                         │
                                                         ▼
                                         ┌───────────────────────────────┐
                                         │ re-diff the file: no changes  │
                                         └───────────────────────────────┘

How

A desired build already resolves an unqualified nextval('<sequence>') to the live sequence through the scratch search path. The scratch table's default then prints exactly as the live one does, so the diff finds no change. renderColumnDef now falls through to the plain column definition for a sequence default that serialType does not accept, as long as no sequence declaration would be lost. The new integration test covers a shared sequence with a serial-style name, a renamed serial table, and a true serial. Each one round-trips to no change, and planning leaves the live schema's sequences as they were.

Risk

The rendered file names a live sequence it does not create. A file that names a sequence that does not exist still fails its build, as before. The file does not record that a renamed table owns its sequence. The diff does not compare ownership, so that loss changes no plan.

Upgrade notes

ErrUnrenderableDefault is deprecated. Render no longer returns it, so a caller that maps it can drop the case.


🤖 Generated with Claude Code

…r prints it

Render refused every sequence-backed default other than an owned serial named <table>_<column>_seq, so a table renamed after it was created, or one on a shared sequence, could not be pulled. Such a default now renders as nextval of the live sequence. The desired build resolves it to that sequence, so the file re-diffs to no change and planning creates no sequence. ErrUnrenderableDefault is deprecated; Render no longer returns it.

Signed-off-by: Armand Parajon <armand@squareup.com>
Signed-off-by: Armand Parajon <armand@squareup.com>
@aparajon
aparajon marked this pull request as ready for review November 30, 2026 11:09
@Kiran01bm

Copy link
Copy Markdown
Collaborator

🤖 Review findings - created by Kiran's code review agent - for pg-sprite/pull/655, 43dcb69.

Verdict: 3 findings — 2 non-blocking (stale limitations row, untested row-security admission), 1 suggestion; approve.

Non-blocking

1. limitations.md still says this default is refused. The "Non-table objects" row says a hand-written nextval default on a standalone sequence with the serial-style name "refuses rather than exporting as serial". With this change it exports as the server prints it, which pull.md now says. AGENTS.md's capability-statement rule asks for the limitations entry to change in the same PR: docs/limitations.md:408.

2. A policy-only row-security apply is now admitted on a table like this, and no test covers it. rowSecurityTableShape gates the table by calling Render, so a table whose default uses a shared sequence, or one renamed after it was created, now passes where it used to be refused. The CLI's matching reason was removed with it. No row-security test has such a table, so the newly reachable executor path has no test, which the coverage rule asks for: row_security.go:290-300.

General suggestions

3. renderRefusals still lists ErrUnrenderableDefault, which Render no longer returns. Keeping the sentinel exported for errors.Is callers is fine. But the list's comment says it holds "every verdict the renderer refuses a table with", and that is no longer true. Consider dropping it from the list and from the matching refusal test: render.go:91-94.

The one thing that could have broken, verified

Whether a rendered nextval default could make a plan create a sequence, or point the column at a different one. The diff still refuses to add a column with a sequence default, or to change a column to one (CauseSerialDefault). So such a default only plans when the file matches the live default exactly, and then it plans nothing: diff.go:692-694, diff.go:909-911. The new integration test checks both outcomes. A shared sequence, and the sequence of a table renamed after it was created, each re-diff to no changes, and the schema's list of sequences is the same afterwards.

Verified correct

  • An owned, NOT NULL column with the serial-style name still renders as serial, and a serial with declared sequence options still refuses.
  • A sequence default that isn't a serial but has declared sequence options still refuses, with ErrUnrenderableSequenceOptions.
  • When rowSecurityShapeReason gets a verdict it has no reason for, it prints the verdict's own text, so removing the case can't print anything false.
  • The rendered file still passes statement.ParseDesired before Render returns it.
  • CI: 18/18 checks green at 43dcb69.

This review was generated by Claude Code (claude-opus-5-5).

@Kiran01bm Kiran01bm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 Approved on Kiran's (@kmuddukrishna) behalf by the scheduled review agent — no blocking findings at 43dcb69. See the review comment above; non-blocking findings and suggestions, if any, are not merge gates.

ErrUnrenderableDefault stays exported for callers that match it, but
Render no longer returns it, so it leaves the refusal list, and the
completeness test skips a verdict marked Deprecated. limitations.md
now says such a default exports as the server prints it, and a test
runs a policy change on a table whose default draws from a sequence it
does not own.

Signed-off-by: Armand Parajon <armand@squareup.com>
@aparajon

Copy link
Copy Markdown
Collaborator Author

🤖 All three findings are fixed in acac481:

  1. limitations.md. The "Non-table objects" row now says a nextval default on a standalone sequence with the serial-style name exports as the server prints it, naming the live sequence, rather than as serial.
  2. Row security admission. TestExecuteRowSecurityAcceptsADefaultOnASequenceItDoesNotOwn runs a policy-only change on a table whose default uses a shared sequence, and on a serial table renamed after it was created. Each applies the new policy and leaves the schema's sequences as they were.
  3. renderRefusals. ErrUnrenderableDefault is out of the list and the completeness test's table. The test now skips a verdict marked Deprecated:, so the list again holds only verdicts Render returns.

🤖 Addressed by Claude Code

Signed-off-by: Armand Parajon <armand@squareup.com>
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.

2 participants