Skip to content

fix(db): track ALTER TABLE … RENAME TO so column-collision detection survives a rebuild - #9841

Merged
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
shin-core:fix/migration-rename-table-collision-9647
Jul 29, 2026
Merged

fix(db): track ALTER TABLE … RENAME TO so column-collision detection survives a rebuild#9841
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
shin-core:fix/migration-rename-table-collision-9647

Conversation

@shin-core

Copy link
Copy Markdown
Contributor

What & why

detectColumnCollisions (src/db/migration-column-extraction.ts) replays every migration's schema events in order to catch a same-table/same-column definition split across two individually-valid migrations. But extractSchemaEvents emitted nothing for ALTER TABLE <old> RENAME TO <new> — the standard SQLite rebuild-and-rename (migrations/0201_ledger_anchor_bittensor.sql does exactly this, since SQLite can't ALTER a CHECK constraint).

So every column tracked under the temporary _new table stayed keyed to it, and the detector went blind on the real table: a later duplicate ADD COLUMN read as a fresh, non-colliding definition — the very class of collision this check exists to catch.

The fix

  • Add a { type: "rename_table"; from; to } SchemaEvent variant.
  • Parse ALTER TABLE <old> RENAME TO <new> into it — matched before the column-rename rule and requiring RENAME immediately followed by TO. The column form (RENAME [COLUMN] <a> TO <b>, an identifier between RENAME and TO) never matches that, and the table form never matches the column rule, so the two are disambiguated in both directions.
  • detectColumnCollisions handles rename_table by re-keying every column tracked under the old table name to the new one — and vacating the old name, so a fresh table reusing it doesn't falsely collide.

Tests (test/unit/migration-column-extraction.test.ts)

  • extractSchemaEvents returns a single rename_table for 0201's exact statement; the two column-rename forms still produce remove_column + define_column (disambiguation proven) — fails on main.
  • Regression: CREATE t(backend) → rebuild-and-rename t_new → tALTER t ADD COLUMN backend yields exactly one collision on t.backend (fails on main, which sees none).
  • A fresh table reusing the vacated old name does not falsely collide.
  • The existing "zero false positives against the real migrations/ directory" invariant still passes (0201 no longer confuses it, and nothing new is flagged).

Validation

  • npm run typecheck green; the extraction suite (39 tests) green.
  • Diff coverage on the changed file is 100% line and branch.
  • git diff --check <base> HEAD clean; no migration or schema change.

Closes #9647

…survives a rebuild

`detectColumnCollisions` replays every migration's schema events to catch a
same-table/same-column definition across two files. But `extractSchemaEvents`
emitted nothing for `ALTER TABLE <old> RENAME TO <new>` — the standard SQLite
rebuild-and-rename (migrations/0201). So every column tracked under the temporary
`_new` table stayed keyed to it, and the detector went blind on the real table:
a later duplicate ADD COLUMN read as a fresh, non-colliding definition.

Add a `rename_table` SchemaEvent and parse the RENAME TO form — matched before the
column-rename rule and requiring `RENAME` immediately followed by `TO`, which the
column form (an identifier between RENAME and TO) never has, so the two are
disambiguated both directions. `detectColumnCollisions` handles it by re-keying
every column tracked under the old table name to the new one (and vacating the old
name, so a fresh table reusing it does not falsely collide).

Closes JSONbored#9647
@shin-core
shin-core requested a review from JSONbored as a code owner July 29, 2026 13:18
@loopover-orb

loopover-orb Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Warning

⏸️ LoopOver review result - manual review recommended

Review updated: 2026-07-29 13:24:53 UTC

2 files · 1 AI reviewer · no blockers · CI green · clean

⏸️ Suggested Action - Manual Review

Review summary
This adds a `rename_table` SchemaEvent for `ALTER TABLE <old> RENAME TO <new>` and re-keys the collision-tracking map on it, fixing the real bug: previously columns stayed keyed under the temporary `_new` table name after a rebuild-and-rename, so the same-column check went blind on the renamed table. The regex ordering (table-rename checked before column-rename, requiring `RENAME` immediately followed by `TO`) correctly disambiguates the two ALTER forms, and the tests exercise both the extraction and the detector's re-keying/vacating behavior with the real migration statement from 0201. The fix is at the correct layer (the schema-event source, not a downstream symptom patch) and the new tests genuinely fail on `main` per the description.

