MCPcopy Create free account
hub / github.com/bytebase/bytebase / RenameIndex

Method RenameIndex

backend/store/model/database.go:1249–1277  ·  view source on GitHub ↗

RenameIndex renames an index in the table. Returns an error if the old index doesn't exist or new index already exists.

(oldName string, newName string)

Source from the content-addressed store, hash-verified

1247// RenameIndex renames an index in the table.
1248// Returns an error if the old index doesn't exist or new index already exists.
1249func (t *TableMetadata) RenameIndex(oldName string, newName string) error {
1250 if oldName == newName {
1251 return nil
1252 }
1253
1254 // Check if old index exists
1255 oldIndex := t.GetIndex(oldName)
1256 if oldIndex == nil {
1257 return errors.Errorf("index %q does not exist in table %q", oldName, t.proto.Name)
1258 }
1259
1260 // Check if new index already exists
1261 if t.GetIndex(newName) != nil {
1262 return errors.Errorf("index %q already exists in table %q", newName, t.proto.Name)
1263 }
1264
1265 // Remove from internal map using old name
1266 oldIndexID := normalizeNameByCaseSensitivity(oldName, t.isDetailCaseSensitive)
1267 delete(t.internalIndexes, oldIndexID)
1268
1269 // Update the index name in the proto
1270 oldIndex.proto.Name = newName
1271
1272 // Add back to internal map using new name
1273 newIndexID := normalizeNameByCaseSensitivity(newName, t.isDetailCaseSensitive)
1274 t.internalIndexes[newIndexID] = oldIndex
1275
1276 return nil
1277}
1278
1279// getIsDetailCaseSensitive is a special case for MySQL, MariaDB, and TiDB.
1280// From MySQL documentation:

Callers 1

tidbAlterTableFunction · 0.80

Calls 3

GetIndexMethod · 0.95
ErrorfMethod · 0.80

Tested by

no test coverage detected