-
Notifications
You must be signed in to change notification settings - Fork 14k
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
fix: Ensure table uniqueness on update #15909
Merged
john-bodley
merged 5 commits into
apache:master
from
john-bodley:john-bodley--table-uniqueness
Aug 2, 2021
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cade3cb
fix: Ensure table uniqueness on update
11965f7
Update models.py
john-bodley feea25d
Update slice.py
john-bodley a2b81bd
Merge branch 'master' into john-bodley--table-uniqueness
john-bodley c08f541
Update datasource_tests.py
john-bodley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
superset/migrations/versions/31b2a1039d4a_drop_tables_constraint.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""drop tables constraint | ||
|
||
Revision ID: 31b2a1039d4a | ||
Revises: ae1ed299413b | ||
Create Date: 2021-07-27 08:25:20.755453 | ||
|
||
""" | ||
|
||
from alembic import op | ||
from sqlalchemy import engine | ||
from sqlalchemy.exc import OperationalError, ProgrammingError | ||
|
||
from superset.utils.core import generic_find_uq_constraint_name | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "31b2a1039d4a" | ||
down_revision = "ae1ed299413b" | ||
|
||
conv = {"uq": "uq_%(table_name)s_%(column_0_name)s"} | ||
|
||
|
||
def upgrade(): | ||
bind = op.get_bind() | ||
insp = engine.reflection.Inspector.from_engine(bind) | ||
|
||
# Drop the uniqueness constraint if it exists. | ||
constraint = generic_find_uq_constraint_name("tables", {"table_name"}, insp) | ||
|
||
if constraint: | ||
with op.batch_alter_table("tables", naming_convention=conv) as batch_op: | ||
batch_op.drop_constraint(constraint, type_="unique") | ||
|
||
|
||
def downgrade(): | ||
|
||
# One cannot simply re-add the uniqueness constraint as it may not have previously | ||
# existed. | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,10 +99,6 @@ def post_assert_metric( | |
return rv | ||
|
||
|
||
def get_table_by_name(name: str) -> SqlaTable: | ||
return db.session.query(SqlaTable).filter_by(table_name=name).one() | ||
|
||
|
||
@pytest.fixture | ||
def logged_in_admin(): | ||
"""Fixture with app context and logged in admin user.""" | ||
|
@@ -132,12 +128,7 @@ def get_nonexistent_numeric_id(model): | |
|
||
@staticmethod | ||
def get_birth_names_dataset() -> SqlaTable: | ||
example_db = get_example_database() | ||
return ( | ||
db.session.query(SqlaTable) | ||
.filter_by(database=example_db, table_name="birth_names") | ||
.one() | ||
) | ||
return SupersetTestCase.get_table(name="birth_names") | ||
|
||
@staticmethod | ||
def create_user_with_roles( | ||
|
@@ -254,13 +245,31 @@ def get_slice( | |
return slc | ||
|
||
@staticmethod | ||
def get_table_by_name(name: str) -> SqlaTable: | ||
return get_table_by_name(name) | ||
def get_table( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cleanup to ensure consistency. |
||
name: str, database_id: Optional[int] = None, schema: Optional[str] = None | ||
) -> SqlaTable: | ||
return ( | ||
db.session.query(SqlaTable) | ||
.filter_by( | ||
database_id=database_id | ||
or SupersetTestCase.get_database_by_name("examples").id, | ||
schema=schema, | ||
table_name=name, | ||
) | ||
.one() | ||
) | ||
|
||
@staticmethod | ||
def get_database_by_id(db_id: int) -> Database: | ||
return db.session.query(Database).filter_by(id=db_id).one() | ||
|
||
@staticmethod | ||
def get_database_by_name(database_name: str = "main") -> Database: | ||
if database_name == "examples": | ||
return get_example_database() | ||
else: | ||
raise ValueError("Database doesn't exist") | ||
|
||
@staticmethod | ||
def get_druid_ds_by_name(name: str) -> DruidDatasource: | ||
return db.session.query(DruidDatasource).filter_by(datasource_name=name).first() | ||
|
@@ -340,12 +349,6 @@ def revoke_role_access_to_table(self, role_name, table): | |
): | ||
security_manager.del_permission_role(public_role, perm) | ||
|
||
def _get_database_by_name(self, database_name="main"): | ||
if database_name == "examples": | ||
return get_example_database() | ||
else: | ||
raise ValueError("Database doesn't exist") | ||
|
||
def run_sql( | ||
self, | ||
sql, | ||
|
@@ -364,7 +367,7 @@ def run_sql( | |
if user_name: | ||
self.logout() | ||
self.login(username=(user_name or "admin")) | ||
dbid = self._get_database_by_name(database_name).id | ||
dbid = SupersetTestCase.get_database_by_name(database_name).id | ||
json_payload = { | ||
"database_id": dbid, | ||
"sql": sql, | ||
|
@@ -448,7 +451,7 @@ def validate_sql( | |
if user_name: | ||
self.logout() | ||
self.login(username=(user_name if user_name else "admin")) | ||
dbid = self._get_database_by_name(database_name).id | ||
dbid = SupersetTestCase.get_database_by_name(database_name).id | ||
resp = self.get_json_resp( | ||
"/superset/validate_sql_json/", | ||
raise_on_error=False, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
grammar nit:
...is MySQL and PostgreSQL