Skip to content

fix(ent): preserve CURRENT_TIMESTAMP default on last_accessed_at - #1196

Merged
kalbasit merged 2 commits into
mainfrom
fix-ent-preserve-current-timestamp-default
May 22, 2026
Merged

kalbasit merged 2 commits into
mainfrom
fix-ent-preserve-current-timestamp-default

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

Ent's field.Time("last_accessed_at").Default(time.Now) only
sets the value in Go code at create-builder time — it doesn't
emit a DB-level DEFAULT. The §8 ent_baseline bridge therefore
recreated narinfos.last_accessed_at and
nar_files.last_accessed_at as plain nullable columns, silently
dropping the DEFAULT CURRENT_TIMESTAMP that the dbmate-era
schema had.

Real consequence: any caller that INSERTs into narinfos /
nar_files via raw SQL (e.g. every sqlc-generated query in
flight during §11) now writes NULL into last_accessed_at,
breaking LRU eviction (which reads that column) and the
contract tests that assert
nims[0].LastAccessedAt.Time == nims[0].CreatedAt on insert.
The bug was masked while tests used dbmate (preserving the
default); the upcoming testhelper rewrite to migrate.Up exposed
it.

Fix:

  • Add entsql.Annotation{DefaultExpr: "CURRENT_TIMESTAMP"} to
    the last_accessed_at field of both NarInfo and NarFile
    Ent schemas. Ent now emits the DB-level default; Schema.Create
    and Atlas-generated migrations both pick it up.
  • Regenerate ent/migrate/schema.go via go generate ./ent.
  • Regenerate the bridge migration via go run ./cmd/generate-migrations -name ent_baseline, which replaces
    the three previously-committed bridge files with refreshed
    ones whose CREATE TABLE statements include
    last_accessed_at datetime NULL DEFAULT (CURRENT_TIMESTAMP)
    (and the dialect equivalents). atlas.sum is regenerated by
    the same tool.

No behaviour change to anything that was already going through
Ent's create builders (those still set time.Now in Go before
INSERT). Restores parity with the dbmate-era schema for the
raw-SQL callers that still exist during §11.

Verified by go test -race ./pkg/database/migrate/... and
./pkg/database/... against SQLite + Postgres + MySQL.

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working go Pull requests that update go code labels May 21, 2026
@kalbasit

kalbasit commented May 21, 2026 •

Copy link
Copy Markdown
Owner Author

This change is part of the following stack:

Change managed by git-spice.

This was referenced May 21, 2026
@kalbasit
kalbasit force-pushed the fix-ent-preserve-current-timestamp-default branch from ed01afa to aa2db41 Compare May 22, 2026 18:07
@coderabbitai

coderabbitai Bot commented May 22, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1a86617f-528f-400b-bfd8-0f03de945e70

📥 Commits

Reviewing files that changed from the base of the PR and between dcf22dc and 9da83df.

📒 Files selected for processing (1)
  • pkg/database/ent_schema_parity_test.go

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Ensure NAR file and NAR info access timestamps default to CURRENT_TIMESTAMP at the database level for consistent last-access tracking.
  • Migrations
    • Update schema migrations to apply the new defaults and adjust NAR-related indexes and constraints.
  • Tests
    • Normalize diff behavior for SQLite defaults and serialize schema creation in tests to prevent flaky diffs and race conditions.

Walkthrough

This PR adds SQL-level CURRENT_TIMESTAMP defaults for last_accessed_at on nar_files and narinfos, updates Ent schema annotations and generated schema, adjusts MySQL/Postgres/SQLite migrations, normalizes SQLite diff comparisons, and serializes test schema creation.

Changes

Ent Schema and Defaults

