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) │ └───────┴─────────────────┴─────────────────┴──────────┴────────────────────────────────────────┘Interface
No new interfaces are introduced.
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.
Assistant
Now let's find where
emit_rename_sqlite_sequence_entryis called, to understand context around RENAME TABLE and add a similar delete function for the ALTER COLUMN case.Assistant
Now let's add a new function to delete the sqlite_sequence entry for a table name.
Assistant
Now let's find the ALTER COLUMN (non-rename) section to add logic that determines if we're losing autoincrement, and call this new function. Let's search for the section again with updated line numbers.
Assistant
Now let's find the place where
emit_rename_sqlite_sequence_entrywas called for RenameTo, and add a call toemit_delete_sqlite_sequence_entryat appropriate place within the AlterColumn branch (after schema table update, similar to trigger/view rewrite emission), guarded byloses_autoincrement_column.Assistant
Good, consistent with using
normalize_ident(table_name)for the sqlite_sequence "name" match. Now let's add the call.Assistant
Now let's also fix the in-memory
has_autoincrementflag update in execute.rs'sop_alter_column.Assistant
Now let's build and re-run the reproduction.
Assistant
Builds fine. Let's re-run reproduction script.
Assistant
Excellent - sqlite_sequence stays empty (no rows) even after reload, so its cleanup persisted to disk. Now let's test edge cases: renaming a column that isn't the autoincrement one, keeping autoincrement column intact via RENAME COLUMN, dropping non-PK column, and altering an autoincrement column to a different INTEGER type without losing PK entirely check sqlite_sequence remains if PK isn't touched.
Assistant
Good - altering an unrelated column preserves sqlite_sequence correctly. Now let's test altering the autoincrement column itself but keeping it INTEGER (no PK re-specified, so it will lose PK anyway per current design) - already covered. Let's also test dropping the autoincrement column via DROP COLUMN to see if that already handles sqlite_sequence cleanup or has the same bug.
Assistant
That's fine - dropping a PK column is disallowed already, so no gap there. Now let's test renaming table (existing rename table sqlite_sequence feature) still works fine, and test with a table without autoincrement (should not attempt any sqlite_sequence writes at all - table doesn't exist yet perhaps).
Assistant
Good - no sqlite_sequence table exists (never created since no AUTOINCREMENT was used), and our code correctly handles that (it checks
resolver.with_schemaforget_btree_table(SQLITE_SEQUENCE_TABLE_NAME)and returns early if None). No crash. Now let's run the existing test suite to make sure nothing is broken.Assistant
Now let's run the sqltest for alter_column and broader test suite related to sqlite_sequence/insert.
Assistant
All alter_column tests pass. Let's also run broader tests related to insert / sqlite_sequence and rename table.
Assistant
All relevant tests pass. Let's also run MVCC mode check since we deal with
database_uses_mvcc(Delete before insert) - let's ensure our new delete function correctly handles MVCC too, since Delete for row removal in general should already work fine in MVCC (no special pre-delete needed, since we're doing an actual delete, not update). Let's test with MVCC flag.Assistant
Good, MVCC mode works too. Now let's quickly test the MVCC scenario for our specific reproduction directly, since MVCC delete might need special handling.
Assistant
Good, standard usage matches similar delete-only code paths elsewhere. Let's view final diff for review.
Assistant
This looks clean. Let's run the full test suite for core (cargo test) filtering to alter/schema related tests broadly, plus clippy check.
Assistant
No clippy warnings. Now let's run the full core test suite to be safe (excluding slow ones is fine, but let's just run all).
Assistant
Good - passes in isolation with our changes too. This confirms it's a flaky/order-dependent test unrelated to our change (uses randomness, e.g. tampering with random offsets that occasionally may not change the actual result). Let's run the full lib test suite again to double check it's not a consistent failure now.
Assistant
All 1775 tests pass this run (the earlier failure was flaky, confirmed by re-run passing consistently). Now let's clean up temp files created and do a final review of the diff.
Assistant
Let's do a final review of the diff once more.
Assistant
This is a focused, minimal fix. Let's go through the checklist.
Assistant
Checklist review:
- Reproduced the error: Yes — built
tursodband ran the exact repro from the issue (CREATE TABLE ... AUTOINCREMENT,INSERT,ALTER TABLE ... ALTER COLUMN ... TO ... TEXT), confirming the stalesqlite_sequencerow remained (t | 2) even though the column was no longerINTEGER PRIMARY KEY AUTOINCREMENT. I also discovered a related in-memory bug wherehas_autoincrementwasn't recomputed, causing subsequent inserts in the same session to keep touching incorrectly.
- Reproduced the error: Yes — built
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.