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, patch the schema-change path, and run focused plus broader tests.
- Assistant
rgis unavailable, so I’ll usegrep/find. Before editing, I’ll also read the repository’s agent guidance and inspect workspace package names to avoid running unnecessarily broad commands. - Assistant
The implementation already has a helper for renaming
sqlite_sequenceentries during table rename, while ALTER COLUMN’s table reconstruction likely omits sequence cleanup. I’ll inspect the helper, the ALTER COLUMN compilation path, DROP TABLE’s cleanup behavior, and nearby integration tests to choose a bytecode-level fix consistent with existing patterns. - Assistant
I need a cleaner view of the control flow because the previous combined output was truncated. I’ll locate function boundaries and inspect specifically where ALTER COLUMN determines old/new AUTOINCREMENT status and emits table rewrites. I’ll also find the generic row-deletion bytecode pattern used by DROP TABLE.
- Assistant
ALTER COLUMN constructs a replacement
BTreeTable; the key condition should be whether the original table is AUTOINCREMENT and the rewritten schema no longer is. I’ll inspect available table metadata fields and the final emission ordering, plus DROP TABLE’s exact sequence cleanup helper, so cleanup is transactional and MVCC-safe. - Assistant
A subtlety is that cloning the table and replacing its column does not automatically recompute the public
has_autoincrementflag. Therefore, cleanup should be derived directly from operation semantics: only a true ALTER (not RENAME COLUMN) of the rowid-alias AUTOINCREMENT column removes AUTOINCREMENT. I’ll verify howAlterColumnupdates in-memory metadata and SQL serialization before implementing that predicate. - Assistant
The bytecode handler will confirm whether the flag becomes false after replacing the PK column. I’ll inspect it and the column mutation bookkeeping. Then I’ll add a small reusable sequence-row deletion emitter near the rename emitter and call it only when altering the AUTOINCREMENT rowid alias. This also naturally removes duplicate stale entries if corruption or MVCC history produced more than one matching row.
- Assistant
The mutation guard only refreshes generated-column layout, not AUTOINCREMENT. I’ll inspect the rest of
op_alter_column; this may reveal a second correctness bug where the in-memory flag remains true until reopen even though persisted SQL no longer contains AUTOINCREMENT. The fix should clear that flag as part of the same operation, otherwise subsequent inserts could still update sequence despite deleting the row. - Assistant
I’ve confirmed two coupled defects: the persistent row is never deleted, and the live schema retains
has_autoincrement = trueafter altering the AUTOINCREMENT PK until reopen. I’ll fix both. Cleanup will target only the altered table’s row(s), only when the old column is the AUTOINCREMENT rowid alias; altering any other column must preserve the counter. The existingsqlite_sequencetable itself will remain, matching SQLite’s behavior after AUTOINCREMENT has once created it. - Assistant
The workspace’s pinned Rust 1.88 formatter reordered and reformatted many unrelated lines in a very large file, creating unacceptable diff noise. I’ll revert both files and reapply the same focused edits without whole-file formatting, then use
rustfmtonly if a narrow mechanism is available; otherwise the inserted code already follows local style andgit diff --checkwill validate whitespace. - Assistant
The focused diff is now clean. Before compiling, I’ll check whether an existing pre-change CLI binary is available; if so I can document direct reproduction of the original issue. Then I’ll build the current CLI and run a temporary SQL script covering: reported conversion, immediate post-ALTER insert behavior, altering a non-PK column (sequence must remain), quoted mixed-case table names, and database reopen persistence.
- Assistant
No prebuilt binary exists, so I cannot execute the old revision without temporarily reverting the patch (which the repository guidance explicitly discourages). The issue’s SQL and code-path inspection establish the reproduction. I’ll now compile the CLI; this will also catch borrow/type issues in both changed modules.
- Assistant
The build command used the directory/package label rather than Cargo’s actual package name (
turso_cli), so dependency resolution succeeded but compilation did not start. I’ll rerun with the correct package ID. - Assistant
Compilation passes. I’ll check CLI noninteractive usage and execute the regression scenarios against temporary files, capturing machine-readable output.
- Assistant
I’ll run one compound scenario with separate tables so results distinguish cleanup from preservation. The mixed-case case is useful because sequence names are normalized elsewhere in this engine; the helper follows existing DROP/RENAME normalization. I’ll then reopen the file and query again.
- Assistant
The reported scenario now yields zero matching sequence rows immediately and after reopen; plain inserts work in the same connection, confirming the in-memory flag fix. Altering a non-PK column preserves and advances its sequence to 2. Mixed-case cleanup also works.
I’ll now run the existing ALTER COLUMN SQL test file through the preferred sqltest runner. First I’ll inspect runner filtering syntax to avoid executing snapshots unnecessarily.
- Assistant
The runner accepts a file path directly. I’ll run the full existing
alter_column.sqltestwith the Rust backend, then the full AUTOINCREMENT suite because the patch changes both ALTER and sequence metadata behavior. - Assistant
Both focused suites pass (57 ALTER tests and 37 AUTOINCREMENT tests). I’ll do a final edge-case check that all duplicate matching sequence entries are removed, then run the relevant Rust integration test module if its package/test target is identifiable. Finally I’ll inspect the diff and repository status for generated artifacts or unintended changes.
- Assistant
Duplicate cleanup succeeds, confirming the cursor loop removes every stale row rather than only the first. The existing reopen integration test specifically verifies that altering a different column preserves AUTOINCREMENT across reopen; I’ll run it as an additional regression guard.
- Assistant
The integration command compiled the full target successfully but
--exactdid not match the module-qualified test name, so it ran zero tests. I’ll rerun using substring filtering so the intended test actually executes. - Assistant
The intended reopen integration test passes. I’ll run Clippy on the changed core crate with warnings denied, then perform a final diff/status review.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.