MCPcopy Create free account
hub / github.com/dolthub/doltgresql / AfterTableDropColumn

Function AfterTableDropColumn

server/hook/table_drop_column.go:31–121  ·  view source on GitHub ↗

AfterTableDropColumn handles updating various table columns, alongside other validation that's unique to Doltgres.

(ctx *sql.Context, runner sql.StatementRunner, nodeInterface sql.Node)

Source from the content-addressed store, hash-verified

29
30// AfterTableDropColumn handles updating various table columns, alongside other validation that's unique to Doltgres.
31func AfterTableDropColumn(ctx *sql.Context, runner sql.StatementRunner, nodeInterface sql.Node) error {
32 n, ok := nodeInterface.(*plan.DropColumn)
33 if !ok {
34 return errors.Errorf("DROP COLUMN post-hook expected `*plan.DropColumn` but received `%T`", nodeInterface)
35 }
36
37 // Grab the table being altered
38 doltTable := core.SQLNodeToDoltTable(n.Table)
39 if doltTable == nil {
40 // If this table isn't a Dolt table then we don't have anything to do
41 return nil
42 }
43 _, root, err := core.GetRootFromContext(ctx)
44 if err != nil {
45 return err
46 }
47 tableName := doltTable.TableName()
48 tableAsType := id.NewType(tableName.Schema, tableName.Name)
49 allTableNames, err := root.GetAllTableNames(ctx, false)
50 if err != nil {
51 return err
52 }
53 sch := n.TargetSchema()
54
55 for _, otherTableName := range allTableNames {
56 if doltdb.IsSystemTable(otherTableName) {
57 // System tables don't use any table types
58 continue
59 }
60 otherTable, ok, err := root.GetTable(ctx, otherTableName)
61 if err != nil {
62 return err
63 }
64 if !ok {
65 return errors.Errorf("root returned table name `%s` but it could not be found?", otherTableName.String())
66 }
67 otherTableSch, err := otherTable.GetSchema(ctx)
68 if err != nil {
69 return err
70 }
71 for _, otherCol := range otherTableSch.GetAllCols().GetColumns() {
72 colType := otherCol.TypeInfo.ToSqlType()
73 dgtype, ok := colType.(*pgtypes.DoltgresType)
74 if !ok {
75 // If this isn't a Doltgres type, then it can't be a table type so we can ignore it
76 continue
77 }
78 if dgtype.ID != tableAsType {
79 // This column isn't our table type, so we can ignore it
80 continue
81 }
82 // Build the UPDATE statement that we'll run for this table
83 trimIdx := -1
84 for i, col := range sch {
85 if col.Name == n.Column {
86 trimIdx = i
87 break
88 }

Callers

nothing calls this directly

Calls 12

SQLNodeToDoltTableFunction · 0.92
GetRootFromContextFunction · 0.92
NewTypeFunction · 0.92
GetAllTableNamesMethod · 0.80
TargetSchemaMethod · 0.80
GetTableMethod · 0.80
ToSqlTypeMethod · 0.80
QueryWithBindingsMethod · 0.80
ErrorfMethod · 0.65
StringMethod · 0.65
TableNameMethod · 0.45
GetSchemaMethod · 0.45

Tested by

no test coverage detected