-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PostgreSQL case sensitivity in listen #530
Conversation
🚀 Deployed on https://67aca8b9ddcf7763593cd668--pglite.netlify.app |
const caseSensitive1 = vi.fn() | ||
await pg.listen('"tesT2"', caseSensitive1) | ||
await pg.query(`NOTIFY "tesT2", 'paYloAd2'`) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to add a tests with spaces in the channel name:
const quotedWithSpaces = vi.fn()
await pg.listen('"Quoted Channel With Spaces"', quotedWithSpaces)
await pg.query(`NOTIFY ""Quoted Channel With Spaces", 'payload1'`)
const unquotedWithSpaces = vi.fn()
await pg.listen('Unquoted Channel With Spaces', unquotedWithSpaces)
// This one may need to throw?
await pg.query(`NOTIFY "Unquoted Channel With Spaces", 'payload1'`)
export function toPostgresName(input: string): string { | ||
let output | ||
if (input.startsWith('"') && input.endsWith('"')) { | ||
// Postgres sensitive case | ||
output = input.substring(1, input.length - 1) | ||
} else { | ||
// Postgres case insensitive - all to lower | ||
output = input.toLowerCase() | ||
} | ||
return output | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fear this breaks channel names with spaces.
You spotted in my original implementation I made a mistake, on the same thread I quoted the channel name, with the worker it was unquoted.
I think it's now apparent we need to decide between two options:
-
Always quote the channel name - this forces it to be case sensitive, but means that channel names with spaces will always work:
pg.listen('Channel With Spaces', ...)
-
Never use quotes and user has to provide them to get the case sensitivity or use spaces:
pg.listen('"Channel With Spaces"', ...)
workspg.listen('Channel With Spaces', ...)
throws an error
I'm not sure what the best option is.
For a reference point, Electric requires the user to provide the quotes to be case sensitive, and defaults to case insensitive.
Thinking about this now, I fear we may have a similar problem in the live.incrementalQuery
api with the key
column argument...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gonna take a moment to have a look at the possibilities. Will let you know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A reason to throw if no quotes provided:
ERROR: syntax error at or near "channel"
LINE 1: LISTEN my channel;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I guess it's not only about spaces, but other characters as well - in an unquoted string.
ERROR: syntax error at or near "&"
LINE 1: LISTEN my&channel;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I think we should take the opportunity to consider what the correct pattern is here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More issues surfaced: if the provided channel name
causes the db engine to throw for whatever reason, there's no cleanup of the #notifyListeners
made in the frontend. This might be the case in other parts of the code as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! thanks @copiltembel
See #529