DROP TABLE: index drop

added missing instructions to drop the indices for a table physically from btree pages in a loop
This commit is contained in:
Zaid Humayun
2025-02-10 10:34:01 +05:30
parent 34f390abc9
commit 07efc9b902
2 changed files with 27 additions and 1 deletions

View File

@@ -48,6 +48,13 @@ impl Schema {
.push(index.clone())
}
pub fn get_indices(&self, table_name: &str) -> &[Rc<Index>] {
let name = normalize_ident(table_name);
self.indexes
.get(&name)
.map_or_else(|| &[] as &[Rc<Index>], |v| v.as_slice())
}
pub fn remove_indices_for_table(&mut self, table_name: &str) {
let name = normalize_ident(table_name);
self.indexes.remove(&name);

View File

@@ -631,7 +631,26 @@ fn translate_drop_table(
program.resolve_label(end_metadata_label, program.offset());
// end of loop on schema table
// 2. Destroy the table structure
// 2. Destroy the indices within a loop
let indices = schema.get_indices(&tbl_name.name.0);
for index in indices {
program.emit_insn(Insn::Destroy {
root: index.root_page,
former_root_reg: 0, // no autovacuum (https://www.sqlite.org/opcode.html#Destroy)
is_temp: 0,
});
let null_reg_1 = program.alloc_register();
let null_reg_2 = program.alloc_register();
program.emit_null(null_reg_1, Some(null_reg_2));
// 3. TODO: Open an ephemeral table, and read over triggers from schema table into ephemeral table
// Requires support via https://github.com/tursodatabase/limbo/pull/768
// 4. TODO: Open a write cursor to the schema table and re-insert all triggers into the sqlite schema table from the ephemeral table and delete old trigger
// Requires support via https://github.com/tursodatabase/limbo/pull/768
}
// 3. Destroy the table structure
program.emit_insn(Insn::Destroy {
root: table.root_page,
former_root_reg: 0, // no autovacuum (https://www.sqlite.org/opcode.html#Destroy)