Layer / File(s) Summary
Ent schema annotations and generated schema
ent/schema/nar_file.go, ent/schema/narinfo.go, ent/migrate/schema.go
last_accessed_at fields add entsql.Annotation{DefaultExpr: "CURRENT_TIMESTAMP"} while keeping Go-side Default(time.Now). The generated ent/migrate/schema.go columns now include Default: schema.Expr("CURRENT_TIMESTAMP").
Database migrations for all dialects
migrations/mysql/20260521021523_ent_baseline.sql, migrations/postgres/20260521021523_ent_baseline.sql, migrations/sqlite/20260521021523_ent_baseline.sql
MySQL/SQLite migrations set last_accessed_at defaults to CURRENT_TIMESTAMP in rewritten tables and add related indexes; Postgres migration edits remove prior handling of dropping/resetting last_accessed_at defaults. Down steps revert other DDL changes accordingly.
SQLite schema equivalence test hook
migrations/equivalence_test.go
Import Atlas schema package, add stripParenWrappedExprDefaults diff hook to normalize SQLite's parenthesized default expressions, and refactor migrate option construction to conditionally apply diff hooks.
Serialize test schema creation
pkg/database/ent_schema_parity_test.go
Wrap m.Create(...) with database.SchemaCreateMu.Lock()/Unlock() to serialize schema creation in tests.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • kalbasit/ncps#1195: Related to serialization of Ent schema creation used by tests (mutex usage referenced).
  • kalbasit/ncps#1190: Modifies Ent schema for last_accessed_at on NAR tables at the same field level.
  • kalbasit/ncps#1187: Introduced TestSchemaEquivalence/drift-check logic in migrations/equivalence_test.go, related to the new SQLite diff-normalization hook.

🐰 Timestamps now flow with the current time,
Default exprs mark each access in flight,
SQL defaults bloom where columns align,
From schema to migrations, all shines bright,
Tests and hooks ensure the diffs are right.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: preserving the CURRENT_TIMESTAMP default on the last_accessed_at column across the schema.
Description check ✅ Passed The description is directly related to the changeset, explaining the problem, its consequences, and the solution implemented across the modified files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Comment @coderabbitai help to get the list of available commands and usage tips.

kalbasit added a commit that referenced this pull request May 22, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
Base automatically changed from refactor-database-share-schema-create-mutex to main May 22, 2026 19:02
Ent's `field.Time("last_accessed_at").Default(time.Now)` only
sets the value in Go code at create-builder time — it doesn't
emit a DB-level DEFAULT. The §8 ent_baseline bridge therefore
recreated `narinfos.last_accessed_at` and
`nar_files.last_accessed_at` as plain nullable columns, silently
dropping the `DEFAULT CURRENT_TIMESTAMP` that the dbmate-era
schema had.

Real consequence: any caller that INSERTs into narinfos /
nar_files via raw SQL (e.g. every sqlc-generated query in
flight during §11) now writes NULL into last_accessed_at,
breaking LRU eviction (which reads that column) and the
contract tests that assert
`nims[0].LastAccessedAt.Time == nims[0].CreatedAt` on insert.
The bug was masked while tests used dbmate (preserving the
default); the upcoming testhelper rewrite to migrate.Up exposed
it.

Fix:

- Add `entsql.Annotation{DefaultExpr: "CURRENT_TIMESTAMP"}` to
  the `last_accessed_at` field of both `NarInfo` and `NarFile`
  Ent schemas. Ent now emits the DB-level default; Schema.Create
  and Atlas-generated migrations both pick it up.
- Regenerate `ent/migrate/schema.go` via `go generate ./ent`.
- Regenerate the bridge migration via `go run
  ./cmd/generate-migrations -name ent_baseline`, which replaces
  the three previously-committed bridge files with refreshed
  ones whose CREATE TABLE statements include
  `last_accessed_at datetime NULL DEFAULT (CURRENT_TIMESTAMP)`
  (and the dialect equivalents). atlas.sum is regenerated by
  the same tool.
- Patch the SQLite case of TestSchemaEquivalence with a
  DiffHook that strips a single outer-paren wrapping from the
  desired-side RawExpr column defaults. Ent's atDefault always
  wraps DefaultExpr values in parens ("CURRENT_TIMESTAMP" ->
  "(CURRENT_TIMESTAMP)") but SQLite's schema introspector
  returns the unwrapped form, and Atlas's SQLite
  defaultChanged compares the two strings byte-for-byte
  without normalization — so every DefaultExpr column tripped
  a phantom ModifyTable diff on this dialect. Postgres/MySQL
  preserve the wrapped form during introspection and do not
  need the hook.

No behaviour change to anything that was already going through
Ent's create builders (those still set time.Now in Go before
INSERT). Restores parity with the dbmate-era schema for the
raw-SQL callers that still exist during §11.

