You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by be...@apache.org on 2023/10/25 18:44:12 UTC

[superset] branch sc-75302 created (now b303c5e828)

This is an automated email from the ASF dual-hosted git repository.

beto pushed a change to branch sc-75302
in repository https://gitbox.apache.org/repos/asf/superset.git


      at b303c5e828 fix; dataset update uniqueness

This branch includes the following new commits:

     new b303c5e828 fix; dataset update uniqueness

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[superset] 01/01: fix; dataset update uniqueness

Posted by be...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

beto pushed a commit to branch sc-75302
in repository https://gitbox.apache.org/repos/asf/superset.git

commit b303c5e828559c273462f6eba16b2f2405cb0903
Author: Beto Dealmeida <ro...@dealmeida.net>
AuthorDate: Wed Oct 25 14:43:20 2023 -0400

    fix; dataset update uniqueness
---
 superset/daos/dataset.py             |  6 +++-
 superset/datasets/commands/update.py |  5 ++-
 tests/unit_tests/dao/dataset_test.py | 70 ++++++++++++++++++++++++++++++++++++
 3 files changed, 79 insertions(+), 2 deletions(-)

diff --git a/superset/daos/dataset.py b/superset/daos/dataset.py
index 8f12fcca93..0a4425dbd7 100644
--- a/superset/daos/dataset.py
+++ b/superset/daos/dataset.py
@@ -99,11 +99,15 @@ class DatasetDAO(BaseDAO[SqlaTable]):
 
     @staticmethod
     def validate_update_uniqueness(
-        database_id: int, dataset_id: int, name: str
+        database_id: int,
+        schema: str | None,
+        dataset_id: int,
+        name: str,
     ) -> bool:
         dataset_query = db.session.query(SqlaTable).filter(
             SqlaTable.table_name == name,
             SqlaTable.database_id == database_id,
+            SqlaTable.schema == schema,
             SqlaTable.id != dataset_id,
         )
         return not db.session.query(dataset_query.exists()).scalar()
diff --git a/superset/datasets/commands/update.py b/superset/datasets/commands/update.py
index af032b709c..8dcc4dfd5f 100644
--- a/superset/datasets/commands/update.py
+++ b/superset/datasets/commands/update.py
@@ -89,7 +89,10 @@ class UpdateDatasetCommand(UpdateMixin, BaseCommand):
         table_name = self._properties.get("table_name", None)
         # Validate uniqueness
         if not DatasetDAO.validate_update_uniqueness(
-            self._model.database_id, self._model_id, table_name
+            self._model.database_id,
+            self._model.schema,
+            self._model_id,
+            table_name,
         ):
             exceptions.append(DatasetExistsValidationError(table_name))
         # Validate/Populate database not allowed to change
diff --git a/tests/unit_tests/dao/dataset_test.py b/tests/unit_tests/dao/dataset_test.py
new file mode 100644
index 0000000000..a38cb2223c
--- /dev/null
+++ b/tests/unit_tests/dao/dataset_test.py
@@ -0,0 +1,70 @@
+# 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.
+
+from sqlalchemy.orm.session import Session
+
+from superset.daos.dataset import DatasetDAO
+
+
+def test_validate_update_uniqueness(session: Session) -> None:
+    """
+    Test the `validate_update_uniqueness` static method.
+
+    In particular, allow datasets with the same name in the same database as long as they
+    are in different schemas
+    """
+    from superset.connectors.sqla.models import SqlaTable
+    from superset.models.core import Database
+
+    SqlaTable.metadata.create_all(session.get_bind())
+
+    database = Database(
+        database_name="my_db",
+        sqlalchemy_uri="sqlite://",
+    )
+    dataset1 = SqlaTable(
+        table_name="my_dataset",
+        schema="main",
+        database=database,
+    )
+    dataset2 = SqlaTable(
+        table_name="my_dataset",
+        schema="dev",
+        database=database,
+    )
+    session.add_all([database, dataset1, dataset2])
+    session.flush()
+
+    assert (
+        DatasetDAO.validate_update_uniqueness(
+            database_id=database.id,
+            schema=dataset1.schema,
+            dataset_id=dataset1.id,
+            name=dataset1.table_name,
+        )
+        is True
+    )
+
+    assert (
+        DatasetDAO.validate_update_uniqueness(
+            database_id=database.id,
+            schema=dataset2.schema,
+            dataset_id=dataset1.id,
+            name=dataset1.table_name,
+        )
+        is False
+    )