engineering rails

Catch the migration that breaks your monitors, in your Rails app's CI

Keep the queries of your Pulso monitors in the repo and check them against the schema on every pull request: offline, without an API key in CI and without reading a row.

Pulso · November 22, 2026

Your monitors are SQL against your production database. Your developers change that database every week. Sooner or later a migration renames users.name to users.full_name, the tests pass, the deploy goes out, and the “New signups” monitor starts failing. Nobody notices until someone asks why there were no signups on Tuesday.

The developer who wrote the migration had no way to know. The monitor lives in Pulso, not in the repo.

So put it in the repo. This post shows how we do it in a Rails app (Pulso itself, as it happens): about 40 lines of Rake, one step in CI, and the pull request that breaks a monitor goes red before it merges.

The idea#

  1. Download the queries of your monitors into a file in the repo, db/pulso.json.
  2. In CI, after the schema is loaded, ask PostgreSQL to prepare each query, never to run it. Preparing parses and plans the query, so PostgreSQL resolves every table, column and type, and answers with the columns the query would return. It reads no rows, so an empty test database is enough.
  3. Fail if a query no longer prepares, or if it no longer returns a column the monitor uses in its filters, message, playbook or row identifiers.
  4. Fail too if the file is older than the latest migration. Whoever changes the schema downloads the queries again, so the check always runs against the monitors as they are today.

CI never talks to Pulso: no network, no API key in your CI secrets, nothing about your production data leaves Pulso except the queries themselves.

1. Download the queries#

Create an API key with only the data_sources:read_queries permission, under API keys in the sidebar. Then copy the queries URL from the Queries API section of the data source: it looks like https://pulso.run/api/v1/data_sources/<id>/queries, and the API docs describe what it returns.

The file holds each monitor’s SQL, whether it is active, the fields it uses, and exported_at, the moment it was downloaded:

{
  "data_source": { "id": "…", "name": "Production", "type": "postgres" },
  "exported_at": "2026-11-22T14:52:48Z",
  "queries": [
    {
      "kind": "monitor",
      "name": "New signups",
      "active": true,
      "sql": "select id, email, name, created_at from users where created_at > now() - interval '1 day'",
      "params": [],
      "fields": [
        { "name": "email", "used_in": ["message", "playbook"], "ambiguous": false },
        { "name": "id", "used_in": ["row_identifier"], "ambiguous": false }
      ]
    }
  ]
}

2. The Rake task#

Save this as lib/tasks/pulso.rake:

require "net/http"

namespace :pulso do
  desc "Download the queries of Pulso's monitors into db/pulso.json"
  task :download_queries do
    uri = URI(ENV.fetch("PULSO_QUERIES_URL"))
    request = Net::HTTP::Get.new(uri, "Authorization" => "Bearer #{ENV.fetch("PULSO_API_KEY")}")
    response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(request) }
    response.value # raises unless it's a 2xx
    File.write("db/pulso.json", JSON.pretty_generate(JSON.parse(response.body)) + "\n")
  end

  desc "Check db/pulso.json's queries against the database's schema, without reading rows"
  task check_queries: :environment do
    export = JSON.parse(File.read("db/pulso.json"))

    ActiveRecord::Base.with_connection do |connection|
      latest = connection.pool.migration_context.current_version.to_s
      if Time.iso8601(export["exported_at"]) < Time.utc(*latest.unpack("a4a2a2a2a2a2").map(&:to_i))
        abort "db/pulso.json is older than the latest migration: run pulso:download_queries"
      end

      monitors = export["queries"].select { |query| query["kind"] == "monitor" && query["active"] }
      failures = monitors.count do |query|
        params = "(#{query["params"].map { "unknown" }.join(", ")})" if query["params"].any?
        connection.transaction(requires_new: true) do
          connection.execute("PREPARE pulso_check#{params} AS SELECT * FROM (\n#{query["sql"]}\n) AS q LIMIT 0")
          columns = connection.raw_connection.describe_prepared("pulso_check").fields
          connection.execute("DEALLOCATE pulso_check")

          used = query["fields"].reject { |field| field["ambiguous"] }.map { |field| field["name"].split(".").first }
          missing = used.uniq - columns
          puts "FAIL #{query["name"]}: no #{missing.join(", ")} in its result" if missing.any?
          missing.any?
        end
      rescue ActiveRecord::StatementInvalid => error
        puts "FAIL #{query["name"]}: #{error.cause.message.strip}"
        true
      end

      abort "#{failures} of #{monitors.size} monitors would break" if failures.positive?
      puts "#{monitors.size} monitors checked"
    end
  end
end

A few details worth knowing:

Download the queries once, and commit the file:

PULSO_QUERIES_URL=https://pulso.run/api/v1/data_sources/<id>/queries \
PULSO_API_KEY=… bin/rails pulso:download_queries
git add db/pulso.json

Keep the key in your password manager and pass it to the command; it never needs to reach CI.

3. The CI step#

Run the check after the schema is loaded into the test database. In GitHub Actions, with RAILS_ENV=test:

      - name: Run tests
        run: bin/rails db:test:prepare test

      - name: Check Pulso's monitors against the schema
        if: ${{ !cancelled() }}
        run: bin/rails pulso:check_queries

if: ${{ !cancelled() }} runs the check even when the tests fail: a migration that breaks the app usually breaks a monitor too, and you want to see both.

What a broken monitor looks like#

The pull request that renames the column now fails like this:

FAIL New signups: ERROR:  column "name" does not exist
LINE 2: select id, email, name, created_at from users where created_...
                          ^
1 of 1 monitors would break

And one that keeps the query valid but stops returning a column the monitor’s message uses:

FAIL New signups: no name in its result
1 of 1 monitors would break

The developer fixes the migration, or updates the monitor in Pulso, downloads the queries again and pushes. Either way, the monitor is fixed before the deploy, not after Tuesday.

Not using Rails?#

The same check works from any CI as a shell script with curl, jq and psql, fetching the queries at check time instead of keeping them in the repo. It’s in the docs, under Check a schema change in CI.


Ready to get alerted when something actually happens to your business?

See how Pulso works