Introducing a new way to review your PRs.

Read the announcement

bruin-data / dac

DaC is a dashboard-as-code tool. Build interactive dashboards using YAML and JSX. Built-in semantic layer. Get your agents to build standardized, reviewable dashboards.

View on GitHub

20 documents · Updated Nov 23, 2026

docs / cli-data-access

Query and connection commands

The query and connection commands give CLI users a direct path from project configuration to live data. They select an environment and connection from .bruin.yml, send SQL through the Bruin query service, and return either query data or a connection health report. This page is part of the CLI platform; see project lifecycle commands and dashboard CLI operations for the neighboring command groups.

Shared data access context

Configuration and environment selection

The data access commands use the project configuration as their source of truth for environments and connections. home/…/common.go resolves an explicit root --config path or discovers .bruin.yml from the working directory and its parents. The root --environment value selects an environment; without it, the configuration uses its default environment or its only environment when there is exactly one.

Each environment groups named connections by connection type. The commands pass the selected configuration file and environment to the query backend. The backend then lets Bruin apply the connection-specific details instead of making the CLI understand each supported data system.

References: cmd/common.go• L12-59 pkg/…/config.go• L11-29 pkg/…/config.go• L46-99 cmd/root.go• L28-47

Query workflow

Query source selection

The query command accepts one of three input forms: SQL written as command arguments, a SQL file, or a dashboard widget. The command chooses a dashboard widget when both dashboard flags are present. Otherwise it reads the SQL file when --file is set, or joins the remaining command arguments into one SQL request.

The file and inline SQL forms require --connection. A dashboard widget supplies its own connection through the dashboard definition. The command rejects a request when it has no SQL source or when a direct SQL source has no connection.

The following flow shows how the command turns CLI input into one executable request.

flowchart TD
    A[CLI query request] --> B{Input form}
    B -->|dashboard and widget| C[Resolve widget SQL and connection]
    B -->|SQL file| D[Read file and use --connection]
    B -->|SQL arguments| E[Join arguments and use --connection]
    B -->|none| F[Return input error]
    C --> G[Execute through selected backend]
    D --> G
    E --> G
    G --> H[Format result for CLI output]

References: cmd/query.go• L22-106 cmd/common.go• L20-36

Dashboard widget query resolution

A dashboard query gives the CLI a way to run the same data request that powers a dashboard widget. home/…/model.go resolves a named query or inline SQL and applies the widget, query, or dashboard connection in that order. The command loads the named dashboard, validates it, applies its default filters, and selects the matching data-bearing widget.

Widget selection supports a widget name, a tab label in the form Widget / Tab, a unique tab name, or the widget job ID. A widget that contains tabs must be addressed through one of its tabs. If a tab name matches more than one widget, the command reports the ambiguity and shows IDs that can identify the intended tab.

Semantic widgets are different from plain SQL widgets. Their dimensions, metrics, filters, and model references are compiled into SQL before execution. A widget with static inline data has no database request, so it is not a runnable query source for this command. Text, divider, and image widgets also do not produce query jobs.

References: cmd/query.go• L127-159 cmd/query.go• L235-290 cmd/query_test.go• L12-75 pkg/…/model.go• L82-114 pkg/…/model.go• L343-371 pkg/…/model.go• L510-545 pkg/…/api.go• L254-304 pkg/…/semantic.go• L12-50

Query execution

The CLI uses the query.Backend contract to keep command flow separate from connection-specific execution. BruinCLIBackend implements that contract by invoking bruin query with JSON output, the selected connection, the configuration file, and the environment. It converts the returned column metadata and rows into the shared QueryResult shape.

The backend reports command failures and invalid output as query errors. The CLI records whether the request succeeded for telemetry, then returns a user-facing query failed error instead of printing a partial result.

References: pkg/…/backend.go• L5-8 pkg/…/bruin.go• L11-27 pkg/…/bruin.go• L42-64 pkg/…/bruin.go• L103-132 pkg/…/result.go• L3-7 cmd/query.go• L95-106

Query result presentation

Output formats

