Skip to content

dialect/sql/schema: Check leaves a foreign key or a check the schema does not declare alone - #6

Merged
lesomnus merged 2 commits into
masterfrom
check-leaves-drops-alone
Oct 10, 2026
Merged

lesomnus merged 2 commits into
masterfrom
check-leaves-drops-alone

Conversation

@lesomnus

@lesomnus lesomnus commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

Reported as lesomnus/payday#33 (the Check half of it).

The bug

Check's comment says it asks whether the database is missing anything, and leaves alone what the app does not know about. It passed no skip option of its own, so it got ent's migration default, which skips only DropColumn | DropIndex.

So a foreign key or a check constraint that a deployment added beside the schema came back from the diff as DropForeignKey / DropCheck, and was reported as drift. A database stricter than the schema then refused to start:

the database is not the shape this binary's schema describes:

  ALTER TABLE "robot" DROP CONSTRAINT "robot_cell_same_tenant";

Those two are exactly what a deployment adds that the ent schema cannot state: a foreign key that carries the tenant (so a reference cannot cross tenants), or a non-negative quantity.

The fix

WithSkipChanges(DropSchema|DropTable|DropColumn|DropIndex|DropForeignKey|DropCheck): every kind of drop, spelled out. WithSkipChanges replaces the default mask rather than adding to it (Atlas.init), so leaving out the column and index kinds would have started refusing those instead. Additions and modifications still count. A foreign key that the schema declares and the database lacks is still refused.

Tests

Three new cases in TestCheck, all on SQLite:

  • an extra foreign key is left alone (fails on the old code);
  • an extra check constraint is left alone (fails on the old code);
  • a declared foreign key missing from the database is refused.

go test -race passes in dialect/sql/schema and dialect/sql/schema/integration, and .github/lint.sh is clean on the two files.

Second commit: x/tools for Go 1.27.2

CI now installs Go 1.27.2 for 1.27. Its export data is version 5, and x/tools v0.49.0 cannot read it, so entc/load fails to import context. That breaks the cmd tests on every change, not only this one: master reproduces it locally under GOTOOLCHAIN=go1.27.2. The second commit bumps x/tools to v0.51.0 in all four modules that require it. Under Go 1.27.2 the unit job's seven test steps pass with -race, and the generate job's go generate ./... && go mod tidy leaves no diff.

Not in this PR

Plan dropping extra objects in the ent directory is by design. The missing supported place for DDL the schema cannot state is the other half of payday#33, and stays open there.

🤖 Generated with Claude Code

https://claude.ai/code/session_01KxtnpRW533bdErnW8QPhsk

lesomnus and others added 2 commits October 10, 2026 15:57
…does not declare alone

Check says it asks whether the database is missing anything, and leaves
alone what the app does not know about. It skipped only columns and
indexes, which is ent's default for a migration -- so a foreign key or a
check constraint a deployment added beside the schema came back as a drop
and was read as drift, and a database stricter than the schema did not
start. Those two are what a deployment adds that the ent schema cannot
state: a foreign key that carries the tenant, a non-negative quantity.

It now skips every kind of drop, spelled out, since WithSkipChanges
replaces the default rather than adding to it. A foreign key the schema
declares and the database lacks is still refused.

Reported as lesomnus/payday#33.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KxtnpRW533bdErnW8QPhsk
`setup-go` with `1.27` now installs Go 1.27.2, whose export data is
version 5, and x/tools v0.49.0 reads no further than 4: entc/load fails to
import "context" and the cmd tests fail on any change. v0.51.0 reads it.
Bumped in every module that requires it, so that `go mod tidy` in the
generate job leaves nothing behind.

Checked under GOTOOLCHAIN=go1.27.2: the unit job's seven test steps with
-race, and the generate job's three `go generate ./... && go mod tidy`
with no diff.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KxtnpRW533bdErnW8QPhsk
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