use current_schema for postgres TableExists, if schema is unspecified#884
Conversation
| return fmt.Sprintf(q, schemaName, tableName) | ||
| } | ||
| q := `SELECT EXISTS ( SELECT 1 FROM pg_tables WHERE tablename = '%s' )` | ||
| q := `SELECT EXISTS ( SELECT 1 FROM pg_tables WHERE schemaname = current_schema() AND tablename = '%s' )` |
There was a problem hiding this comment.
If we go down this route, then we should also handle the edge case where current_schema () returns NULL.
Returns the name of the schema that is first in the search path (or a null value if the search path is empty). This is the schema that will be used for any tables or other named objects that are created without specifying a target schema.
E.g., SET search_path TO '';
So the suggestion is to update the predicate to:
- AND schemaname = current_schema()
+ AND (current_schema() IS NULL OR schemaname = current_schema())What do you think?
There was a problem hiding this comment.
I'm all for being defensive here.
Updated the PR.
In practice, though, you get a [3F000] ERROR: no schema has been selected to create in when trying to create a table and current_schema() is NULL, so the later steps would fail.
|
lgtm, thanks for your input! |
This will check if the table exists in the schema where it will otherwise be created.
The current solution will fail if the table exists in another schema