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) │ └───────┴─────────────────┴─────────────────┴──────────┴────────────────────────────────────────┘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 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 start by exploring the repository structure and finding the relevant code for ALTER COLUMN and sqlite_sequence handling.
- Assistant
Let me search with grep instead and look at the core structure.
- Assistant
The alter table logic is in core/translate/alter.rs. Let me look at that.
- Assistant
Let me read the alter column handling and how sequence table cleanup is done elsewhere.
- Assistant
Let me look at drop table sequence cleanup and continue reading the AlterColumn implementation.
- Assistant
Let me look at how drop table handles sqlite_sequence and also how drop column might handle it. Also need to look at the AlterTable for adding/dropping autoincrement.
- Assistant
Let me check DropColumn for autoincrement handling and how scheme characteristics get updated.
- Assistant
Let me read the AlterColumn VDBE instruction implementation and how schema recreate works.
- Assistant
Let me continue reading op_alter_column to see how has_autoincrement is handled.
- Assistant
Let me look at how schema SQL is rewritten during ALTER COLUMN - specifically the AlterTable function path.
- Assistant
Let me keep reading for CreateTable handling and howALTER COLUMN replaces column.
- Assistant
Let me find related tests and any existing cleanup helpers for sqlite_sequence.
- Assistant
There's a test_alter_table_reopen.rs that specifically tests sqlite_sequence. Let me look at that and test_ddl.rs for alter column tests.
- Assistant
Let me check how SQLite and Turso handle dropping the last autoincrementtable, and how create table creates sqlite_sequence.
- Assistant
Let me check SQLite docs/COMPAT and whether drop table ever removes the sequence table itself.
- Assistant
Let me look at AGENTS.md guidance and reproduce the issue.
- Assistant
Let me see if there's a built binary and reproduce the bug.
- Assistant
There's a relevant commit about stale sqlite_sequence rows. Let me inspect it and then build to reproduce.
- Assistant
Let me look at how drop table cleans sequence entries more carefully, and check if there's a shared helperwe should reuse for AlterColumn.
- Assistant
Let me examine how the schema is rebuilt after ALTER and whether has_autoincrement stays stale in memory.
- Assistant
Let me look at how primary_key_columns is used and whether AlterColumn handling of PK is broken too.
- Assistant
Build is downloading the toolchain. Meanwhile I'll dig into emit_rename_sqlite_sequence_entry and the insert path for sequence cleanup patterns.
- Assistant
Let me wait for the build and look at the schema update path once more to plan the fix carefully.
- Assistant
There's already a binary — let me reproduce the issue.
- Assistant
Reproduced. Let me also check INSERT after ALTER and DROP TABLE for comparison.
- Assistant
Confirmed the bug: after ALTER, inserts still bump sqlite_sequence. I'll implement cleanup matching DROP TABLE and clear
has_autoincrement. - Assistant
Let me understand how primary key state is maintained before making the change.
- Assistant
I'll add a helper to delete sqlite_sequence entries and call it when ALTER COLUMN removes AUTOINCREMENT, and clear
has_autoincrementin the VDBE op. - Assistant
I'll navigate to the right place and apply the edit.
- Assistant
Now I can use replace correctly.
- Assistant
Next I'll wire the helper into the ALTER COLUMN path when autoincrement is removed.
- Assistant
I'll detect loss of autoincrement and emit the sequence entry delete before the schema cookie update.
- Assistant
I'll add the loses_autoincrement check near when we compute version stuff, and call delete before SetCookie.
- Assistant
Now update op_alter_column to clear has_autoincrement and fix primary_key_columns.
- Assistant
Need to open the execute.rs file.
- Assistant
I'll capture the old column's AI/PK state before replacement and update schema flags accordingly.
- Assistant
I'll update the section to save old column AI state and fix primary_key and has_autoincrement.
- Assistant
Let me re-check how table-level PRIMARY KEY is rewritten on ALTER COLUMN — I may have over-cleared primary_key_columns.
- Assistant
Let me check if BTreeTable keeps the original SQL5 that can tell table-level vs column-level PK.
- Assistant
I'll curb the primary_key_columns fix to the autoincrement case so table-level PKs stay intact.
- Assistant
Need to open xem the execute file again.
- Assistant
Now I can do the replace.
- Assistant
Let me build and verify the fix, then add a regression test.
- Assistant
Build succeeded. Let me reproduce the fix.
- Assistant
Empty sequence selects look good. Let me confirm their output format and whether the issue wants the table dropped too.
- Assistant
Fix matches DROP TABLE. Running related existing tests without adding new ones.
- Assistant
Alter table tests passed. Let me also run autoincrement and alter_column sqltests.
- Assistant
Let me run the alter_column sqltest more carefully and autoincr tests.
- Assistant
The filter timing looks suspiciously short — let me check whether the tests actually ran.
- Assistant
Let me check help.
- Assistant
Let me verify a few more edge cases: non-AI column alters and mixed-case table names.
- Assistant
I'll format the code and run a final set of related tests.
- Assistant
The issue title emphasizes the table itself staying behind. Let me check whether we should drop
sqlite_sequencewhen no AUTOINCREMENT tables remain. - Assistant
I'll stick with entry cleanup (matching DROP TABLE) and review the final diff.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.