Verified by `go test -race ./pkg/database/migrate/...` and
`./pkg/database/...` and `./migrations/...` against
SQLite + Postgres + MySQL.
@kalbasit
kalbasit force-pushed the fix-ent-preserve-current-timestamp-default branch from aa2db41 to dcf22dc Compare May 22, 2026 19:02
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
@kalbasit
kalbasit enabled auto-merge (squash) May 22, 2026 19:03
…ateMu

TestEntSchemaParity was calling m.Create(context.Background(),
migrate.Tables...) without holding database.SchemaCreateMu, while
other parallel tests in the same binary (e.g. freshSchemaSQLite in
client_test.go) correctly held the mutex.

Ent mutates the package-level migrate.Tables slice during Schema.Create,
so concurrent callers race on those shared *schema.Table objects. The
race detector caught this as a write/read conflict in
(*Atlas).setupTables vs (*Atlas).aIndexes.

Fix: acquire database.SchemaCreateMu around the m.Create call in
TestEntSchemaParity, consistent with every other caller in the process.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
@codecov

codecov Bot commented May 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 4.94%. Comparing base (cbd6db0) to head (9da83df).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #1196   +/-   ##
=====================================
  Coverage   4.94%   4.94%           
=====================================
  Files          6       6           
  Lines        445     445           
=====================================
  Hits          22      22           
  Misses       419     419           
  Partials       4       4           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kalbasit
kalbasit merged commit 57749da into main May 22, 2026
16 checks passed
@kalbasit
kalbasit deleted the fix-ent-preserve-current-timestamp-default branch May 22, 2026 20:51
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
kalbasit added a commit that referenced this pull request May 22, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
kalbasit added a commit that referenced this pull request May 23, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
kalbasit added a commit that referenced this pull request May 23, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
kalbasit added a commit that referenced this pull request May 23, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
kalbasit added a commit that referenced this pull request May 23, 2026
PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.
kalbasit added a commit that referenced this pull request May 23, 2026
fix(ncps): drop dbURL from createDatabaseQuerier error wraps

PR #1192 review: 3 sec-high comments flagged that
`createDatabaseQuerier`'s error wraps embed `dbURL` via `%q`.
Practical database URLs include credentials, leaking them into
server logs. Drop the URL from the three new wraps the §11.1
thread-through commit introduced.

Resolves:
- PR #1192 PRRT_kwDONV6tZs6DsTQ7 (line 990)
- PR #1192 PRRT_kwDONV6tZs6DsTRK (line 998)
- PR #1192 PRRT_kwDONV6tZs6DsTRQ (line 1003)

fix(cache): preserve tx debug logs + use time.Timer in retry

PR #1194 review surfaced two issues in withEntTransaction:

1. (PRRT_kwDONV6tZs6DsQzn) Restore "starting transaction" /
   "committed successfully" debug logs that the legacy
   executeTransaction emitted. Behind Debug level so noise
   stays opt-in; restores observability parity.

2. (PRRT_kwDONV6tZs6DsQzo) Replace `case <-time.After(delay):`
   inside the retry select with `time.NewTimer(delay)` +
   `t.Stop()` in the ctx-cancellation branch to avoid leaking
   the underlying timer when ctx wins the race.

Behaviour-preserving on the happy path.

fix(migrations): restore +goose NO TRANSACTION on sqlite bridge

PR #1196 (PRRT_kwDONV6tZs6DsQXZ, high): the
`-- +goose NO TRANSACTION` directive was dropped from the
SQLite ent_baseline bridge when `fix-ent-preserve-current-
timestamp-default` regenerated the migration via
`cmd/generate-migrations`.

In SQLite, `PRAGMA foreign_keys = off` is a no-op inside a
transaction. Goose wraps migrations in a transaction by
default, so without the NO TRANSACTION directive the PRAGMA
silently has no effect — and the subsequent
`DROP TABLE <parent>` / `ALTER TABLE new_X RENAME TO X` swaps
would fail in production against any database with existing
FK-referenced rows. The original §8 bridge had this directive;
the regeneration lost it because Atlas's GooseFormatter
doesn't emit it automatically.

Restored the directive plus its explanatory comment, and
regenerated `migrations/sqlite/atlas.sum` so Atlas's
checksum validator accepts the file.

Postgres and MySQL bridges don't need an equivalent — they
use real `ALTER TABLE` for column changes (not SQLite's
shadow-table-recreate pattern) and don't depend on a PRAGMA
to disable referential integrity mid-migration.

