File format and Git

功能性的

How a .sqllynb file is laid out, why it diffs cleanly, why results stay out of it, and how to review notebook changes in Git.

A notebook is one UTF-8 JSON file with the extension .sqllynb. SQLly writes it the same way every time, so the only lines that change in a commit are the ones you changed: review notebooks in pull requests like any other code.

What's in the file

{
  "format": "sqlly.notebook",
  "format_version": 2,
  "notebook_id": "5f0c2a9e-8d1b-4c47-9a63-2f1e7b0d4c18",
  "title": "Northwind revenue review",
  "connection_refs": [
    {
      "id": "conn_1",
      "profile_id": "0b6f4d1a-…",
      "label": "Storefront Dev",
      "database": "Northwind"
    }
  ],
  "parameters": [
    {
      "id": "p_min_year",
      "name": "min_year",
      "type": "integer",
      "default": 1997
    }
  ],
  "cells": [
    {
      "id": "c_qm4tz7a2kd",
      "kind": "markdown",
      "source": "# Revenue by country",
      "execution_policy": {
        "read_only": true
      }
    },
    {
      "id": "c_h3x9wpe5rb",
      "name": "by_country",
      "kind": "sql",
      "source": "SELECT ShipCountry, SUM(Freight) AS Freight\nFROM dbo.Orders\nWHERE YEAR(OrderDate) >= :min_year\nGROUP BY ShipCountry",
      "execution_policy": {
        "read_only": true,
        "max_rows": 10000
      }
    },
    {
      "id": "c_b7nf2kq6vy",
      "kind": "chart",
      "source": "",
      "execution_policy": {
        "read_only": true
      },
      "spec": {
        "chart": {
          "kind": "bar",
          "x": { "column": "ShipCountry" },
          "y": [{ "column": "Freight" }]
        },
        "input": "c_h3x9wpe5rb"
      }
    }
  ],
  "data_retention": {
    "embed_outputs": false
  },
  "settings": {
    "run_mode": "manual",
    "default_policy": {
      "read_only": true
    }
  }
}
format, format_version
一律 "sqlly.notebook" and, today, 2. A file with a newer version is refused with a message rather than opened and mangled.
notebook_id
A stable id for the notebook. Trust, run history and saved results are tied to it together with the file this computer trusted, so a copied file that keeps the id doesn't share them: the copy takes a new id the first time you save it in SQLly.
connection_refs
The connections the notebook uses, by slot. A slot names a saved connection only by its opaque id plus a display label and database: never a server name, user or password. On another computer the id doesn't match anything, so SQLly asks which of that person's connections each slot should use. See remapping connections.
parameters
Parameter definitions: name, type, default, validation and flags such as sensitive。參見 Parameters and inputs.
cells
Each cell has an id (c_ plus ten letters and digits, never reused), an optional name、它的 kind、它的 source text, an optional connection_ref slot, the execution_policy (read-only, timeout, row and download limits, cache mode), an optional session_group, and a kind-specific spec object. A cell on the notebook's local data has "target": "local" in its spec; a file that marks it with connection_ref: "local" instead (as older command-line notebooks did) is read the same and saved in the spec form.
data_retention
Whether results are embedded in the file. Off by default; see below.
settings
Notebook-wide settings: the run mode and the policy new cells start with.

Why it diffs cleanly

  • Fixed order. Fields are always written in the same order, settings objects (every spec, parameter defaults, unknown fields) have their keys sorted at every depth, indentation is two spaces and the file ends with a newline.
  • Stable ids. A cell keeps its id for life, so moving a cell shows up as a move, and references by id survive renames.
  • No results. Query results are not written into the file, so a re-run doesn't produce a diff and a commit never leaks yesterday's data. Results live in the app's own run history and (optionally) its encrypted cache instead; see Run history and snapshots.
  • Little that is machine-specific. Window sizes, panel layout and trust records stay on your computer, never in the file. The exception is an attachment picked from outside the notebook's folder, which records that file's path; another computer won't read it until someone picks the file there.

data_retention.embed_outputs: true is the one exception: it asks for capped result snapshots to be kept in the file, for a notebook that must carry its numbers with it. Leave it off for anything in Git. sqlly-cli notebook validate warns about a notebook that embeds outputs.

Files from newer and older versions

  • Unknown fields are kept. A field or setting written by a newer SQLly is read, kept and written back unchanged when you save. A setting value this version doesn't know (a new run mode or cache mode, say) behaves like the default and is saved back as it was; a parameter with an unknown re-run setting waits for Apply.
  • Unknown cell kinds are kept. A cell kind this version doesn't know shows as a read-only placeholder ("This … cell needs a newer version of SQLLY") and is saved back as it was.
  • Older SQLly notebooks (.sqlnb.json) open and are upgraded in memory: cells keep their cell_N ids and get that id as their name, so {{cell_2}} references keep working; old @name globals become text parameters with their old value as the default; and SQL cells that change data stay runnable (they open with writes allowed) while new cells start read-only. Saving writes a .sqllynb file next to it; the old file is never rewritten by autosave.
  • Jupyter .ipynb files open and save in Jupyter's own format; see Jupyter and older notebooks.

Notebooks in Git

The file is already readable in a plain git diff, but SQL inside JSON strings shows up as one long line with \n in it. The CLI can turn a notebook into plain text for Git, and compare two versions cell by cell:

# .gitattributes
*.sqllynb diff=sqllynb

# once per machine
git config --global diff.sqllynb.textconv "sqlly-cli notebook cat"
git config --global difftool.sqllynb.cmd 'sqlly-cli notebook diff "$LOCAL" "$REMOTE"'

After that, git diff shows each cell's SQL as real lines, and git difftool -t sqllynb shows added, removed, moved and renamed cells, each changed source as a line diff, and changes to settings, safety policy, connections and parameters, ignoring formatting-only churn. Details and the CI check are on The sqlly notebook CLI.

A notebook pulled from Git is a notebook from elsewhere. If a pull changes a notebook's SQL or connections, SQLly opens it untrusted, even if you trusted the previous version, so you can read what changed before anything runs. Markdown-only changes keep it trusted. See Security model.

If the file changes on disk while it's open in SQLly (a pull, a checkout, a sync folder), the top bar shows Changed on disk 與 重新載入 和 Keep mine, and Save waits for your choice: "report.sqllynb changed on disk. Choose Reload or Keep mine before saving." Saving keeps the file's permissions, and a notebook already open in a tab is never opened twice: opening it again switches to its tab.

Merge conflicts are ordinary JSON conflicts. Because every cell is its own object with a stable id and the formatting never shifts, two people editing different cells rarely conflict; when they do, resolve the JSON, then run sqlly-cli notebook validate to check the result.