Skip to content

fix: detect chart SQL dialect from queue connection#43

Merged
vishaltps merged 1 commit into
mainfrom
fix/chart-multi-db-adapter
Jul 13, 2026
Merged

fix: detect chart SQL dialect from queue connection#43
vishaltps merged 1 commit into
mainfrom
fix/chart-multi-db-adapter

Conversation

@vishaltps

Copy link
Copy Markdown
Owner

Summary

  • Fixes the activity chart 500ing the dashboard on multi-database setups where Solid Queue runs on a different database engine than the host app's primary connection.
  • Detects the SQL dialect from the Solid Queue model's own connection instead of ActiveRecord::Base.

Problem

On a multi-database app with a MySQL primary and a dedicated PostgreSQL queue database (config.solid_queue.connects_to = { database: { writing: :queue } }), the chart's time-bucketing query raised:

PG::UndefinedFunction: ERROR: function unix_timestamp(timestamp without time zone) does not exist
SELECT FLOOR((UNIX_TIMESTAMP(created_at) - ...) / 3600) ...

ChartDataService#adapter? picked the SQL dialect from ActiveRecord::Base.connection — the primary (MySQL) database — so it emitted MySQL UNIX_TIMESTAMP() SQL and then executed it against the Postgres queue database. Because show_chart defaults to true and the chart data is computed inline in OverviewController#index with no rescue, this took down the entire dashboard page for affected users, not just the chart.

Fixes #42.

Solution

Detect the adapter from the model's own connection (SolidQueue::Job.connection), which is the connection the query actually runs on. The model is threaded through bucket_index_expr into adapter?.

For single-database apps and same-engine multi-database apps, SolidQueue::Job.connection resolves to the same adapter as ActiveRecord::Base.connection, so the generated SQL is byte-identical — no behavioral change. Only the previously-crashing mixed-engine path changes.

Changes

  • app/services/solid_queue_monitor/chart_data_service.rb: adapter?(model, name) reads model.connection.adapter_name; bucket_index_expr takes the model.
  • spec/services/solid_queue_monitor/chart_data_service_spec.rb: new adapter-detection specs per engine (Postgres / MySQL / Trilogy / SQLite) plus a regression case reproducing config.show_chart = true fails with postgres #42 (MySQL primary + Postgres queue model → asserts EXTRACT(EPOCH ...), never UNIX_TIMESTAMP).
  • CHANGELOG.md: [Unreleased] → Fixed entry.

Testing

  • Unit tests added (adapter detection + config.show_chart = true fails with postgres #42 regression)
  • Full suite green: 346 examples, 0 failures
  • bundle exec rubocop clean on changed files
  • spec/requests/solid_queue_monitor/csp_compatibility_spec.rb passes
  • Edge cases considered (single-DB and same-engine multi-DB produce identical SQL)

Backward compatibility

Fully backward compatible. Safe to ship as a patch release (2.2.1) — no config, no migration.

Checklist

  • Code follows project patterns
  • Self-review completed
  • Tests pass
  • No debug statements left

The activity chart's time-bucketing query picked its SQL dialect from
ActiveRecord::Base.connection, the host app's primary database. On
multi-database setups where Solid Queue runs on a different engine
(e.g. MySQL primary + dedicated PostgreSQL queue via
config.solid_queue.connects_to), this built MySQL UNIX_TIMESTAMP() SQL
and ran it against Postgres, raising PG::UndefinedFunction and 500ing
the whole dashboard since show_chart defaults to true.

Detect the adapter from the Solid Queue model's own connection instead.
Single-database and same-engine setups emit byte-identical SQL.

Fixes #42
@vishaltps vishaltps merged commit 5ecac99 into main Jul 13, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

config.show_chart = true fails with postgres

1 participant