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

Function BeforeTableModifyColumn

server/hook/table_modify_column.go:37–106  ·  view source on GitHub ↗

BeforeTableModifyColumn handles validation that's unique to Doltgres.

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

Source from the content-addressed store, hash-verified

35
36// BeforeTableModifyColumn handles validation that's unique to Doltgres.
37func BeforeTableModifyColumn(ctx *sql.Context, runner sql.StatementRunner, nodeInterface sql.Node) (sql.Node, error) {
38 n, ok := nodeInterface.(*plan.ModifyColumn)
39 if !ok {
40 return nil, errors.Errorf("MODIFY COLUMN pre-hook expected `*plan.ModifyColumn` but received `%T`", nodeInterface)
41 }
42
43 // Figure out what was changed. We know it's not the name because we have a dedicated *RenameColumn node.
44 changed := beforeTableModifyColumnChange_None
45 newColumn := n.NewColumn()
46 for _, col := range n.TargetSchema() {
47 if col.Name == newColumn.Name {
48 if !col.Type.Equals(newColumn.Type) {
49 changed = beforeTableModifyColumnChange_Type
50 }
51 }
52 }
53 if changed == beforeTableModifyColumnChange_None {
54 return n, nil
55 }
56
57 // Grab the table being altered (so we know the schema)
58 doltTable := core.SQLNodeToDoltTable(n.Table)
59 if doltTable == nil {
60 // If this table isn't a Dolt table then we don't have anything to do
61 return n, nil
62 }
63 _, root, err := core.GetRootFromContext(ctx)
64 if err != nil {
65 return n, nil
66 }
67 tableName := doltTable.TableName()
68 tableAsType := id.NewType(tableName.Schema, tableName.Name)
69 allTableNames, err := root.GetAllTableNames(ctx, false)
70 if err != nil {
71 return nil, err
72 }
73
74 for _, otherTableName := range allTableNames {
75 if doltdb.IsSystemTable(otherTableName) {
76 // System tables don't use any table types
77 continue
78 }
79 otherTable, ok, err := root.GetTable(ctx, otherTableName)
80 if err != nil {
81 return nil, err
82 }
83 if !ok {
84 return nil, errors.Errorf("root returned table name `%s` but it could not be found?", otherTableName.String())
85 }
86 otherTableSch, err := otherTable.GetSchema(ctx)
87 if err != nil {
88 return nil, err
89 }
90 for _, otherCol := range otherTableSch.GetAllCols().GetColumns() {
91 colType := otherCol.TypeInfo.ToSqlType()
92 dgtype, ok := colType.(*pgtypes.DoltgresType)
93 if !ok {
94 // If this isn't a Doltgres type, then it can't be a table type so we can ignore it

Callers

nothing calls this directly

Calls 12

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

Tested by

no test coverage detected