The query result is a table of named columns and positional rows. The CLI presents that same result in one of three formats so a person can inspect it or another tool can consume it.

FormatMain useBehavior
tableHuman inspectionAligns columns, displays NULL, shortens long cell values, and shows the row count.
jsonProgram useWrites the full result with column metadata and rows as indented JSON.
csvFile exportWrites a header row followed by one CSV record for each result row.

Unknown output values follow the table path. Scripts should set --output json or --output csv explicitly when the format is part of a contract.

References: cmd/query.go• L46-51 cmd/query.go• L161-233 pkg/…/result.go• L3-12

Connection workflow

Connection checks

The connections command checks every connection in the selected environment. It sends a minimal SELECT 1 request to each named connection through the same backend used by query, so a successful check confirms that the configured route can accept a basic query.

The checks run concurrently because they are independent. The command gathers one status for each connection and prints its name, connection type, and a short success or error message. It returns an error after printing the report when at least one check fails.

The following flow shows the health-check path and its all-or-nothing command result.

flowchart TD
    A[connections command] --> B[Load selected environment]
    B --> C[Create one check per configured connection]
    C --> D[Run SELECT 1 concurrently]
    D --> E[Collect name type and status]
    E --> F[Print connection report]
    F --> G{Any failed check?}
    G -->|no| H[Exit successfully]
    G -->|yes| I[Return failure after report]

References: cmd/connections.go• L13-35 cmd/connections.go• L37-68 cmd/connections.go• L70-86 cmd/connections.go• L91-98

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the root configuration and environment flags aligned with the values passed to BruinCLIBackend; a mismatch can query one environment while reporting another in telemetry.
  • Preserve the QueryResult column-and-row shape when changing backend output parsing, because table, JSON, and CSV presentation all consume it.
  • Keep dashboard widget resolution and semantic SQL compilation aligned with the dashboard model contract, especially for default filters and connection inheritance.
  • Keep tab-name matching and widget job IDs aligned with dashboard job creation so a CLI name resolves to the intended query.
  • Keep the SELECT 1 connection probe compatible with every connection type supported by the configured Bruin backend.
  • Preserve the non-zero failure behavior of connections after the status table is printed so automation can detect partial health.

Important failure modes

TriggerConsequenceGuard
.bruin.yml cannot be found or parsedThe command stops before selecting an environment or backend.Pass a valid root --config path or run from a directory below the project configuration.
No environment is named and the config has no default or single environmentConnection lookup fails before a request starts.Set --environment or define default_environment.
Direct SQL has no --connectionThe command rejects the request without executing SQL.Add --connection; dashboard widget queries can supply their own connection.
Dashboard or widget name is missing or invalidWidget SQL is not resolved and no query runs.Use names that exist in the selected dashboard and keep the dashboard valid.
A tab name matches more than one dashboard widgetThe command rejects the request because it cannot select one query.Use the full Widget / Tab label or the reported widget job ID.
Bruin returns a command error or non-JSON outputThe query fails and no partial result is printed.Check the connection, SQL, environment, and Bruin CLI availability.
One or more connection probes failThe report is printed, then connections exits with an error.Fix each reported connection before treating the check as successful.

Key files

FileWhy to read it
cmd/query.goPrimary query workflow from input selection through execution and output formatting.
cmd/query_test.goExamples of dashboard tab names, full labels, IDs, and ambiguity behavior.
cmd/connections.goConnection health workflow and its concurrent probe behavior.
cmd/common.goShared configuration discovery and backend construction used by both commands.
pkg/…/config.goEnvironment and connection lookup rules.
pkg/…/backend.goSmall execution contract that separates CLI flow from data-system behavior.
pkg/…/bruin.goBoundary between DAC and the Bruin query command.
pkg/…/result.goShared shape of returned columns and rows.
pkg/…/model.goDashboard query sources, connection inheritance, and default filters used by widget queries.
pkg/…/api.goShared widget-job resolution and execution behavior used when a CLI query targets a dashboard widget.
pkg/…/semantic.goHigh-level semantic query compilation path for model-backed widgets.