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)
| 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. |
| 44 | func 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. |
| 73 | func BuildColumnSchemaMap( |
nothing calls this directly
no test coverage detected