Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #3619
Notes
I went around in circles a bit with this PR, which is why it took a while. I'll describe my thought process chronologically in hopes that it might be useful as these same problems could potentially apply to other DB objects as well...
First I proceeded to remove the
if_not_exists
parameter frommsar.create_schema
because I didn't want the additional complexity in there. I wanted to default to not usingIF NOT EXISTS
for the sake of simplicity. This is in line with comments in this meeting (starting at 21:10) on which @mathemancer and @Anish9901 were supportive. I wanted to raise an exception if the supplied schema name already existed.But then I noticed that our codebase actually made use of that
if_not_exists
parameter in a few places for internal schemas and creating ephemeral schemas on the fly for type inference. Ugh!My initial instinct was to modify the IF NOT EXISTS code locations by resorting to calling raw SQL from within the service layer (instead of calling functions). I had a tricky time figuring out how to do that cleanly with SQLAlchemy. I imagine it's possible, but it didn't seem to fit our patterns, and I didn't want to fiddle with it too much.
After some hemming and hawing, I decided to retain support for
if_not_exists
, inmsar.create_schema
while also adding support for setting descriptions on schemas within the same function. This choice sent me down a rabbit hole, making me wish I had trusted my early intuition onif_not_exists
more strongly.The
msar.create_schema
function got more and more complex as I discovered more edge cases and inconsistencies. If it can return an existing schema, then we need to make sure thatNULL
description values don't overwrite existing description. Ok fine. Then for the front end's sake, we should actually be fetching and returning the description. Ok. But that means the return value gets much more complex. It should be an object instead of a simple oid. And if it's an object, it ought to match the structure of the objects returned frommsar.get_schemas
, meaning it should supply atable_count
property too. Ugh. So I started modifyingmsar.get_schemas
to accept asch_oid
filter parameter which would allow me to compose that function to reliably get the full schema details in a consistent manner. But then I just threw up my hands and said, "this is ridiculous!"So I decided to split the function into two:
msar.create_schema_if_not_exists(sch_name text)
and
msar.create_schema(sch_name text, description text DEFAULT '')
This means you can use
IF NOT EXISTS
OR you can supply a description — but you can't do both at the same time. And at the API layer there's no way to useIF NOT EXISTS
.This approach has some limitations. But compared to cramming all that logic in one function, it's much simpler and less prone to weird edge cases and bugs.
Checklist
Update index.md
).develop
branch of the repositoryDeveloper Certificate of Origin
Developer Certificate of Origin