dropColumnInternal is the internal implementation that allows controlling position renumbering.
(columnName string, renumberPositions bool)
| 999 | |
| 1000 | // dropColumnInternal is the internal implementation that allows controlling position renumbering. |
| 1001 | func (t *TableMetadata) dropColumnInternal(columnName string, renumberPositions bool) error { |
| 1002 | // Check if column exists |
| 1003 | if t.GetColumn(columnName) == nil { |
| 1004 | return errors.Errorf("column %q does not exist in table %q", columnName, t.proto.Name) |
| 1005 | } |
| 1006 | |
| 1007 | // Remove from internal map |
| 1008 | columnID := normalizeNameByCaseSensitivity(columnName, t.isDetailCaseSensitive) |
| 1009 | delete(t.internalColumn, columnID) |
| 1010 | |
| 1011 | // Remove from proto's column list |
| 1012 | newColumns := make([]*storepb.ColumnMetadata, 0, len(t.proto.Columns)-1) |
| 1013 | for _, column := range t.proto.Columns { |
| 1014 | if t.isDetailCaseSensitive { |
| 1015 | if column.Name != columnName { |
| 1016 | newColumns = append(newColumns, column) |
| 1017 | } |
| 1018 | } else { |
| 1019 | if !strings.EqualFold(column.Name, columnName) { |
| 1020 | newColumns = append(newColumns, column) |
| 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | t.proto.Columns = newColumns |
| 1025 | |
| 1026 | // Renumber positions to be sequential (1-indexed) if requested |
| 1027 | // MySQL/TiDB: renumber positions (1, 2, 3, ...) |
| 1028 | // PostgreSQL: keep original positions (gaps are allowed) |
| 1029 | if renumberPositions { |
| 1030 | for i, col := range newColumns { |
| 1031 | col.Position = int32(i + 1) |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | // Remove column from indexes that reference it |
| 1036 | for _, index := range t.internalIndexes { |
| 1037 | var newExpressions []string |
| 1038 | for _, expr := range index.proto.Expressions { |
| 1039 | if t.isDetailCaseSensitive { |
| 1040 | if expr != columnName { |
| 1041 | newExpressions = append(newExpressions, expr) |
| 1042 | } |
| 1043 | } else { |
| 1044 | if !strings.EqualFold(expr, columnName) { |
| 1045 | newExpressions = append(newExpressions, expr) |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | index.proto.Expressions = newExpressions |
| 1050 | } |
| 1051 | |
| 1052 | // Remove empty indexes (indexes that had all columns dropped) |
| 1053 | var indexesToRemove []string |
| 1054 | for indexName, index := range t.internalIndexes { |
| 1055 | if len(index.proto.Expressions) == 0 { |
| 1056 | indexesToRemove = append(indexesToRemove, indexName) |
| 1057 | } |
| 1058 | } |
no test coverage detected