github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/schema/alterschema/dropcolumn.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package alterschema 16 17 import ( 18 "context" 19 "errors" 20 "fmt" 21 22 "github.com/dolthub/dolt/go/libraries/doltcore/doltdb" 23 "github.com/dolthub/dolt/go/libraries/doltcore/schema" 24 ) 25 26 var ErrKeylessAltTbl = errors.New("schema alterations not supported for keyless tables") 27 28 // DropColumn drops a column from a table, and removes its associated cell values 29 func DropColumn(ctx context.Context, tbl *doltdb.Table, colName string, foreignKeys []doltdb.ForeignKey) (*doltdb.Table, error) { 30 if tbl == nil { 31 panic("invalid parameters") 32 } 33 34 sch, err := tbl.GetSchema(ctx) 35 if err != nil { 36 return nil, err 37 } 38 39 if schema.IsKeyless(sch) { 40 return nil, ErrKeylessAltTbl 41 } 42 43 var dropTag uint64 44 if col, ok := sch.GetAllCols().GetByName(colName); !ok { 45 return nil, schema.ErrColNotFound 46 } else if col.IsPartOfPK { 47 return nil, errors.New("Cannot drop column in primary key") 48 } else { 49 dropTag = col.Tag 50 } 51 52 for _, foreignKey := range foreignKeys { 53 for _, fkTag := range foreignKey.TableColumns { 54 if dropTag == fkTag { 55 return nil, fmt.Errorf("cannot drop column `%s` as it is used in foreign key `%d`", colName, dropTag) 56 } 57 } 58 for _, fkTag := range foreignKey.ReferencedTableColumns { 59 if dropTag == fkTag { 60 return nil, fmt.Errorf("cannot drop column `%s` as it is used in foreign key `%d`", colName, dropTag) 61 } 62 } 63 } 64 65 for _, index := range sch.Indexes().IndexesWithColumn(colName) { 66 _, err = sch.Indexes().RemoveIndex(index.Name()) 67 if err != nil { 68 return nil, err 69 } 70 tbl, err = tbl.DeleteIndexRowData(ctx, index.Name()) 71 if err != nil { 72 return nil, err 73 } 74 } 75 76 cols := make([]schema.Column, 0) 77 err = sch.GetAllCols().Iter(func(tag uint64, col schema.Column) (stop bool, err error) { 78 if col.Name != colName { 79 cols = append(cols, col) 80 } 81 return false, nil 82 }) 83 84 if err != nil { 85 return nil, err 86 } 87 88 colColl := schema.NewColCollection(cols...) 89 newSch, err := schema.SchemaFromCols(colColl) 90 if err != nil { 91 return nil, err 92 } 93 newSch.Indexes().AddIndex(sch.Indexes().AllIndexes()...) 94 95 return tbl.UpdateSchema(ctx, newSch) 96 }