Module: Collavre::IndexedJsonColumns

Extended by:
ActiveSupport::Concern
Included in:
Channel
Defined in:
app/models/concerns/collavre/indexed_json_columns.rb

Overview

Keeps denormalized real columns in lockstep with keys stored inside a JSON column, so database indexes can be defined over plain columns instead of JSON expressions.

WHY: A JSON-expression index serializes per-adapter -- json_extract(config, '$.x') on SQLite vs config->>'x' on PostgreSQL. Because schema.rb is dumped from the SQLite dev DB, it carries the json_extract form, which is not a PostgreSQL function, so db:schema:load crashes on the prod PostgreSQL database. Promote the JSON key to a real column (which indexes and dumps identically on both adapters) and keep it synced from the JSON column on every save. The JSON column stays the source of truth; the promoted columns are re-derived on each save.

Usage:

include Collavre::IndexedJsonColumns

indexed_json_columns json: :config, columns: {
repo_full_name: "repo_full_name",
pr_number:      "pr_number",
}

json: names the JSON attribute (source of truth). columns: maps each promoted column to the JSON key it mirrors. A before_save re-derives every promoted column from the JSON attribute so the plain-column indexes enforce the same guarantees the JSON-expression indexes did.