MCPcopy
hub / github.com/PeerDB-io/peerdb / GetDestinationTableSchema

Function GetDestinationTableSchema

flow/pkg/postgres/dest_validation.go:44–70  ·  view source on GitHub ↗

GetDestinationTableSchema queries a PostgreSQL table's column type information using SELECT * FROM tableIdentifier LIMIT 0 to get field descriptions with OIDs. Returns pgx.ErrNoRows if the table exists but has no columns.

(ctx context.Context, conn *pgx.Conn, tableIdentifier string)

Source from the content-addressed store, hash-verified

42// using SELECT * FROM tableIdentifier LIMIT 0 to get field descriptions with OIDs.
43// Returns pgx.ErrNoRows if the table exists but has no columns.
44func GetDestinationTableSchema(ctx context.Context, conn *pgx.Conn, tableIdentifier string) (map[string]ColumnSchema, error) {
45 parsedTable, err := common.ParseTableIdentifier(tableIdentifier)
46 if err != nil {
47 return nil, fmt.Errorf("invalid table identifier %s: %w", tableIdentifier, err)
48 }
49
50 customTypeMapping, err := GetCustomDataTypes(ctx, conn)
51 if err != nil {
52 return nil, fmt.Errorf("failed to fetch custom type mapping: %w", err)
53 }
54
55 rows, err := conn.Query(ctx,
56 "SELECT * FROM "+parsedTable.String()+" LIMIT 0",
57 pgx.QueryExecModeSimpleProtocol,
58 )
59 if err != nil {
60 return nil, err
61 }
62 fields := slices.Clone(rows.FieldDescriptions())
63 rows.Close()
64
65 if len(fields) == 0 {
66 return nil, pgx.ErrNoRows
67 }
68
69 return BuildColumnSchemaMap(fields, conn.TypeMap(), customTypeMapping)
70}
71
72// BuildColumnSchemaMap assembles a ColumnSchema map from pgx FieldDescriptions.
73func BuildColumnSchemaMap(

Callers

nothing calls this directly

Calls 6

GetCustomDataTypesFunction · 0.85
BuildColumnSchemaMapFunction · 0.85
QueryMethod · 0.65
FieldDescriptionsMethod · 0.65
CloseMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected