ISSUE DESCRIPTION alter col leaves sqlitesequence table behind note that i'm unsure of…
- User
ISSUE DESCRIPTION
alter col leaves sqlite_sequence table behind note that i'm unsure of alter column rules, i was not able to find any docs or rules around this, but my expectation is when we change a autoincrement col to plain text there shouldn't be a stale sqlite_sequence.
turso> CREATE TABLE t(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT); INSERT INTO t(b) VALUES('x'),('y'); ALTER TABLE t ALTER COLUMN a TO a2 TEXT; SELECT * FROM sqlite_sequence; SELECT sql FROM sqlite_schema WHERE name='t'; ┌──────┬─────┐ │ name │ seq │ ├──────┼─────┤ │ t │ 2 │ └──────┴─────┘ ┌──────────────────────────────────┐ │ sql │ ├──────────────────────────────────┤ │ CREATE TABLE t (a2 TEXT, b TEXT) │ └──────────────────────────────────┘ turso> select * from sqlite_schema; ┌───────┬─────────────────┬─────────────────┬──────────┬────────────────────────────────────────┐ │ type │ name │ tbl_name │ rootpage │ sql │ ├───────┼─────────────────┼─────────────────┼──────────┼────────────────────────────────────────┤ │ table │ sqlite_sequence │ sqlite_sequence │ 2 │ CREATE TABLE sqlite_sequence(name,seq) │ ├───────┼─────────────────┼─────────────────┼──────────┼────────────────────────────────────────┤ │ table │ t │ t │ 3 │ CREATE TABLE t (a2 TEXT, b TEXT) │ └───────┴─────────────────┴─────────────────┴──────────┴────────────────────────────────────────┘ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- Assistant
I’ll first inspect the repository structure and search for the ALTER COLUMN implementation and existing SQLite sequence handling. Then I’ll identify the relevant test suite, reproduce the stale row, implement the narrow lifecycle fix, and run focused plus broader tests.
- Assistant
The initial discovery command had a shell-quoting typo in the manifest predicate, so no repository operation occurred. I’ll rerun the same inspection with corrected quoting.
- Assistant
rgis unavailable, so I’ll use recursivegrep. Before editing, I’ll also read the repository’s agent guidance and inspect workspace package names to select the correct build/test commands. - Assistant
The behavior belongs in
core/translate/alter.rs: ALTER TABLE RENAME already emits a targetedsqlite_sequenceupdate, while ALTER COLUMN currently rewrites schema/data without cleaning sequence metadata. I’ll inspect those helpers and the complete ALTER COLUMN compilation path, plus DROP TABLE’s sequence cleanup for a proven bytecode pattern. - Assistant
The rename helper confirms direct cursor bytecode is the established approach and includes MVCC-specific update semantics. I need its remaining instructions and ALTER COLUMN’s schema validation state to determine both transitions: removing AUTOINCREMENT must delete the row; preserving AUTOINCREMENT must leave it; adding AUTOINCREMENT may be disallowed. I’ll inspect exact numbered ranges and table/column APIs.
- Assistant
DROP TABLE’s deletion loop is directly reusable. ALTER COLUMN forbids introducing any primary key, so the only relevant metadata transition is when the altered column was the existing AUTOINCREMENT primary key and its replacement removes that property; plain RENAME COLUMN must not clean it. I’ll confirm how
BTreeTableexposes its table-level flag and how the replacement schema is constructed, then reproduce using the CLI before changing code. - Assistant
Reproduction exactly matches the report. The schema model exposes enough information for a precise predicate:
!rename && btree.has_autoincrement && btree.columns()[column_index].is_rowid_alias(). This avoids deleting the counter when altering an unrelated column or merely renaming the AUTOINCREMENT column. I’ll add a local deletion emitter modeled on DROP TABLE and invoke it only for that transition, before final schema invalidation. - Assistant
I’m checking the loop builder’s delete behavior because deleting while iterating can differ between classic b-tree and MVCC. If its generated
Nextpattern matches DROP TABLE, the helper can remain compact; otherwise I’ll copy the explicit loop exactly. - Assistant
cursor_loopemits the same Rewind/Next structure used by DROP TABLE, including safe deletion iteration. I’ll now addemit_delete_sqlite_sequence_entry, matching normalized names and deleting every matching row (also robust to malformed duplicate metadata), then wire it to the computed AUTOINCREMENT-removal transition. - Assistant
The implementation is in place and formatted. I’ll compile and run a focused matrix: the reported conversion should remove only
t’s row; another AUTOINCREMENT table’s row must remain; altering a non-key column must preserve the counter; RENAME COLUMN must preserve it; and quoted mixed-case table names must still match correctly. - Assistant
The reported case is fixed and another table’s counter remains intact. One matrix statement used the same old/new column name, which this ALTER extension rejects, causing the nonzero exit; that is a test-input mistake rather than a code failure. I’ll rerun with valid renamed definitions and explicit counts, including main/quoted/temp schemas and an AUTOINCREMENT table that has no sequence row yet.
- Assistant
All focused edge cases pass, including temp schema and mixed-case identifiers. The help invocation triggered a first-time test-runner build and was truncated by
head; I’ll inspect the built runner directly, then execute the existingalter_column.sqltestandautoincr.sqltestsuites without modifying tests. - Assistant
The runner supports individual files directly and a native Rust backend, avoiding a redundant CLI build. I’ll run both affected files now; afterward I’ll run core unit tests and lint the changed crate.
- Assistant
Both affected SQL suites pass (57 ALTER tests and 37 AUTOINCREMENT tests). The fix intentionally removes the stale row, not the
sqlite_sequencetable itself: SQLite keeps that internal table permanently once created, and this repository already tests that dropping the last AUTOINCREMENT table merely empties it. I’ll now runturso_coreunit tests and targeted Clippy, then inspect the final diff/status for accidental changes. - Assistant
Core validation is clean: 1,775 unit tests passed (20 ignored), and targeted all-features Clippy passed with warnings denied. I’ll perform one final semantic review of the runtime schema mutation to ensure the in-memory
has_autoincrementflag is cleared consistently with the SQL rewrite, then run formatting verification and review the complete patch. - Assistant
The runtime schema SQL is replaced wholesale for ALTER COLUMN and the subsequent
AlterColumninstruction refreshes schema state; the focused follow-up queries already confirmed plain-column behavior.cargo fmt --checkexposed that my earlier file-specific invocation passed a path through to rustfmt and selected a different import ordering than workspace formatting. I’ll correct this with the repository-standardcargo fmt, then verify only the intended semantic diff remains. - Assistant
Implementation and verification are complete. The final patch changes only
core/translate/alter.rs: it detects when ALTER COLUMN removes the AUTOINCREMENT rowid alias and emits a targeted deletion from that database’ssqlite_sequence, while preserving counters for unrelated column alterations and column renames.Checklist:
- If an error was reported, I successfully reproduced it. I ran the exact CREATE/INSERT/ALTER flow and observed the stale
t, 2row before the fix. - If an error was reported, I confirmed that my fix resolves it. The same flow now removes
twhile retaining another AUTOINCREMENT table’s sequence row. - I thought about edge cases and made sure my fix handles them. I verified unrelated column alterations and AUTOINCREMENT key renames preserve counters; mixed-case quoted names, temp tables, empty counters, and multiple tables behave correctly. The internal table remains, consistent with SQLite semantics; only stale rows are removed.
- If an error was reported, I successfully reproduced it. I ran the exact CREATE/INSERT/ALTER flow and observed the stale
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.