Rules file

The ghostrow.rules.yaml format: what Ghostrow writes, which edits it keeps, how to add your own checks, and how to commit the file with your code.

ghostrow.rules.yaml is the reviewable record of what Ghostrow understood about your schema. Ghostrow writes it, you edit it, and your edits win on every later run. Commit it with your code so everyone generates the same data.

When it is written

ghostrow seed writes the file on --dry-run and after a successful run. haunt and twin read it and never change it. Use another path with --rules <file>.

The file is rewritten each time from the merged result. Since 1.0.1, comments you add are kept: Ghostrow matches rules by id and updates the file in place.

Example

This is the real output of ghostrow seed --schema shop.sql --dry-run on the six-table shop schema used in Ghostrow's tests, cut down to one table and two rules:

# Ghostrow rules (gr-schema-1). Review and commit this file with your code.
# Change a column's meaning, add "values", set "rows" per table, switch a rule off with
# "enabled: false", or add your own:  - { kind: check, table: orders, expr: "total >= 0" }
# Your edits are kept on every run. Same schema + rules + --seed = same rows.
engine: gr-schema-1/1.0
source: shop.sql
tables:
  customers:
    columns:
      tenant_id:
        meaning: reference
        confidence: 1
        evidence: foreign key to tenants
      email:
        meaning: email
        confidence: 0.83
        evidence: name, type
      nic:
        meaning: national_id_lk
        confidence: 0.96
        evidence: name, type, CHECK on length 10 or 12
rules:
  - id: unique:customers.tenant_id+email
    kind: unique
    table: customers
    expr: UNIQUE (tenant_id, email)
    enabled: true
    evidence: declared UNIQUE
    source: schema
  - id: sum:orders.total
    kind: sum
    table: orders
    expr: orders.total = sum of order_lines.amount
    enabled: true
    evidence: order_lines references orders and both carry an amount
    source: inferred

Tables and columns

FieldMeaning
engineThe inference engine and version that wrote the file
sourceThe schema file, folder or database the plan was read from
tables.<name>.rowsRows to generate for this table. Not written until you add it
tables.<name>.columns.<name>.meaningWhat the column holds, for example email, money, status
...valuesA list of allowed values. Not written until you add it
...confidence, ...evidenceHow sure Ghostrow is (0 to 1) and why. Informational

Change a meaning to one Ghostrow knows and it is used from the next run, with confidence 1 and the evidence set in rules file. The known meanings are:

id, reference, uuid, email, phone, first_name, last_name, full_name, username,
password_hash, token, company, job_title, title, description, slug, url, domain,
ip_address, sku, national_id_lk, national_id, postal_code, street, address, city,
region, country, country_code, currency_code, language_code, timezone, latitude,
longitude, money, percent, quantity, count, age, rating, year, birth_date, date,
datetime, time, boolean, status, color, mime_type, file_path, json, bytes,
integer, number, code, text

An unknown meaning is ignored with a note: unknown meaning "<name>" ignored. Known meanings: .... A meaning that does not fit the column type is also not applied.

Add values to a column to draw from a fixed list:

      currency:
        meaning: currency_code
        values: [USD, LKR]

Set rows on a table to fix its size. Row counts are taken from --table first, then the rules file, then the --rows calculation (see Seed).

Rules

Every entry under rules has an id, a kind, a table, an expr that reads as plain text, an enabled flag, and evidence and source fields. The kinds Ghostrow writes are:

KindMeaning
fkA foreign key. Child values point at real parent rows
uniqueA primary key, UNIQUE constraint or unique index. Values never repeat
checkA declared CHECK, or a limit read from code (source: code)
orderOne date is not before another, such as shipped_at after placed_at
copyA column equals a column of the referenced row, such as unit_price from the product
productA column is the product of two others, such as amount = quantity × unit_price
sumA parent total equals the sum of its children, such as an order total

source says where a rule came from: schema, inferred, code or rules file.

Set enabled: false on a rule to switch it off. Rules are matched by id, so keep the id as it is. In a dry run, a disabled rule shows as [off].

Add your own check

Add an entry with kind: check, a table and an expr. You can leave out id (Ghostrow gives it one that starts with user:) or choose your own. A rule Ghostrow cannot use is reported in a note rather than dropped silently.

rules:
  - { kind: check, table: orders, expr: "total >= 0" }

On the next run it is applied while generating and checked on every row:

rules         orders: CHECK (total >= 0)  (added in rules file)

Only check rules can be added by hand. Other kinds are ignored with the note only "check" rules can be added by hand. Rules and columns that no longer exist in the schema are dropped when the file is rewritten.

Commit it

Commit ghostrow.rules.yaml and review changes to it in pull requests, as you would a schema migration. A new column shows up as a new entry, and a changed meaning shows as a plain diff. In CI, a rules file in the repository is what makes every run use the same decisions. See CI.

Next