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.
sqlturso> 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 using command. Additionally, you can use and to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call ) using or commands. However you don't need to use or before submitting your changes. In fact, only submits currently unstaged changes because it uses to get the changes that need to be submitted. - Assistant
I’ll first map the repository and search for ALTER COLUMN and
sqlite_sequencehandling. Then I’ll identify the rewrite path, reproduce with the project’s test tooling, patch the lifecycle logic, and run focused plus relevant broader tests. - Assistant
rgis unavailable, so I’ll use recursivegrepand inspect repository guidance and likely SQLite parser/alter implementation locations. - Assistant
The implementation already has specialized sequence handling for table rename, while ALTER COLUMN’s table-rebuild path likely omits equivalent cleanup. I’ll inspect the helper bytecode and the ALTER COLUMN translation flow, along with DROP TABLE’s sequence deletion for a reusable pattern.
- Assistant
I need the exact instruction ordering and scope boundaries before modifying anything. I’ll read the complete helper, ALTER COLUMN’s schema-update/rewrite emission, and DROP TABLE translation around its cleanup logic.
- Assistant
The DROP snippet begins mid-block; I’ll capture the full condition and the remainder of ALTER emission. The key semantic question is whether cleanup should happen only when AUTOINCREMENT is removed (not on rename-only or an ALTER retaining AUTOINCREMENT), and whether introducing AUTOINCREMENT should initialize sequence state. The issue specifically requires removal, so I’ll derive old/new flags from parsed table metadata.
- Assistant
BTreeTableexposeshas_autoincrement, and ALTER COLUMN rejects adding a primary key, meaning a true ALTER of an AUTOINCREMENT primary-key column necessarily removes AUTOINCREMENT; rename-only preserves it. The robust condition is still old table has AUTOINCREMENT and the rewritten schema no longer does. I’ll inspect setup variables and schema mutation behavior to ensure the computed state aligns with the SQL rewrite. - Assistant
The cloned
btreeis not automatically recomputing table-level AUTOINCREMENT when replacing a column, so relying on it would be wrong. The operation’s semantics are simpler: onlyAlterColumn(notRenameColumn) can strip constraints, and if the original table has AUTOINCREMENT, the altered column must be its rowid-alias PK because SQLite permits AUTOINCREMENT only there. I’ll verify parser/schema representation and runtimeAlterColumnupdates the in-memory flag, then extract a shared sequence-row deletion helper rather than duplicate DROP’s loop. - Assistant
I’ll inspect the rest of
op_alter_column; if it leaveshas_autoincrementtrue, this bug includes an in-memory schema inconsistency beyond the stale row, potentially causing later inserts to keep updatingsqlite_sequence. The fix must update both persisted metadata behavior and runtime schema state. - Assistant
The runtime confirms a second defect:
op_alter_columnreplaces the column but never clearsbtree.has_autoincrement. I’ll reproduce the report now using the CLI, including a follow-up insert to expose any continued metadata effects. Then I’ll add a targeted deletion emitter conditioned on altering the actual rowid-alias AUTOINCREMENT column and clear the in-memory flag at the same semantic point. - Assistant
Reproduction matches the report and shows the stale in-memory flag causes the sequence to advance again after ALTER, so both fixes are required. I’ll add a deletion bytecode helper beside the rename helper, invoke it only when a non-rename ALTER targets the original AUTOINCREMENT rowid alias, and clear
has_autoincrementinop_alter_columnbefore replacing that column. - Assistant
The installed Rust 1.88 formatter rewrote large pre-existing sections due import/style-version differences, creating unrelated noise. I’ll revert these two files and reapply the same narrowly scoped edits without whole-file formatting; the inserted code already follows local formatting conventions.
- Assistant
The patch is now minimal. I’ll rerun the exact reproduction and then exercise edge cases: altering a different column must preserve the sequence and future increments; rename-column must preserve it; mixed-case/quoted table names must match normalized sequence names; and attached database cleanup must target the correct schema.
- Assistant
Main-schema behavior and preservation edge cases pass. ATTACH was disabled by default rather than failing the fix, so I’ll rerun that case with the required CLI flag. Then I’ll run the existing ALTER COLUMN SQL test suite and relevant Rust checks.
- Assistant
Attached-schema cleanup also passes. I’ll query the SQL test runner’s options to run only
alter_column.sqltest, avoiding an unnecessary full suite while still validating all existing ALTER behavior. - Assistant
I’ll run the complete existing ALTER COLUMN SQL test file using the native Rust backend with snapshot writes disabled, then run
cargo checkfor the affected core crate. - Assistant
All 57 existing ALTER COLUMN cases pass and the core crate type-checks. I’ll inspect
cursor_loopto verify deleting the current row cannot skip adjacent duplicate sequence entries (possible under MVCC history/legacy corruption), and run the AUTOINCREMENT SQL suite because this change touches sequence semantics. - Assistant
All 37 AUTOINCREMENT tests pass. The combined command’s
sedpath missed because the function is earlier in the file; I’ll inspect it now, then consider transaction rollback: sequence deletion and schema mutation should roll back together if row rewrite validation fails, and the runtime flag mutation only executes after all prior bytecode succeeds. - Assistant
cursor_loopusesNextafter deletion, matching the established DROP TABLE implementation. I’ll test atomicity explicitly by forcing ALTER validation to fail after sequence deletion; the original schema and sequence counter must both remain intact. I’ll also test multiple AUTOINCREMENT tables so only the target row is removed. - Assistant
My first failure predicate was not guaranteed false under SQLite’s dynamic type comparison and the ALTER correctly succeeded, so it did not test rollback. I’ll rerun with
CHECK(length(plain)=0), which is deterministically false for existing rowids"1"and"2". - Assistant
The ALTER CHECK validation currently accepts these expressions in this path, so this did not produce a rollback scenario; that is unrelated existing behavior and I won’t broaden scope. The normal and preservation cases already establish the fix. I’ll add concise comments documenting why cleanup and in-memory flag clearing are paired, then run core Clippy and focused core tests.
- Assistant
Clippy passes with warnings denied. The focused name filter found no unit tests (all 1,795 were filtered), so I’ll run the full
turso_corelibrary unit suite to get meaningful coverage, then inspect the final diff and repository status for generated artifacts. - Assistant
The full core library suite passes: 1,775 passed and 20 ignored. I’ll now review the final diff for scope, verify no temporary/generated files entered the repository, and summarize the exact behavior and checklist before submission.
- Assistant
Final review is clean and limited to the two implementation files.
Checklist:
- Successfully reproduced the reported error. The original SQL left
(t, 2)insqlite_sequence; a subsequent insert advanced it to3, confirming both persisted-row and cached-metadata defects. - Confirmed the fix resolves the error. The exact scenario now returns zero matching sequence rows, including after another insert. Mixed-case names and attached databases also clean up correctly.
- Considered edge cases. Cleanup is restricted to a true
ALTER COLUMNof the AUTOINCREMENT rowid-alias column. Altering another column andRENAME COLUMNboth preserve and continue advancing the sequence. Other tables’ sequence rows remain untouched. - Ran affected existing tests. All 57 ALTER COLUMN SQL tests passed; all 37 AUTOINCREMENT SQL tests passed; , Clippy with warnings denied, and the full core library suite passed (1,775 passed, 20 ignored).
- Successfully reproduced the reported error. The original SQL left
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.