RenameColumn renames a column in the table. Returns an error if the old column doesn't exist or new column already exists.
(oldName string, newName string)
| 1123 | // RenameColumn renames a column in the table. |
| 1124 | // Returns an error if the old column doesn't exist or new column already exists. |
| 1125 | func (t *TableMetadata) RenameColumn(oldName string, newName string) error { |
| 1126 | if oldName == newName { |
| 1127 | return nil |
| 1128 | } |
| 1129 | |
| 1130 | // Check if old column exists |
| 1131 | oldColumn := t.GetColumn(oldName) |
| 1132 | if oldColumn == nil { |
| 1133 | return errors.Errorf("column %q does not exist in table %q", oldName, t.proto.Name) |
| 1134 | } |
| 1135 | |
| 1136 | // Check if new column already exists |
| 1137 | if t.GetColumn(newName) != nil { |
| 1138 | return errors.Errorf("column %q already exists in table %q", newName, t.proto.Name) |
| 1139 | } |
| 1140 | |
| 1141 | // Remove from internal map using old name |
| 1142 | oldColumnID := normalizeNameByCaseSensitivity(oldName, t.isDetailCaseSensitive) |
| 1143 | delete(t.internalColumn, oldColumnID) |
| 1144 | |
| 1145 | // Update the column name in the proto |
| 1146 | oldColumn.proto.Name = newName |
| 1147 | |
| 1148 | // Add back to internal map using new name |
| 1149 | newColumnID := normalizeNameByCaseSensitivity(newName, t.isDetailCaseSensitive) |
| 1150 | t.internalColumn[newColumnID] = oldColumn |
| 1151 | |
| 1152 | // Update column references in indexes |
| 1153 | for _, index := range t.internalIndexes { |
| 1154 | for i, expr := range index.proto.Expressions { |
| 1155 | if t.isDetailCaseSensitive { |
| 1156 | if expr == oldName { |
| 1157 | index.proto.Expressions[i] = newName |
| 1158 | } |
| 1159 | } else { |
| 1160 | if strings.EqualFold(expr, oldName) { |
| 1161 | index.proto.Expressions[i] = newName |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | return nil |
| 1168 | } |
| 1169 | |
| 1170 | func (t *ExternalTableMetadata) GetProto() *storepb.ExternalTableMetadata { |
| 1171 | return t.proto |
no test coverage detected