cardinal

Fixtures and patches

Ship records with an app, add fields to other apps' RecTypes, and migrate data between versions.

Fixtures

A fixture is a record the app ships. List the RecType in the manifest's fixtures, in the order the installer should write them, and put the records in fixtures/<rectype in snake case>.yaml.

crm/app.exs
fixtures: ["Custom Field", "Property Setter", "CRM Lead Status", "CRM Deal Status"]
crm/fixtures/crm_lead_status.yaml
- {lead_status: New, type: Open, color: gray, position: 1}
- {lead_status: Contacted, type: Ongoing, color: orange, position: 2}
- {lead_status: Qualified, type: Won, color: green, position: 4}

The installer upserts fixtures on install and on every migrate. A record lists only the values that differ from the RecType's defaults. The RecType must name records the same way on every sync, by field:<fieldname> or prompt (the record then carries name).

A missing file, or a file the manifest does not list, fails the build.

Fields on another app's RecType

fixtures/custom_field.yaml adds fields to any RecType of core or of an app this one requires. Each record is named <RecType>-<fieldname> and needs no custom_ prefix.

crm/fixtures/custom_field.yaml
- {parent_rectype: Contact, fieldname: salutation, fieldtype: Link, label: Salutation, options: Salutation, insert_after: contact_section}

cardinal app uninstall deletes these rows. The columns and their values stay, and a reinstall brings the fields back.

fixtures/property_setter.yaml changes a property of another RecType or one of its fields. CRM lets Ctrl+G find a contact by phone:

crm/fixtures/property_setter.yaml
- {parent_rectype: Contact, property: search_fields, value: [email_id]}
- {parent_rectype: Contact, fieldname: mobile_no, property: in_global_search, value: true}

A fixture record with when_installed: erp is written only on sites that have ERP, for an app that works with or without it. See Apps that work together.

Patches

A patch moves data once, on migrate. Put it in patches/<version>/ and use Cardinal.Patch:

erp/patches/v0_2/reference_doctype.ex
defmodule Erp.Patches.V0_2.ReferenceDoctype do
  use Cardinal.Patch

  @impl true
  def id, do: "erp.v0_2.reference_doctype"

  @impl true
  def phase, do: :post_schema

  @impl true
  def run(ctx) do
    for rectype <- ["Batch", "Serial No"] do
      {:ok, _} = Cardinal.Patch.rename_field(ctx, rectype, "reference_rectype", "reference_doctype")
    end

    :ok
  end
end
CallbackMeaning
id/0A stable string, stored in the site's patch_log
phase/0:pre_schema runs before the schema sync, :post_schema after the fixtures
run/1Gets an as_system ctx
depends_on/0Patch ids of the same app to run first
transaction/0false for large moves that commit their own work. They must be safe to run twice

A fresh install marks every patch applied without running it. rename_field/4 copies values into the new field wherever it is empty. The old column is never dropped.

On this page