Nits — 3 non-blocking
  • The re-keying loop in `detectColumnCollisions` (migration-column-extraction.ts:232-238) does `[...tracked]` and iterates the whole map on every `rename_table` event; fine at this repo's migration-file scale but worth a comment if the corpus ever grows significantly.
  • No test covers a rename onto a name that already has *other* tracked columns from an unrelated table (i.e., partial overlap during re-keying) — low risk given the existing 'vacated old name' test already covers the adjacent case.
  • Consider adding one more regression case where the renamed-to table already existed with different columns before the rename, to confirm re-keying merges rather than overwrites entries at the new key.

Decision drivers

  • ✅ Code review — No blockers (1 reviewer)
  • ✅ Gate result — Passing (No configured blocker found.)
Context & advisory signals — never blocks the verdict
Signal Result Evidence
Linked issue ✅ Linked #9647
Related work ✅ No active overlap found No same-issue or scoped active PR overlap found.
Change scope ✅ 20/20 Low review scope from cached public metadata (1 linked issue).
Validation posture ❌ 5/25 Preflight is holding this PR: the review lane is unavailable, so it is not ready for automated review.
Contributor workload ✅ 10/10 Author activity: 71 registered-repo PR(s), 52 merged, 0 issue(s).
Contributor context ✅ Confirmed Gittensor contributor shin-core; Gittensor profile; 71 PR(s), 0 issue(s).
Improvement ✅ Minor risk: clean · value: minor · LLM: moderate
Linked issue satisfaction

Addressed
The diff adds the required rename_table SchemaEvent, disambiguates it from column-rename via regex ordering/anchoring, and re-keys tracked columns in detectColumnCollisions using the drop_table-style iterate-and-rewrite pattern rather than a parallel alias map, matching the required approach. Tests cover the exact 0201 statement, both column-rename forms, the three-file regression fixture expectin

Review context
  • Author: shin-core
  • Role context: outside_contributor
  • Public audience mode: oss maintainer
  • Lane context: Repository is registered but has no active allocation in the current snapshot.
  • Public profile languages: TypeScript, JavaScript, Solidity, Dart, Python, CSS, PHP, Rust
  • Official Gittensor activity: 71 PR(s), 0 issue(s).
  • PR-specific overlap: none found.
Contributor next steps
  • Start here: Await review-lane availability.
  • Then work through the remaining 1 step in the Signals table above.
Signal definitions
  • Related work = same linked issue, overlapping active PRs, or title/path similarity.
  • Change scope = cached public metadata such as size labels, draft state, and review-burden hints.
  • Validation posture = whether the PR provides enough public validation/test evidence for maintainer review.
  • Contributor workload = public contributor activity and cleanup pressure, not a repo-wide quality failure.
  • Contributor context = public GitHub/Gittensor identity context; non-Gittensor status is not a blocker.
🧪 Chat with LoopOver

Ask LoopOver a question about this PR directly in a comment — grounded only in the same cached, public-safe facts shown above, never a new claim.

  • @loopover ask &lt;question&gt; answers contribution-quality Q&A with source citations and freshness.
  • @loopover chat &lt;question&gt; answers in natural prose from cached decision-pack facts via local inference (maintainer/collaborator; read-only).
  • A plain-language @loopover mention with a real question is routed to the closest matching read-only command automatically — no exact syntax required.

Full command reference: https://loopover.ai/docs/loopover-commands

🧪 Experimental — new and may change.

🟩 Safe / merged · 🟦 Advisory · 🟨 Held for review · 🟥 Blocked / closed


💰 Earn for open-source contributions like this. Gittensor lets GitHub contributors earn for the work they already do — register to start earning →.

Checked by LoopOver, a quiet PR intelligence layer for OSS maintainers.

  • Re-run LoopOver review

@superagent-security

Copy link
Copy Markdown
Contributor

Superagent didn't find any vulnerabilities or security issues in this PR.

@loopover-orb loopover-orb Bot added the gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier. label Jul 29, 2026
@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.09%. Comparing base (2b2c98a) to head (6c5e00d).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9841      +/-   ##
==========================================
+ Coverage   79.04%   79.09%   +0.05%     
==========================================
  Files         281      282       +1     
  Lines       58409    58549     +140     
  Branches     6698     6737      +39     
==========================================
+ Hits        46171    46311     +140     
  Misses      11955    11955              
  Partials      283      283              
Flag Coverage Δ
backend 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/db/migration-column-extraction.ts 100.00% <100.00%> (ø)

@loopover-orb loopover-orb Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LoopOver approves — the gate is satisfied and CI is green.

@loopover-orb
loopover-orb Bot merged commit d574e9f into JSONbored:main Jul 29, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

db: migration column-collision detection goes blind on any table rebuilt with `ALTER TABLE …

1 participant