Skip to content
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

Fixes #35767 to allow for view creation into specified schema from [ Create a view ] in DBManager #58976

Merged
merged 4 commits into from
Oct 17, 2024

Conversation

gisn8
Copy link
Contributor

@gisn8 gisn8 commented Oct 4, 2024

Fixes #35767 to allow for view creation into specified schema from [ Create a view ] in DBManager

Description

Currently, using [ Create a view ] from DBManager for postgres automatically loads the view into the public schema named the entire string. It is reasonable for the user to assume that specifying the schema name as schema.view will create the view into the schema specified. Instead, it's creates a view called schema.view in the public schema. This is generally warned against.

Tested

In order to meet users' expectations, allowed for testing if the string has a "." in the name and to separate the string into two parts to feed into the quoteId which is already set up to accept schema and viewname as a list to process appropriately.

Recommended documentation:
"The Create a View button opens a dialog where you can enter the name for the new view. By default, the view is created in the public schema of the PostgreSQL database. However, you can specify a different schema by using the schema.viewname naming convention."

Backporting is recommended to meet user expectations.

Fixes #35767 to allow for view creation into specified schema from [ Create a view ] in DBManager

@github-actions github-actions bot added this to the 3.40.0 milestone Oct 4, 2024
Comment on lines 1017 to 1023
user_input = view

if '.' in user_input: # To allow view creation into specified schema
schema, view_name = user_input.split('.')
sql = "CREATE VIEW %s AS %s" % (self.quoteId([schema, view_name]), query)
else: # No schema specified; uses public
sql = "CREATE VIEW %s AS %s" % (self.quoteId(view), query)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be hardened a little -- eg if the view name was entered badly with two periods, then we'll get a raw python exception here. How about:

Suggested change
user_input = view
if '.' in user_input: # To allow view creation into specified schema
schema, view_name = user_input.split('.')
sql = "CREATE VIEW %s AS %s" % (self.quoteId([schema, view_name]), query)
else: # No schema specified; uses public
sql = "CREATE VIEW %s AS %s" % (self.quoteId(view), query)
view_name_parts = view.split('.')
if len(view_name_parts) == 2: # To allow view creation into specified schema
schema, view_name = view_name_parts
sql = "CREATE VIEW %s AS %s" % (self.quoteId([schema, view_name]), query)
else: # No specific schema specified
sql = "CREATE VIEW %s AS %s" % (self.quoteId(view), query)

I've updated the code as recommended and added an error message should there there be too many periods. Thanks for the suggestion!
Copy link

github-actions bot commented Oct 8, 2024

🪟 Windows builds

Download Windows builds of this PR for testing.
Debug symbols for this build are available here.
(Built from commit 9314b27)

🪟 Windows Qt6 builds

Download Windows Qt6 builds of this PR for testing.
(Built from commit 9314b27)

Thanks! Much more user-friendly.

Co-authored-by: Nyall Dawson <[email protected]>
@nyalldawson nyalldawson merged commit 45d40ad into qgis:master Oct 17, 2024
29 checks passed
@gisn8 gisn8 deleted the allow_schema_on_create_view branch October 21, 2024 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DB manager: impossible to create a view in another schema than public
2 participants