From 45d959635cf17454501b2036017971a640d7be77 Mon Sep 17 00:00:00 2001 From: "Levy A." Date: Tue, 12 Aug 2025 17:59:38 -0300 Subject: [PATCH 1/2] fix: check if index exists with the same name --- core/translate/alter.rs | 8 +++++++- testing/alter_table.test | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/core/translate/alter.rs b/core/translate/alter.rs index 1613aeae4..8df31d58a 100644 --- a/core/translate/alter.rs +++ b/core/translate/alter.rs @@ -323,7 +323,13 @@ pub fn translate_alter_table( ast::AlterTableBody::RenameTo(new_name) => { let new_name = new_name.as_str(); - if schema.get_table(new_name).is_some() { + if schema.get_table(new_name).is_some() + || schema + .indexes + .values() + .flatten() + .any(|index| index.name == normalize_ident(new_name)) + { return Err(LimboError::ParseError(format!( "there is already another table or index with this name: {new_name}" ))); diff --git a/testing/alter_table.test b/testing/alter_table.test index 24bf74fe8..6f44b9ad1 100755 --- a/testing/alter_table.test +++ b/testing/alter_table.test @@ -126,3 +126,15 @@ do_execsql_test_in_memory_any_error fail-alter-table-drop-primary-key-column-con CREATE TABLE t (a, b, PRIMARY KEY (a)); ALTER TABLE t DROP a; } + +do_execsql_test_in_memory_any_error fail-alter-table-rename-to-existing-index { + CREATE TABLE x (a); + CREATE INDEX y ON x (a); + ALTER TABLE x RENAME TO y; +} + +do_execsql_test_in_memory_any_error fail-alter-table-rename-to-existing-table { + CREATE TABLE x (a); + CREATE TABLE y (a); + ALTER TABLE x RENAME TO y; +} From 6a37916b4983c748d83e97c75209f62adf9f7784 Mon Sep 17 00:00:00 2001 From: "Levy A." Date: Tue, 12 Aug 2025 22:43:37 -0300 Subject: [PATCH 2/2] fix: rename indexed columns --- core/vdbe/execute.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/core/vdbe/execute.rs b/core/vdbe/execute.rs index fda2982a7..f5e9c254d 100644 --- a/core/vdbe/execute.rs +++ b/core/vdbe/execute.rs @@ -7267,7 +7267,7 @@ pub fn op_rename_column( ) -> Result { load_insn!( RenameColumn { - table, + table: table_name, column_index, name }, @@ -7279,7 +7279,7 @@ pub fn op_rename_column( conn.with_schema_mut(|schema| { let table = schema .tables - .get_mut(table) + .get_mut(table_name) .expect("table being renamed should be in schema"); let table = Arc::make_mut(table); @@ -7294,6 +7294,20 @@ pub fn op_rename_column( .columns .get_mut(*column_index) .expect("renamed column should be in schema"); + + if let Some(indexes) = schema.indexes.get_mut(table_name) { + for index in indexes { + let index = Arc::make_mut(index); + for index_column in &mut index.columns { + if index_column.name + == *column.name.as_ref().expect("btree column should be named") + { + index_column.name = name.to_owned(); + } + } + } + } + column.name = Some(name.to_owned()); });