|
| 1 | +// Copyright 2026 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 hook |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/cockroachdb/errors" |
| 21 | + "github.com/dolthub/dolt/go/libraries/doltcore/doltdb" |
| 22 | + "github.com/dolthub/go-mysql-server/sql" |
| 23 | + "github.com/dolthub/go-mysql-server/sql/plan" |
| 24 | + |
| 25 | + "github.com/dolthub/doltgresql/core" |
| 26 | + "github.com/dolthub/doltgresql/core/id" |
| 27 | + pgtypes "github.com/dolthub/doltgresql/server/types" |
| 28 | +) |
| 29 | + |
| 30 | +// AfterTableRename handles updating various columns using the table type, alongside other validation that's unique |
| 31 | +// to Doltgres. |
| 32 | +func AfterTableRename(ctx *sql.Context, runner sql.StatementRunner, nodeInterface sql.Node) error { |
| 33 | + n, ok := nodeInterface.(*plan.RenameTable) |
| 34 | + if !ok { |
| 35 | + return errors.Errorf("RENAME TABLE post-hook expected `*plan.RenameTable` but received `%T`", nodeInterface) |
| 36 | + } |
| 37 | + |
| 38 | + // Grab the table being altered (so we know the schema) |
| 39 | + sqlTable, ok := n.TableExists(ctx, n.NewNames[0]) |
| 40 | + if !ok { |
| 41 | + // Views do not manifest as tables, so we'll return here if this isn't a table |
| 42 | + return nil |
| 43 | + } |
| 44 | + doltTable := core.SQLTableToDoltTable(sqlTable) |
| 45 | + if doltTable == nil { |
| 46 | + // If this table isn't a Dolt table then we don't have anything to do |
| 47 | + return nil |
| 48 | + } |
| 49 | + _, root, err := core.GetRootFromContext(ctx) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + tableName := doltTable.TableName() |
| 54 | + tableName.Name = n.OldNames[0] |
| 55 | + tableAsType := id.NewType(tableName.Schema, tableName.Name) |
| 56 | + allTableNames, err := root.GetAllTableNames(ctx, false) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + for _, otherTableName := range allTableNames { |
| 62 | + if doltdb.IsSystemTable(otherTableName) { |
| 63 | + // System tables don't use any table types |
| 64 | + continue |
| 65 | + } |
| 66 | + otherTable, ok, err := root.GetTable(ctx, otherTableName) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + if !ok { |
| 71 | + return errors.Errorf("root returned table name `%s` but it could not be found?", otherTableName.String()) |
| 72 | + } |
| 73 | + otherTableSch, err := otherTable.GetSchema(ctx) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + for _, otherCol := range otherTableSch.GetAllCols().GetColumns() { |
| 78 | + colType := otherCol.TypeInfo.ToSqlType() |
| 79 | + dgtype, ok := colType.(*pgtypes.DoltgresType) |
| 80 | + if !ok { |
| 81 | + // If this isn't a Doltgres type, then it can't be a table type so we can ignore it |
| 82 | + continue |
| 83 | + } |
| 84 | + if dgtype.ID != tableAsType { |
| 85 | + // This column isn't our table type, so we can ignore it |
| 86 | + continue |
| 87 | + } |
| 88 | + // The ALTER updates the type on the schema since it still has the old one |
| 89 | + alterStr := fmt.Sprintf(`ALTER TABLE "%s"."%s" ALTER COLUMN "%s" TYPE "%s"."%s";`, |
| 90 | + otherTableName.Schema, otherTableName.Name, otherCol.Name, tableName.Schema, n.NewNames[0]) |
| 91 | + // We run the statement as though it's interpreted since we're running new statements inside the original |
| 92 | + _, err = sql.RunInterpreted(ctx, func(subCtx *sql.Context) ([]sql.Row, error) { |
| 93 | + _, rowIter, _, err := runner.QueryWithBindings(subCtx, alterStr, nil, nil, nil) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + return sql.RowIterToRows(subCtx, rowIter) |
| 98 | + }) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + return nil |
| 105 | +} |
0 commit comments