sadpandajoe commented on code in PR #43499:
URL: https://github.com/apache/superset/pull/43499#discussion_r4366506616


##########
superset/models/sql_lab.py:
##########
@@ -635,6 +716,41 @@ def _last_run_delta_humanized(self) -> str:
     def last_run_delta_humanized(self) -> str:
         return self._last_run_delta_humanized
 
+    def load_database(self) -> None:
+        """Reload ``self.database`` from ``db_id`` if it's out of sync.
+
+        Mirrors ``SqlaTable.load_database``: needed in ``before_update``
+        because ``db_id`` may have just changed in-memory while the
+        ``database`` relationship still points at the previously loaded row.
+        """
+        # pylint: disable=import-outside-toplevel
+        from superset.models.core import Database
+
+        if self.db_id and (not self.database or self.database.id != 
self.db_id):
+            session = sqla.inspect(self).session  # pylint: 
disable=disallowed-name
+            self.database = 
session.query(Database).filter_by(id=self.db_id).one()
+
+    @staticmethod
+    def before_update(
+        mapper: "Mapper",
+        connection: "Connection",
+        target: "SavedQuery",
+    ) -> None:
+        target.load_database()
+        security_manager.saved_query_before_update(mapper, connection, target)
+
+    @staticmethod
+    def after_delete(
+        mapper: "Mapper",
+        connection: "Connection",
+        target: "SavedQuery",
+    ) -> None:
+        security_manager.saved_query_after_delete(mapper, connection, target)
+
+
+sqla.event.listen(SavedQuery, "before_update", SavedQuery.before_update)

Review Comment:
   `UpdateDatabaseCommand._update_catalog_attribute()` rewrites 
`SavedQuery.catalog` with a bulk 
`db.session.query(SavedQuery).filter_by(...).update(...)`, which never fires 
this mapper `before_update` listener, and `database_after_update` only resyncs 
saved-query charts on a database rename. So when a single-catalog connection's 
default catalog changes from A to B (same display name, multi-catalog off), the 
saved queries move to B while their charts keep A's 
`catalog_perm`/`schema_perm`, and a non-owner granted only B's schema can't see 
those charts. Could the catalog-change path resync the dependent charts too?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to