BeforeTableAddColumn handles validation that's unique to Doltgres.
(ctx *sql.Context, runner sql.StatementRunner, nodeInterface sql.Node)
| 30 | |
| 31 | // BeforeTableAddColumn handles validation that's unique to Doltgres. |
| 32 | func BeforeTableAddColumn(ctx *sql.Context, runner sql.StatementRunner, nodeInterface sql.Node) (sql.Node, error) { |
| 33 | n, ok := nodeInterface.(*plan.AddColumn) |
| 34 | if !ok { |
| 35 | return nil, errors.Errorf("ADD COLUMN pre-hook expected `*plan.AddColumn` but received `%T`", nodeInterface) |
| 36 | } |
| 37 | // If the column being added doesn't have a default value, then we don't have anything to check (for now) |
| 38 | if n.Column().Default == nil { |
| 39 | return n, nil |
| 40 | } |
| 41 | |
| 42 | // Grab the table being altered |
| 43 | doltTable := core.SQLNodeToDoltTable(n.Table) |
| 44 | if doltTable == nil { |
| 45 | // If this table isn't a Dolt table then we don't have anything to do |
| 46 | return n, nil |
| 47 | } |
| 48 | _, root, err := core.GetRootFromContext(ctx) |
| 49 | if err != nil { |
| 50 | return n, nil |
| 51 | } |
| 52 | tableName := doltTable.TableName() |
| 53 | tableAsType := id.NewType(tableName.Schema, tableName.Name) |
| 54 | allTableNames, err := root.GetAllTableNames(ctx, false) |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | |
| 59 | for _, otherTableName := range allTableNames { |
| 60 | if doltdb.IsSystemTable(otherTableName) { |
| 61 | // System tables don't use any table types |
| 62 | continue |
| 63 | } |
| 64 | otherTable, ok, err := root.GetTable(ctx, otherTableName) |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | if !ok { |
| 69 | return nil, errors.Errorf("root returned table name `%s` but it could not be found?", otherTableName.String()) |
| 70 | } |
| 71 | otherTableSch, err := otherTable.GetSchema(ctx) |
| 72 | if err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | for _, otherCol := range otherTableSch.GetAllCols().GetColumns() { |
| 76 | colType := otherCol.TypeInfo.ToSqlType() |
| 77 | dgtype, ok := colType.(*pgtypes.DoltgresType) |
| 78 | if !ok { |
| 79 | // If this isn't a Doltgres type, then it can't be a table type so we can ignore it |
| 80 | continue |
| 81 | } |
| 82 | if dgtype.ID != tableAsType { |
| 83 | // This column isn't our table type, so we can ignore it |
| 84 | continue |
| 85 | } |
| 86 | return nil, errors.Errorf(`cannot alter table "%s" because column "%s.%s" uses its row type`, |
| 87 | tableName.Name, otherTableName.Name, otherCol.Name) |
| 88 | } |
| 89 | } |
nothing calls this directly
no test coverage detected