Repository: apache/superset
File: superset/commands/database/utils.py (Python)

Code before:
        # SQLite can't run on a separate thread, so ``utils.timeout`` fails
        # RuntimeError catches the equivalent error from duckdb.
        return engine.dialect.do_ping(engine)


def add_permissions(database: Database, ssh_tunnel: SSHTunnel | None) -> None:
    """
    Add DAR for catalogs and schemas.
    """
    # 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,
        )

        for catalog in catalogs:
            security_manager.add_permission_view_menu(
                "catalog_access",
                security_manager.get_catalog_perm(
                    database.database_name,
                    catalog,
                ),
            )
    else:
        catalogs = [None]

    for catalog in catalogs:
        try:
            for schema in database.get_all_schema_names(
                catalog=catalog,
                cache=False,
                ssh_tunnel=ssh_tunnel,
            ):
                security_manager.add_permission_view_menu(
                    "schema_access",
                    security_manager.get_schema_perm(
                        database.database_name,
                        catalog,
                        schema,
                    ),
                )
        except GenericDBException:  # pylint: disable=broad-except
            logger.warning("Error processing catalog '%s'", catalog)
            continue


def add_vm(
    session: Session,
    security_manager: SupersetSecurityManager,
    view_menu_name: str | None,

Review comment (suggestion):
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.

Code after:
        # SQLite can't run on a separate thread, so ``utils.timeout`` fails
        # RuntimeError catches the equivalent error from duckdb.
        return engine.dialect.do_ping(engine)


def add_permissions(database: Database, ssh_tunnel: SSHTunnel | None) -> None:
    """
    Add DAR for catalogs and schemas.
    """
    # TODO: Migrate this to use the non-commiting add_pvm helper instead
    if database.db_engine_spec.supports_catalog:
        # 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()}

        for catalog in catalogs:
            security_manager.add_permission_view_menu(
                "catalog_access",
                security_manager.get_catalog_perm(
                    database.database_name,
                    catalog,
                ),
            )
    else:
        catalogs = [None]

    for catalog in catalogs:
        try:
            for schema in database.get_all_schema_names(
                catalog=catalog,
                cache=False,
                ssh_tunnel=ssh_tunnel,
            ):
                security_manager.add_permission_view_menu(
                    "schema_access",
                    security_manager.get_schema_perm(
                        database.database_name,
                        catalog,
                        schema,