Skip to content

DB_CONNECTION_MUTATOR Does Not Switch Database #39225

@rishabhsingh97

Description

@rishabhsingh97

Bug description

Type: Bug / Limitation
Component: Embedded Superset / Multi-tenancy / Database Connection
Version: 6.0.1

Summary
DB_CONNECTION_MUTATOR is documented as a hook to dynamically alter database connection parameters at runtime per user. However, it does not work for per-request tenant database switching.

Expected Behavior
When using embedded Superset with guest tokens, setting username in the guest token payload should allow DB_CONNECTION_MUTATOR to dynamically route each tenant's chart queries to their respective database (e.g., tenant1 → db_tenant1, tenant2 → db_tenant2) using the same dashboard and dataset.

Reproduction Steps
Configure embedded Superset with GUEST_ROLE_NAME and guest token

Add DB_CONNECTION_MUTATOR to config that switches uri.database based on username

def DB_CONNECTION_MUTATOR(uri, params, username, security_manager, source):
    import logging
    logger = logging.getLogger(__name__)

    logger.info("[DB_CONNECTION_MUTATOR] source=%s username=%s original_db=%s is_guest=%s", source, username, uri.database, security_manager.is_guest_user())

    if security_manager.is_guest_user():
        guest_user = security_manager.get_current_guest_user_if_guest()
        uri = uri.set(database=username) # for tenants with separate DBs named after their username
        logger.info("[DB_CONNECTION_MUTATOR] switched DB to: %s", uri.database)
    else:
        logger.info("[DB_CONNECTION_MUTATOR] no DB switch — not a guest user")

    return uri, params

DB_CONNECTION_MUTATOR = DB_CONNECTION_MUTATOR

Generate guest token for username tenant1 → load embedded dashboard → data loads from tenant1 DB ✓
Generate guest token for username tenant2 → load same embedded dashboard → data still loads from tenant1 DB ✗

Screenshots/recordings

Image

Superset version

master / latest-dev

Python version

3.11

Node version

18 or greater

Browser

Chrome

Additional context

No response

Checklist

  • I have searched Superset docs and Slack and didn't find a solution to my problem.
  • I have searched the GitHub issue tracker and didn't find a similar bug report.
  • I have checked Superset's logs for errors and if I found a relevant Python stacktrace, I included it here as text in the "additional context" section.

Metadata

Metadata

Assignees

No one assigned

    Labels

    data:databasesRelated to database configurations and connectionsembeddedvalidation:requiredA committer should validate the issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions