porsager / porsager/postgres

Sql method does not correctly handle strings with parentheses

Open
#1,099 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

Thank you for this project! I was very glad to stumble upon it.

I've got a use case where I need to dynamically create database columns from a json file. This json file contains the names and types of each column. E.g:

[
  { "name": "TEMP", "type": "uint8" }, 
  { "name":  "VOLTAGE", "type": "flt16" }
] 

I then map these types into data types supported by Postgres. E.g. uint8 becomes int2 and flt16 becomes real.

After this, I feed those names and mapped data types into an await sql`ALTER TABLE X ADD COLUMN ${ sql( name ) } ${ sql(mappedType) }` command using postgres.js.

The issue is that one of my types maps to the postgres type bit(4), but postgres.js does not seem to parse this correctly and I get a postgres error: code 42704: type: ''bit(4)'' does not exist. I am suspicious about the double quoting going on here and assume that the sql method does not like to parse strings with parentheses. Escaping the parentheses did not help.

I appreciate any help or thoughts on the issue.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the ALTER TABLE command using sql(name), sql(mappedType), and the bit(4) type, then inspect the sql method entry point. Confirm the quoting behavior and add coverage for this case; done means PostgreSQL accepts the generated statement without treating bit(4) as a quoted type.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, postgres
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.