Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions superset/commands/database/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,21 @@ def add_permissions(database: Database, ssh_tunnel: SSHTunnel | None) -> None:
"""
# TODO: Migrate this to use the non-commiting add_pvm helper instead
if database.db_engine_spec.supports_catalog:
catalogs = database.get_all_catalog_names(
cache=False,
ssh_tunnel=ssh_tunnel,
)
# Adding permissions to all catalogs (and all their schemas) can take a long
# time (minutes, while importing a chart, eg). If the database does not
# support cross-catalog queries (like Postgres), and the multi-catalog
# feature is not enabled, then we only need to add permissions to the
# default catalog.
if (
database.db_engine_spec.supports_cross_catalog_queries
or database.allow_multi_catalog
):
catalogs = database.get_all_catalog_names(
cache=False,
ssh_tunnel=ssh_tunnel,
)
else:
catalogs = {database.get_default_catalog()}
Copy link
Member

Choose a reason for hiding this comment

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

I just realized we probably want to pass the ssh_tunnel to get_default_catalog, since for some DB engine specs we need to make a connection and run a query in order to figure out the default catalog (it can't be inferred from the SQLAlchemy URI). But we can do that in the future when we need it.


for catalog in catalogs:
security_manager.add_permission_view_menu(
Expand Down
31 changes: 31 additions & 0 deletions tests/unit_tests/databases/commands/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,37 @@ def test_add_permissions(mocker: MockerFixture) -> None:
)


def test_add_permissions_get_default_catalog(mocker: MockerFixture):
"""
Test permissions only added to the default catalog if multi-catalog is not enabled.
Important when the database does not support cross-catalog queries (like Postgres).
"""
database = mocker.MagicMock()
database.database_name = "my_db"
database.db_engine_spec.supports_catalog = True
database.db_engine_spec.supports_cross_catalog_queries = False
database.allow_multi_catalog = False
database.get_all_catalog_names.return_value = ["catalog1", "catalog2"]
database.get_default_catalog.return_value = "catalog1"
database.get_all_schema_names.side_effect = [["schema1"], ["schema2"]]
ssh_tunnel = mocker.MagicMock()
add_permission_view_menu = mocker.patch(
"superset.commands.database.importers.v1.utils.security_manager."
"add_permission_view_menu"
)

add_permissions(database, ssh_tunnel)

add_permission_view_menu.assert_has_calls(
[
mocker.call("catalog_access", "[my_db].[catalog1]"),
mocker.call("schema_access", "[my_db].[catalog1].[schema1]"),
]
)

assert add_permission_view_menu.call_count == 2


def test_add_permissions_handle_failures(mocker: MockerFixture) -> None:
"""
Test adding permissions to a database when it's created in case
Expand Down
Loading