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 to understand the codebase and then find the relevant code for ALTER COLUMN functionality.
Assistant
I need to make only one tool call at a time. Let me start by exploring the repository structure.
Assistant
This is the Turso database (a SQLite-compatible database in Rust). Let me search for ALTER COLUMN related code.
Assistant
The file is large. Let me search for the specific ALTER COLUMN functionality and also look for sqlite_sequence handling.
Assistant
Let me look at the translate/alter.rs file to understand the full ALTER COLUMN flow.
Assistant
Now let me understand how sqlite_sequence is managed. Let me look at the relevant code for autoincrement handling.
Assistant
Let me look at how sqlite_sequence is created/managed during table operations.
Assistant
Now let me look at how DROP TABLE handles sqlite_sequence for reference on the pattern used.
Assistant
Let me look at the DROP TABLE flow to understand how sqlite_sequence entries are removed there.
Assistant
Let me look at how DROP TABLE handles the sqlite_sequence removal. Let me search further in the drop table code.
Assistant
Let me look at the AlterTable function (AlterColumn) that updates the sqlite_schema SQL.
Assistant
Let me understand the full picture. Let me build the project and reproduce the issue first.
Assistant
Let me check the insert.rs autoincrement handling to understand the full impact.
Assistant
Let me check if ParseSchema is emitted after ALTER COLUMN and how the schema gets refreshed.
Assistant
Let me understand the schema API to check for autoincrement tables across the schema.
Assistant
Let me look at the beginning of the alter_table translate function to understand the structure.
Assistant
Now let me build the project to make sure I can compile, and reproduce the issue.
Assistant
The build succeeded. Now let me reproduce the issue.
Assistant
I've reproduced the issue. Now let me look at existing tests for alter column and autoincrement to understand testing patterns.
Assistant
Let me look at how DROP TABLE updates the in-memory schema at runtime.
Assistant
Let me look at the AlterColumn instruction definition.
Assistant
Let me find all places where the AlterColumn instruction is constructed and the load_insn pattern.
Assistant
Let me check if there's any existing logic that drops sqlite_sequence when no autoincrement tables remain.
Assistant
Let me look at how DROP INDEX deletes from sqlite_schema for a simpler pattern.
Assistant
Let me look at how the Destroy instruction is executed to understand what's needed.
Assistant
OK, the Destroy instruction handles the btree destruction. Now I understand the full mechanism.
Let me now implement the fix. I'll write a helper function in alter.rs that emits the bytecode to:
- Delete the sqlite_sequence entry for the table.
- If
drop_tableis true: destroy the sqlite_sequence btree and delete its row from sqlite_schema.
Let me look at how to emit the Destroy + sqlite_schema deletion. I'll model the sqlite_schema deletion on the DROP INDEX pattern (loop over sqlite_schema, find name='sqlite_sequence' AND type='table', delete).
For the in-memory schema update in op_alter_column:
- Set
has_autoincrement = falseon the altered table (if autoincrement_removed). - Remove sqlite_sequence from in-memory schema (if drop_sqlite_sequence_table). I can use
schema.remove_table(SQLITE_SEQUENCE_TABLE_NAME).
Let me check
remove_tableexists.Assistant
Let me look at the imports and structure of alter.rs.
Assistant
Now I have all the info I need. Let me check the
SQLITE_SEQUENCE_TABLE_NAMEimport in alter.rs.Assistant
Now let me look at the exact location where I'll insert my code in the ALTER COLUMN branch, right before the SetCookie.
Assistant
Now I have a clear picture. Let me implement the fix. First, I'll modify the AlterColumn instruction to add flags.
Assistant
Now update explain.rs to handle the new fields.
Assistant
Now update the op_alter_column function in execute.rs to handle the new fields and update the in-memory schema.
Assistant
Now let me add the in-memory schema update in op_alter_column. Let me find the right place - after the schema mutation block that updates the column.
Assistant
Let me find where the
with_database_schema_mutclosure ends in op_alter_column.Assistant
Let me look more carefully at the schema mutation closure to understand borrow patterns.
Assistant
Let me look at the structure around the column update and PK/FK handling.
Assistant
I'll add the
has_autoincrement = falseupdate right after the rowid-alias recomputation (before the FK loop to avoid borrow conflicts).Assistant
Now let me add the in-memory
remove_tablefor sqlite_sequence after the main schema mutation closure. Let me view the exact location.Assistant
Now let me add the helper function and the inline code in translate/alter.rs. First, let me add the helper function near
emit_rename_sqlite_sequence_entry.Assistant
I'll add the helper function right after
emit_rename_sqlite_sequence_entryends. Let me insert it.Assistant
Now let me compute the flags and emit the cleanup in the ALTER COLUMN branch. Let me view the area around the SetCookie.
Assistant
Let me check the variable names in the ALTER COLUMN branch - is it
btreeororiginal_btree?Assistant
Let me find the variable name for the btree in the ALTER COLUMN branch. Let me search around line 1691 where the original column is referenced.
Assistant
Let me check where
btreeis defined in this function.Assistant
Hmm,
btreeat line 868 is a clone. Let me check the scope. Thebtreevariable used in the AlterColumn branch (line 1741btree.get_column(from)) - is it the samebtreefrom line 868? Let me check the scope of that variable.Line 868 is
let mut btree = (*original_btree).clone();. This is probably inside a different branch (DROP COLUMN maybe). Let me check the overall function structure - the match on alter_table body.Let me look at where the match arms are and the scope of
btree.Assistant
Let me check if writing to register 0 (former_root_reg) is safe, as DROP INDEX uses
former_root_reg: 0.Assistant
former_root_reg: 0is the established "no autovacuum" pattern used in DROP INDEX, DROP VIEW, etc. Now let me add the computation and cleanup emission before the SetCookie.Assistant
Now let me build to check for compilation errors.
Assistant
The build succeeded. Now let me test the fix with the reproduction scenario.
Assistant
The
sqlite_sequencetable is now gone. Let me verify the full state.Assistant
The output is hard to read with the pretty formatting. Let me use list output mode.
Assistant
The fix works. Now let me test edge cases. First, multiple autoincrement tables where only one is altered.
Assistant
Excellent! When only one of two autoincrement tables is altered, the table is kept but the stale entry is removed. Now let me test altering a non-PK column on an autoincrement table (should preserve autoincrement).
Assistant
Altering a non-PK column preserves autoincrement correctly. Now let me test renaming the autoincrement column.
Assistant
Renaming the autoincrement column preserves autoincrement correctly. Now let me test persistence to disk and reopening - a critical edge case.
Assistant
Persistence works correctly. Let me test the multiple autoincrement tables case with persistence and reopening.
Assistant
Both cases work. Let me test changing the autoincrement column to INTEGER (keeping type but losing PRIMARY KEY).
Assistant
The INTEGER case also correctly removes autoincrement. (Note: the empty
a2values when affinity doesn't change is pre-existing rowid-alias behavior, unrelated to my fix - the TEXT case rewrites rows and shows 1, 2.)Now let me run the existing tests to make sure I didn't break anything. Let me find the relevant test files.
Assistant
Let me check function.rs for AlterTableFunc to make sure the AlterColumn function definition doesn't need updating.
Assistant
That's fine. Now let me run the relevant existing tests. Let me start with the alter table reopen test.
Assistant
That test passes. Let me run the broader alter table and DDL tests.
Assistant
All 56 alter table tests pass. Let me run the DDL and write path tests.
Assistant
All passing. Let me run vacuum tests since they interact with sqlite_sequence.
Assistant
Let me also run the SQL tests for alter_column. Let me check how those are run.
Assistant
The test runner needs the debug build of tursodb. Let me try the rust backend instead, or build the debug binary.
Assistant
All 57 alter_column sqltests pass. Let me check the MVCC consideration - autoincrement isn't supported in MVCC mode, but let me verify my code handles edge cases. Let me check the stmt_journal which references has_autoincrement.
Assistant
The stmt_journal uses
has_autoincrementfrom the table, which I correctly set to false. Let me verify database integrity after the alter.Assistant
Integrity check passes. Let me also verify integrity with multiple autoincrement tables.
Assistant
Let me test with auto_vacuum enabled to ensure the Destroy with
former_root_reg: 0works correctly.Assistant
Autovacuum isn't enabled by default. Let me check the experimental flag. Actually, the error says "Use --experimental-autovacuum flag to enable it." This is a CLI flag. Let me try it. But actually, autovacuum is experimental and may not be the main concern. Let me check if there's a simpler way to test, or just skip this edge case since DROP INDEX/VIEW use the same pattern.
Actually, let me check if autovacuum is even relevant here. The
former_root_regis for incremental vacuum. Whenformer_root_reg: 0, the Destroy writes the former root page to register 0. In autovacuum mode, this is used to track freed pages for the vacuum to move other pages into. But since we're destroying the sqlite_sequence table entirely (and it's a small table), using register 0 should be fine - the same as DROP INDEX/VIEW.Let me try with the experimental flag.
Assistant
Autovacuum mode works correctly. Let me test VACUUM after the alter.
Assistant
VACUUM works correctly. Let me test attached databases.
Assistant
There's an integrity check issue with attached databases. Let me check if this is pre-existing by testing DROP TABLE on an attached database.
Assistant
The "Page X: never used" issue is PRE-EXISTING - it occurs with DROP TABLE on attached databases too, using the same Destroy instruction. It's not specific to my change. Let me verify the attached database file is actually correct by reopening it directly.
Assistant
The attached database is consistent when reopened directly. The integrity check quirk with
PRAGMA aux.integrity_checkis pre-existing (same as DROP TABLE).Let me run a broader set of tests. Let me run the full query_processing tests.
Assistant
All 358 query_processing tests pass. Let me run the trigger tests and check for any sqltests related to autoincrement/sequence.
Assistant
All 37 autoincr tests pass. There's an interesting test
autoinc-drop-last-table-empties-sequence. Let me look at it to understand expected behavior for the last autoincrement table.Assistant
Let me verify that after dropping sqlite_sequence, creating a new autoincrement table recreates it correctly.
Assistant
After dropping sqlite_sequence, creating a new autoincrement table correctly recreates it. Let me run a few more relevant sqltests to be thorough.
Assistant
All tests pass. Let me review my changes and refine the helper to avoid a potential issue: having the sqlite_sequence cursor open while destroying its btree. Let me restructure so the
drop_tablecase doesn't open the entry-deletion cursor at all (since the whole btree is destroyed anyway).Assistant
Let me rewrite the helper function with a cleaner structure that avoids opening the sqlite_sequence cursor when we're going to destroy the whole btree anyway.
Assistant
Build succeeded. Let me re-run the key test scenarios to verify the refactored code works.
Assistant
Both cases work. Let me re-run the persistence and integrity tests.
Assistant
Let me re-test autovacuum and the "create new autoincrement after drop" case with the refactored code.
Assistant
Everything works. Let me run the full test suites once more to confirm the refactored code passes.
Assistant
All pass, including
test_autoincrement_blocked_in_mvcc. Let me run the sqltests one final time.Assistant
All 94 sqltests pass. Let me do a final review of all my changes.
Assistant
The diff looks clean and complete. Let me test one more edge case: an autoincrement table with no inserts (empty sqlite_sequence) being altered.
Assistant
That edge case works too. Let me check for any compiler warnings related to my changes.
Assistant
No warnings. Let me run clippy on the affected crates to be thorough.
Assistant
Clippy found an unnecessary clone. In the non-drop branch,
seq_tableisn't used after the cursor allocation, so I can move it. Let me fix this.Assistant
No more clippy warnings. Let me rebuild and run a final verification.
Assistant
The
sqlite_sequencetable no longer exists (as expected - it's dropped). Let me verify the complete final state.Assistant
The fix works perfectly. Let me run the full relevant integration test suite one final time to confirm nothing is broken.
Assistant
All tests pass (332 integration + 1 MVCC + 98 sqltests). Let me clean up the temporary files.
Assistant
The changes are complete and all tests pass. Let me provide a summary of my work.
Summary
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.