mikebridge commented on code in PR #45100:
URL: https://github.com/apache/superset/pull/45100#discussion_r4235493737


##########
superset/semantic_layers/models.py:
##########
@@ -166,6 +168,55 @@ class ColumnMetadata:
     extra: str | None = None
 
 
+_METADATA_IDENTITY_KEY: str = "semantic_layer_cache_identity"
+
+
+@dataclass
+class _MetadataCacheIdentity:
+    """Session-local identities, captured before discovery and forgotten on 
Save."""
+
+    tokens: dict[uuid.UUID, str] = field(default_factory=dict)
+
+    @classmethod
+    def for_session(cls, session: Session) -> _MetadataCacheIdentity:
+        """Keep the memo's storage convention in one place."""
+        if _METADATA_IDENTITY_KEY not in session.info:
+            session.info[_METADATA_IDENTITY_KEY] = cls()
+        return cast(_MetadataCacheIdentity, 
session.info[_METADATA_IDENTITY_KEY])
+
+    def capture(self, layer: SemanticLayer, session: Session) -> str:
+        """Web, task and MCP operations share a credential-independent 
identity."""
+        if layer.uuid not in self.tokens:
+            namespace: str | Callable[[], str] = current_app.config.get(
+                "SEMANTIC_LAYER_CACHE_NAMESPACE", ""
+            )
+            if callable(namespace):
+                namespace = namespace()
+            if not isinstance(namespace, str):
+                raise TypeError(
+                    "SEMANTIC_LAYER_CACHE_NAMESPACE must resolve to a string"
+                )
+            url: sa.engine.URL = 
session.get_bind(mapper=SemanticLayer).engine.url

Review Comment:
   Not a real crash path: on SQLAlchemy 2.0, `Engine.engine` is a property that 
returns the engine itself, and `Connection.engine` exists too, so `.engine.url` 
works for whichever one `get_bind()` returns. I checked this on 2.0.52 with 
Flask-SQLAlchemy 3.1.1, and the tests in `simple_cache_clear_test.py` that use 
a real session bind pass.



-- 
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