State & Config Flow
Relevant source files
The following files were used as context for generating this wiki page:
The State & Config Flow in superset-ext manages the transformation of Meltano environment variables into valid Apache Superset configurations. This system ensures that Superset can run within the Meltano ecosystem by dynamically generating a superset_config.py file and preparing the execution environment with necessary paths and database URIs.
This module is responsible for bridging the gap between Meltano's plugin configuration (defined in meltano.yml) and Superset's requirement for a Python-based configuration script and specific environment variables like FLASK_APP and SUPERSET_CONFIG_PATH.
Sources: superset_ext/extension.py:30-40, superset_ext/utils.py:65-98, README.md:27-39
Configuration Extraction and Transformation
The extension extracts configuration by filtering environment variables that start with a specific prefix (SUPERSET_). It then performs several transformations to map these variables to Superset-specific keys.
Environment Preparation Logic
The prepared_env function serves as the primary engine for state preparation. It performs the following operations:
- Prefix Trimming: Environment variables starting with
SUPERSET_are collected and the prefix is removed for internal mapping. - Path Resolution: It dynamically constructs default paths for
SUPERSET_HOMEandSUPERSET_CONFIG_PATHusing theMELTANO_PROJECT_ROOTenvironment variable. - Variable Mapping: Standard Meltano config keys like
HOMEandCONFIG_PATH(when prefixed) are mapped toSUPERSET_HOMEandSUPERSET_CONFIG_PATH. - Default Assignment: It ensures critical variables like
FLASK_APP(defaulting to "superset") andSQLALCHEMY_DATABASE_URIare present.
Sources: superset_ext/utils.py:41-63, superset_ext/utils.py:65-98
The following diagram illustrates the flow from system environment variables to the prepared environment dictionary:
Sources: superset_ext/utils.py:65-98
Configuration File Generation
A key component of the state flow is the generation of a physical superset_config.py file. Superset requires this file for its runtime configuration.
Writing the Config Script
The write_config utility generates a Python script that injects configuration values into the local module scope. Only uppercase keys from the configuration dictionary are exported as attributes, adhering to Superset's configuration requirements.
| Step | Action | Description |
|---|---|---|
| Check Existence | path.exists() | Checks if the config file already exists to avoid overwriting unless force=True. |
| Directory Setup | os.makedirs | Ensures the parent directory (e.g., analyze/superset/) exists. |
| Script Construction | List Join | Builds a Python script that iterates through the config dict and calls setattr(module, key, value). |
| File I/O | write() | Persists the generated script to the path defined by SUPERSET_CONFIG_PATH. |
Sources: superset_ext/utils.py:101-143, superset_ext/extension.py:42-49
Initialization State Flow
The initialize command orchestrates the transition from a raw environment to a ready-to-use Superset instance. This sequence involves both file system state changes and database migrations.
Persistence and Directory Structure
The state of the extension is largely defined by the following filesystem locations:
- Config Path: Defined by
SUPERSET_CONFIG_PATH, typically$MELTANO_PROJECT_ROOT/analyze/superset/superset_config.py. - Home Directory: Defined by
SUPERSET_HOME, typically$MELTANO_PROJECT_ROOT/.meltano/utilities/superset. - Database: Usually a SQLite instance located at
$SUPERSET_HOME/superset.db.
Sources: superset_ext/utils.py:80-95, README.md:21-25
Key Configuration Mapping
The following table describes how Meltano environment variables are mapped into the Superset configuration state.
| Meltano Environment Variable | Internal Key | Superset Configuration / Purpose |
|---|---|---|
SUPERSET_HOME | SUPERSET_HOME | Primary data directory for Superset state. |
SUPERSET_CONFIG_PATH | SUPERSET_CONFIG_PATH | Path where the Python config file is written. |
SUPERSET_SQLALCHEMY_DATABASE_URI | SQLALCHEMY_DATABASE_URI | Connection string for the Superset metadata database. |
SUPERSET_ROW_LIMIT | ROW_LIMIT | Example of a pass-through Superset configuration value. |
SUPERSET_SECRET_KEY | SECRET_KEY | Flask secret key required for session signing. |
Sources: superset_ext/utils.py:65-98, README.md:18-27, tests/test_superset_ext_utils.py:35-56
Summary of Configuration Handling
The Superset class initializes its configuration state during instantiation by calling prepared_env(ENV_PREFIX). It specifically filters out keys starting with EXT_ to ensure that extension-specific settings (used by the Meltano EDK) do not bleed into the actual Superset application configuration. This filtered dictionary is then used as the source of truth for both the environment variables passed to the superset subprocess and the content of the superset_config.py file.
Sources: superset_ext/extension.py:30-40, superset_ext/utils.py:41-63