Note: `TestSchemaEquivalence/sqlite` is currently broken at
HEAD for a separate, pre-existing reason traced to the
`cf28126` regen (the test fails both with and without this
fix). Will be investigated as a follow-up.

fix(cache): add retry-on-deadlock to storeNarInfoInDatabase

PR #1203 (PRRT_kwDONV6tZs6DsScG, medium): standalone
`storeNarInfoInDatabase` (Cache-free entry used by
MigrateNarInfo and the `ncps migrate-narinfo` CLI) called
`dbClient.WithTransaction` directly, skipping the
deadlock-retry loop the `*Cache.withEntTransaction` provides.
Under load — concurrent migrations of many narinfos against
SQLite, or contended PostgreSQL deployments — that meant
transient SQLITE_BUSY / serialization failures bubbled all the
way out, where the legacy path would have retried.

Fix: extract the retry policy from `*Cache.withEntTransaction`
into a package-level `withEntTransactionRetry(ctx, dbClient,
operation, fn)` helper. `*Cache.withEntTransaction` now
forwards to it; `storeNarInfoInDatabase` uses it directly.
Same retry parameters (5 attempts, 50ms initial back-off
doubling per attempt, ctx-aware via time.Timer + Stop).

No behaviour change for any caller already going through
`*Cache.withEntTransaction`; the previously-missing retry
is now present for MigrateNarInfo / CLI migrations.

fix(cache): batch HashIn + null-safe SUM + unshadow ent

PR #1204 surfaced three concerns in §11.4's non-tx NarFile
conversion (`feat-cache-narfile-non-tx-to-ent`):

1. (PRRT_kwDONV6tZs6DsRgI, high) The CDC delayed-cleanup
   replacement for `GetOldCompressedNarFiles` was a two-step
   query that paged ALL chunked NAR hashes into Go memory then
   passed them to a single `entnarfile.HashIn(...)`. On caches
   with many chunked NARs that risks (a) loading megabytes of
   hashes into memory and (b) blowing past driver parameter
   limits (Postgres ≤ 65535, modern SQLite ≤ 32766, older
   SQLite ≤ 999). Fixed by batching HashIn in chunks of
   `cdcCleanupHashBatchSize` (= 500, a new package const).
   The batch size is well below all known limits and well
   above typical chunked-hash counts, so the job stays a
   single round-trip for any practical install.

2. (PRRT_kwDONV6tZs6DsRgL, medium) The metrics-observer SUM
   aggregator scanned into a plain `int64`. SQL `SUM` over an
   empty set returns NULL, which fails to scan into `int64`
   ("sql: Scan, storing driver.Value type <nil> into type
   *int64"). The metric callback would have started failing
   the moment any deployment booted with an empty nar_files
   table. Switched to `sql.NullInt64` with a Valid check;
   NULL is treated as zero. The `sql:"sum"` tag matches the
   alias Ent emits via `ent.Sum`.

3. (PRRT_kwDONV6tZs6DsRgO, medium) Local var `ent` shadowed
   the `ent` package import inside the cron handler closure.
   No actual breakage in the current code (no `ent.X` use
   inside that scope) but it makes the scope brittle for
   future edits. Renamed to `entClient`.

`go test -race ./pkg/cache/...` passes against SQLite +
Postgres + MySQL + Redis. Lint clean.

fix(cache): batch relinkNarInfosToNarFile bulk insert

PR #1205 (PRRT_kwDONV6tZs6DsRuq, medium):
`relinkNarInfosToNarFileWithEntTx` passed every queried narinfo
ID into a single `tx.NarInfoNarFile.CreateBulk(...)` call. Each
row sends 2 parameters (narinfo_id, nar_file_id), so a closure
with N narinfos sharing a NAR URL sends 2N placeholders. On
older SQLite builds (limit 999) that fails for any N > 499;
even on Postgres (limit 65535) very large closures could
silently overflow.

Fixed by chunking the loop into batches of 500 rows = 1000
placeholders per batch, well below all driver limits and well
above realistic closure sizes — so the common case stays a
single round-trip.

batch size is a local const inside the helper rather than a
package constant because the limit derives from this specific
row's placeholder count (2 cols × batch size); other bulk
inserts with different column counts get their own limit.

`go test -race ./pkg/cache/...` passes against SQLite +
Postgres + MySQL + Redis. Lint clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update go code merge-stack size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant