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

Function AfterTableAddColumn

server/hook/table_add_column.go:94–179  ·  view source on GitHub ↗

AfterTableAddColumn 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

92
93// AfterTableAddColumn handles updating various table columns, alongside other validation that's unique to Doltgres.
94func AfterTableAddColumn(ctx *sql.Context, runner sql.StatementRunner, nodeInterface sql.Node) error {
95 n, ok := nodeInterface.(*plan.AddColumn)
96 if !ok {
97 return errors.Errorf("ADD COLUMN post-hook expected `*plan.AddColumn` but received `%T`", nodeInterface)
98 }
99
100 // Grab the table being altered
101 doltTable := core.SQLNodeToDoltTable(n.Table)
102 if doltTable == nil {
103 // If this table isn't a Dolt table then we don't have anything to do
104 return nil
105 }
106 _, root, err := core.GetRootFromContext(ctx)
107 if err != nil {
108 return err
109 }
110 tableName := doltTable.TableName()
111 tableAsType := id.NewType(tableName.Schema, tableName.Name)
112 allTableNames, err := root.GetAllTableNames(ctx, false)
113 if err != nil {
114 return err
115 }
116 sch := doltTable.Schema(ctx)
117
118 for _, otherTableName := range allTableNames {
119 if doltdb.IsSystemTable(otherTableName) {
120 // System tables don't use any table types
121 continue
122 }
123 otherTable, ok, err := root.GetTable(ctx, otherTableName)
124 if err != nil {
125 return err
126 }
127 if !ok {
128 return errors.Errorf("root returned table name `%s` but it could not be found?", otherTableName.String())
129 }
130 otherTableSch, err := otherTable.GetSchema(ctx)
131 if err != nil {
132 return err
133 }
134 for _, otherCol := range otherTableSch.GetAllCols().GetColumns() {
135 colType := otherCol.TypeInfo.ToSqlType()
136 dgtype, ok := colType.(*pgtypes.DoltgresType)
137 if !ok {
138 // If this isn't a Doltgres type, then it can't be a table type so we can ignore it
139 continue
140 }
141 if dgtype.ID != tableAsType {
142 // This column isn't our table type, so we can ignore it
143 continue
144 }
145 // Build the UPDATE statement that we'll run for this table
146 rowValues := make([]string, len(sch)+1)
147 for i, col := range sch {
148 rowValues[i] = fmt.Sprintf(`("%s")."%s"`, otherCol.Name, col.Name)
149 }
150 rowValues[len(rowValues)-1] = "NULL"
151 // The UPDATE changes the values in the table

Callers

nothing calls this directly

Calls 12

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

Tested by

no test coverage detected