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/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()); }); 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; +}