Răsfoiți Sursa

Restore every column a backup carries (#611)

* Restore every column a backup carries and prove it with a round trip

A restore dropped columns and links on sixteen models: payment state and
share links on work orders and quotes, customer numbers and tax ids, read
state on texts, tire movement shelves and more. Quantities, capacities and
mileages of zero came back as a default. Counter-sale status reports were
never exported.

Every hand-written restore map is now held to the schema by a test, and a
database test exports a fully filled workshop, restores it into an empty
one and compares every table column by column.

* Keep a quote's tire set and inspection through a selective restore

The link-back passes ran only when the backup carried tire sets or
inspections, so a quotes-only restore cleared links to sets and
inspections still in the workshop. Both now run on every restore and
accept any set or inspection the workshop holds at that point.
Bernt Christian Egeland 2 zile în urmă
părinte
comite
374e579b5d

+ 625 - 0
src/__tests__/db/backup-round-trip.db.test.ts

@@ -0,0 +1,625 @@
+/**
+ * A backup gives back what went into it, on a real PostgreSQL
+ * (`npm run test:db` with E2E_DATABASE_URL set).
+ *
+ * The guard tests beside the restore read its source against the schema,
+ * which catches a column somebody forgot in a field map. This one does not
+ * read anything: it seeds a workshop with a row in every place a backup file
+ * can hold one, every column filled with something other than its default,
+ * exports it through the real export, empties the database, and restores the
+ * file into a workshop with nothing in it. Every row of every table is then
+ * compared with the dump taken before, column by column.
+ *
+ * The seed is built from the Prisma schema and the backup layout, not by
+ * hand, so a column or a nested model added later is seeded, exported and
+ * compared without anyone touching this file. What is allowed to differ is
+ * listed in EXPECTED with the reason; anything else fails.
+ */
+import {
+  cpSync,
+  existsSync,
+  mkdirSync,
+  mkdtempSync,
+  readdirSync,
+  readFileSync,
+  rmSync,
+  writeFileSync,
+} from 'node:fs'
+import { tmpdir } from 'node:os'
+import path from 'node:path'
+import { PassThrough } from 'node:stream'
+import { afterAll, beforeAll, describe, expect, it } from 'vitest'
+import { readPrismaSchema } from '@/__tests__/stubs/prisma-schema'
+import { BACKUP_LAYOUT, type LayoutNode } from '@/lib/backup/layout'
+import { UPLOAD_CATEGORIES } from '@/lib/backup/manifest'
+import { TENANCY_SCHEMA } from '@/lib/backup/tenancy-schema'
+
+const url = process.env.E2E_DATABASE_URL ?? ''
+const databaseName = url.split('/').pop()?.split('?')[0]?.toLowerCase() ?? ''
+if (url && !/test|e2e/.test(databaseName)) {
+  throw new Error(
+    `Refusing to use database "${databaseName}": the suite empties it, so its name must say test or e2e.`
+  )
+}
+
+// Set before the app's modules load, so the client connects to the test
+// database and files land in a folder of this run's own.
+process.env.DATABASE_URL = url
+const dataRoot = mkdtempSync(path.join(tmpdir(), 'backup-round-trip-'))
+process.env.DATA_ROOT = dataRoot
+
+type Row = Record<string, unknown>
+type Dump = Map<string, Row[]>
+
+const SCHEMA = readPrismaSchema()
+const ORG = 'org_round_trip'
+const OWNER = 'user_owner'
+/** A second member, so a column that names a person is not always the importer. */
+const MEMBER = 'user_member'
+
+interface Field {
+  name: string
+  type: string
+  optional: boolean
+  list: boolean
+  /** The text of `@default(...)`, when the column has one. */
+  fallback: string | null
+}
+
+/** Each model's stored columns, and the table it lives in. */
+function readModels(): { fields: Map<string, Field[]>; tables: Map<string, string> } {
+  const fields = new Map<string, Field[]>()
+  const tables = new Map<string, string>()
+  for (const block of SCHEMA.matchAll(/^model\s+(\w+)\s*\{([\s\S]*?)^\}/gm)) {
+    const list: Field[] = []
+    for (const line of block[2].split('\n')) {
+      const field =
+        /^\s+(\w+)\s+(String|Int|Float|Boolean|DateTime|Json|Bytes|BigInt)(\?|\[\])?(\s.*)?$/.exec(
+          line
+        )
+      if (!field || line.trim().startsWith('//')) continue
+      const fallback = /@default\((.*)\)/.exec(field[4] ?? '')
+      list.push({
+        name: field[1],
+        type: field[2],
+        optional: field[3] === '?',
+        list: field[3] === '[]',
+        fallback: fallback ? fallback[1].replace(/\).*$/, '') : null,
+      })
+    }
+    fields.set(block[1], list)
+    const map = /@@map\("([^"]+)"\)/.exec(block[2])
+    tables.set(block[1], map ? map[1] : block[1])
+  }
+  return { fields, tables }
+}
+
+const { fields: FIELDS, tables: TABLE_OF } = readModels()
+const MODEL_OF = new Map([...TABLE_OF].map(([model, table]) => [table, model]))
+
+/** One place in a backup file that holds rows: a dataset, or a list nested in a row. */
+interface Position {
+  id: string
+  model: string
+  parent: Position | null
+}
+
+function positions(): Position[] {
+  const out: Position[] = []
+  const walk = (key: string, node: LayoutNode, parent: Position | null) => {
+    const position = { id: parent ? `${parent.id}_${key}` : key, model: node.model, parent }
+    out.push(position)
+    for (const [childKey, child] of Object.entries(node.children ?? {})) {
+      walk(childKey, child, position)
+    }
+  }
+  for (const [key, node] of Object.entries(BACKUP_LAYOUT)) walk(key, node, null)
+  return out
+}
+
+const POSITIONS = positions()
+const POSITIONS_OF = new Map<string, Position[]>()
+for (const position of POSITIONS) {
+  POSITIONS_OF.set(position.model, [...(POSITIONS_OF.get(position.model) ?? []), position])
+}
+
+function lowerFirst(name: string): string {
+  return name.charAt(0).toLowerCase() + name.slice(1)
+}
+
+/**
+ * Places the layout offers that the app never fills, so no row is seeded
+ * there. Each is a statement about the product, not about the backup.
+ */
+const NEVER_FILLED: Readonly<Record<string, string>> = {}
+
+/**
+ * The column that ties a nested row to the row it nests in, where the name
+ * does not say so.
+ */
+const PARENT_LINK: Readonly<Record<string, string>> = {}
+
+/**
+ * Values the generic seed cannot guess: a column whose text means something
+ * to the restore (a file link it rewrites, a status it checks), or a pair of
+ * columns the app never fills together. Keyed by model, then by column; a
+ * function gets the position's id. `undefined` leaves the generic value.
+ */
+type Override = unknown | ((position: Position) => unknown)
+/** A tax split as the documents store it; the restore reads it leniently and writes it clean. */
+const TAX_SPLIT = [
+  { name: 'GST', rate: 5, amount: 4.2, registrationNumber: 'RT 0001' },
+  { name: 'QST', rate: 9.975, amount: 8.38, compound: true },
+]
+const OVERRIDES: Readonly<Record<string, Readonly<Record<string, Override>>>> = {
+  // A status the restore does not know the stage of is not restored.
+  WorkOrderStatus: { stage: 'in-progress' },
+  // Months, mileage and an expiry belong to an included warranty only.
+  ServiceRecord: { warrantyStatus: 'included', taxComponents: TAX_SPLIT },
+  Quote: { warrantyStatus: 'included', taxComponents: TAX_SPLIT },
+  // The kinds of mark at completion, a list.
+  Inspection: { markTypesSnapshot: [{ key: 'dent', label: 'Dent', color: '#f00' }] },
+  // The newer mark a dent was recorded again as. The seed has one mark per
+  // place, so it names itself: what matters is that the link comes back.
+  ConditionMark: { carriedToId: (position: Position) => position.id },
+}
+
+/** How much of their place in the file two positions share, for choosing a row in the same tree. */
+function sharedPrefix(a: string, b: string): number {
+  let n = 0
+  while (n < a.length && n < b.length && a[n] === b[n]) n++
+  return n
+}
+
+/**
+ * The seed is run twice. `filled` gives every number something above zero;
+ * `zeros` gives every number zero, which is what a restore written as
+ * `value || fallback` turns into the fallback.
+ */
+type Variant = 'filled' | 'zeros'
+
+/** A generic value for a column, never the column's default. */
+function generic(field: Field, position: Position, index: number, variant: Variant): unknown {
+  const tag = `${field.name}:${position.id}`
+  if (field.list) return field.type === 'String' ? [`${tag}:1`, `${tag}:2`] : []
+  switch (field.type) {
+    case 'String':
+      return /email/i.test(field.name) ? `${position.id}.${field.name}@example.test` : tag
+    case 'Int':
+      return variant === 'zeros' ? 0 : 3 + index
+    case 'BigInt':
+      return BigInt(variant === 'zeros' ? 0 : 1000 + index)
+    case 'Float':
+      return variant === 'zeros' ? 0 : 12.5 + index
+    case 'Boolean':
+      return field.fallback !== 'true'
+    case 'DateTime':
+      // A fixed moment per column, in the past: a restore that stamps "now"
+      // instead of the stored time shows up as a difference.
+      return new Date(Date.UTC(2024, 0, 1 + (index % 300), 7, index % 60, 9, 120))
+    case 'Json':
+      return { seeded: tag, list: [1, 2], nested: { on: true } }
+    case 'Bytes':
+      return new Uint8Array([1, 2, 3, index % 250])
+    default:
+      throw new Error(`No seed value for ${field.type}`)
+  }
+}
+
+function rowFor(position: Position, variant: Variant): Row {
+  const model = position.model
+  const known = TENANCY_SCHEMA[model]
+  const fields = FIELDS.get(model)
+  if (!known || !fields) throw new Error(`${model} is in the layout but not in the schema`)
+
+  // The models this model nests under elsewhere in the file. A row has one
+  // parent, so the columns that point at the other kinds stay empty.
+  const otherParents = new Set(
+    (POSITIONS_OF.get(model) ?? [])
+      .filter((other) => other !== position && other.parent?.model !== position.parent?.model)
+      .map((other) => other.parent?.model)
+      .filter((name): name is string => !!name)
+  )
+  const parentLinks = position.parent
+    ? Object.entries(known.foreignKeys)
+        .filter(([, fk]) => fk.target === position.parent?.model)
+        .map(([column]) => column)
+    : []
+  const parentLink =
+    PARENT_LINK[model] ??
+    parentLinks.find((column) => column === `${lowerFirst(position.parent?.model ?? '')}Id`) ??
+    parentLinks[0]
+  if (position.parent && !parentLink) {
+    throw new Error(`${model} has no column pointing at ${position.parent.model}`)
+  }
+
+  const row: Row = {}
+  fields.forEach((field, index) => {
+    const override = OVERRIDES[model]?.[field.name]
+    const overridden = typeof override === 'function' ? override(position) : override
+    if (field.name in (OVERRIDES[model] ?? {}) && overridden !== undefined) {
+      row[field.name] = overridden
+      return
+    }
+    if (field.name === 'id') {
+      row.id = position.id
+      return
+    }
+    if (field.name === 'organizationId') {
+      row.organizationId = ORG
+      return
+    }
+    if (position.parent && field.name === parentLink) {
+      row[field.name] = position.parent.id
+      return
+    }
+    const fk = known.foreignKeys[field.name]
+    const plain = !fk && field.type === 'String' && !field.list
+    // A column that names a person without a foreign key: who created,
+    // unlocked or wrote something.
+    if (plain && /(^userId|UserId|ById)$/.test(field.name)) {
+      row[field.name] = MEMBER
+      return
+    }
+    const plainTarget =
+      plain && field.name.endsWith('Id')
+        ? field.name.charAt(0).toUpperCase() + field.name.slice(1, -2)
+        : null
+    // A column named after its own model is not a link: `telegramMessageId`
+    // on a Telegram message is Telegram's number for it.
+    const target =
+      fk?.target ??
+      (plainTarget && plainTarget !== model && POSITIONS_OF.has(plainTarget) ? plainTarget : null)
+    if (target === 'User') {
+      row[field.name] = MEMBER
+      return
+    }
+    if (target === 'Organization') {
+      row[field.name] = ORG
+      return
+    }
+    if (target) {
+      // A row of the same tree first: a job's attachment is filed under one
+      // of that job's concerns, not another job's.
+      const candidates = (POSITIONS_OF.get(target) ?? [])
+        .filter((other) => other !== position)
+        .sort((a, b) => sharedPrefix(b.id, position.id) - sharedPrefix(a.id, position.id))
+      const empty = otherParents.has(target) || candidates.length === 0
+      if (empty && fk?.required) {
+        throw new Error(`${model}.${field.name} is required and has nothing to point at`)
+      }
+      row[field.name] = empty ? null : candidates[0].id
+      return
+    }
+    row[field.name] = generic(field, position, index, variant)
+  })
+  return row
+}
+
+let db: typeof import('@/lib/db')['db']
+let importCore: typeof import('@/lib/backup/import')
+let buildData: typeof import('@/lib/backup/build-data')
+let tableNames: string[] = []
+
+async function listTables(): Promise<string[]> {
+  const rows = await db.$queryRawUnsafe<{ table_name: string }[]>(
+    `select table_name from information_schema.tables
+     where table_schema = 'public' and table_type = 'BASE TABLE' order by table_name`
+  )
+  return rows.map((r) => r.table_name).filter((t) => t !== '_prisma_migrations')
+}
+
+async function emptyDatabase(): Promise<void> {
+  await db.$executeRawUnsafe(`truncate table ${tableNames.map((t) => `"${t}"`).join(', ')} cascade`)
+}
+
+/** Every row of every table, by table, as the database holds it. */
+async function dumpAll(): Promise<Dump> {
+  const out: Dump = new Map()
+  for (const table of tableNames) {
+    const rows = await db.$queryRawUnsafe<{ row: string }[]>(
+      `select row_to_json(t)::text as row from "${table}" t`
+    )
+    if (rows.length) {
+      out.set(
+        table,
+        rows
+          .map((r) => r.row)
+          .sort()
+          .map((text) => JSON.parse(text) as Row)
+      )
+    }
+  }
+  return out
+}
+
+/** The people and the workshop itself: what a backup never carries and a restore expects to find. */
+async function createWorkshop(): Promise<void> {
+  await db.user.createMany({
+    data: [
+      { id: OWNER, name: 'Owner', email: 'owner@round-trip.test' },
+      { id: MEMBER, name: 'Member', email: 'member@round-trip.test' },
+    ],
+  })
+  await db.organization.create({ data: { id: ORG, name: 'Round trip workshop' } })
+  await db.organizationMember.createMany({
+    data: [
+      { id: 'member_owner', userId: OWNER, organizationId: ORG, role: 'owner' },
+      { id: 'member_member', userId: MEMBER, organizationId: ORG, role: 'member' },
+    ],
+  })
+}
+
+/** One file in every upload folder a backup carries. */
+function seedFiles(): void {
+  for (const category of UPLOAD_CATEGORIES) {
+    const dir = path.join(dataRoot, 'uploads', ORG, category)
+    mkdirSync(dir, { recursive: true })
+    writeFileSync(path.join(dir, `${category}-file.bin`), `bytes of ${category}`)
+  }
+}
+
+function filesOf(dir: string): Record<string, string> {
+  const out: Record<string, string> = {}
+  if (!existsSync(dir)) return out
+  for (const category of readdirSync(dir).sort()) {
+    for (const name of readdirSync(path.join(dir, category)).sort()) {
+      out[`${category}/${name}`] = readFileSync(path.join(dir, category, name), 'utf-8')
+    }
+  }
+  return out
+}
+
+/**
+ * Writes every seeded row in one transaction with the foreign key checks
+ * off, so the rows can go in in any order; each link was built to point at
+ * a row of the seed, and the restore would refuse one that did not.
+ */
+async function seedRows(variant: Variant): Promise<number> {
+  const rows = POSITIONS.filter((position) => !(position.id in NEVER_FILLED)).map((position) => ({
+    position,
+    row: rowFor(position, variant),
+  }))
+  await db.$transaction(
+    async (tx) => {
+      await tx.$executeRawUnsafe(`set local session_replication_role = replica`)
+      for (const { position, row } of rows) {
+        const delegate = (
+          tx as unknown as Record<string, { create(args: unknown): Promise<unknown> }>
+        )[lowerFirst(position.model)]
+        try {
+          await delegate.create({ data: row })
+        } catch (error) {
+          throw new Error(`Could not seed ${position.id} (${position.model}): ${String(error)}`)
+        }
+      }
+    },
+    { timeout: 120_000 }
+  )
+  return rows.length
+}
+
+async function exportWorkshop(
+  only?: (keyof typeof buildData.DEFAULT_OPTIONS)[]
+): Promise<{ dataFile: string; filesDir: string; text: string }> {
+  const chunks: string[] = []
+  const out = new PassThrough()
+  out.on('data', (chunk: Buffer | string) => chunks.push(chunk.toString()))
+  const options = { ...buildData.DEFAULT_OPTIONS }
+  if (only) {
+    for (const key of Object.keys(options) as (keyof typeof options)[]) {
+      options[key] = only.includes(key)
+    }
+  }
+  await buildData.writeBackupData(ORG, { ...options, includeSecrets: true }, out)
+  out.end()
+  const text = chunks.join('')
+  const dir = mkdtempSync(path.join(dataRoot, 'backup-'))
+  const dataFile = path.join(dir, 'data.json')
+  writeFileSync(dataFile, text)
+  // The folders a backup packs, as the export route and the snapshot do.
+  const filesDir = path.join(dir, 'files')
+  for (const category of UPLOAD_CATEGORIES) {
+    const from = path.join(dataRoot, 'uploads', ORG, category)
+    if (existsSync(from)) cpSync(from, path.join(filesDir, category), { recursive: true })
+  }
+  return { dataFile, filesDir, text }
+}
+
+/** Tables a backup does not carry, which this test puts back itself. */
+const NOT_IN_A_BACKUP = new Set(['User', 'Organization', 'OrganizationMember'])
+
+interface Difference {
+  where: string
+  before: unknown
+  after: unknown
+}
+
+function compare(before: Dump, after: Dump): Difference[] {
+  const out: Difference[] = []
+  for (const table of new Set([...before.keys(), ...after.keys()])) {
+    const model = MODEL_OF.get(table) ?? table
+    if (NOT_IN_A_BACKUP.has(model)) continue
+    const was = new Map((before.get(table) ?? []).map((row) => [String(row.id), row]))
+    const is = new Map((after.get(table) ?? []).map((row) => [String(row.id), row]))
+    for (const [id, row] of was) {
+      const restored = is.get(id)
+      if (!restored) {
+        out.push({ where: `${model} row ${id}`, before: 'present', after: 'missing' })
+        continue
+      }
+      for (const column of Object.keys(row)) {
+        if (JSON.stringify(row[column]) !== JSON.stringify(restored[column])) {
+          out.push({ where: `${model}.${column}`, before: row[column], after: restored[column] })
+        }
+      }
+    }
+    for (const id of is.keys()) {
+      if (!was.has(id)) {
+        out.push({ where: `${model} row ${id}`, before: 'missing', after: 'present' })
+      }
+    }
+  }
+  return out
+}
+
+/**
+ * What a restore is allowed to change, and why. Keyed as the differences
+ * are named: `Model.column`, or `Model row <id>` for a row that is lost or
+ * appears.
+ */
+const OWNED_BY_WHOEVER_RESTORES =
+  'The row is owned by whoever restores the backup: the column is required, and the account the file names may not exist here.'
+const EXPECTED: Readonly<Record<string, string>> = {
+  'AppSetting.userId': OWNED_BY_WHOEVER_RESTORES,
+  'CustomFieldDefinition.userId': OWNED_BY_WHOEVER_RESTORES,
+  'Customer.userId': OWNED_BY_WHOEVER_RESTORES,
+  'InspectionReminderCampaign.createdById': OWNED_BY_WHOEVER_RESTORES,
+  'InventoryPart.userId': OWNED_BY_WHOEVER_RESTORES,
+  'LaborPreset.userId': OWNED_BY_WHOEVER_RESTORES,
+  'Quote.userId': OWNED_BY_WHOEVER_RESTORES,
+  'ScheduledMessage.createdById': OWNED_BY_WHOEVER_RESTORES,
+  'StockMovement.userId': OWNED_BY_WHOEVER_RESTORES,
+  'TireSet.userId': OWNED_BY_WHOEVER_RESTORES,
+  'TireWarehouse.userId': OWNED_BY_WHOEVER_RESTORES,
+  'Vehicle.userId': OWNED_BY_WHOEVER_RESTORES,
+}
+
+/** What differs only when the numbers are zero. */
+const NO_PERIOD =
+  'A warranty of zero months or zero distance is no period, stored as none (normalizeWarranty), as the editor stores it.'
+const EXPECTED_AT_ZERO: Readonly<Record<string, string>> = {
+  'Quote.warrantyMonths': NO_PERIOD,
+  'Quote.warrantyMileage': NO_PERIOD,
+  'ServiceRecord.warrantyMonths': NO_PERIOD,
+  'ServiceRecord.warrantyMileage': NO_PERIOD,
+}
+
+/**
+ * Columns the seed leaves empty, and why. Everything else must hold a value
+ * before the export, or the comparison says nothing about it.
+ */
+const IMPORT_BATCH =
+  'Spreadsheet import batches are not in a backup, so nothing is there to point at.'
+const ONE_PARENT = 'The row belongs to one kind of parent; this is the column for the other kind.'
+const LEFT_EMPTY: Readonly<Record<string, string>> = {
+  'Customer.importBatchId': IMPORT_BATCH,
+  'Inspection.importBatchId': IMPORT_BATCH,
+  'ServiceRecord.importBatchId': IMPORT_BATCH,
+  'Vehicle.importBatchId': IMPORT_BATCH,
+  'Reminder.vehicleId': ONE_PARENT,
+  'ServiceRecord.vehicleId': ONE_PARENT,
+  'StatusReport.inspectionId': ONE_PARENT,
+  'StatusReport.serviceRecordId': ONE_PARENT,
+  'StoredImage.inventoryPartId': ONE_PARENT,
+  'StoredImage.tireMeasurementId': ONE_PARENT,
+}
+
+const describeDb = url ? describe : describe.skip
+
+describeDb('a backup round trip', () => {
+  beforeAll(async () => {
+    ;({ db } = await import('@/lib/db'))
+    importCore = await import('@/lib/backup/import')
+    buildData = await import('@/lib/backup/build-data')
+    tableNames = await listTables()
+    expect(tableNames.length).toBeGreaterThan(50)
+  })
+
+  afterAll(async () => {
+    if (db) await db.$disconnect()
+    rmSync(dataRoot, { recursive: true, force: true })
+  })
+
+  it.each([
+    'filled',
+    'zeros',
+  ] as const)('restores every row, column and file into an empty workshop (%s)', async (variant) => {
+    await emptyDatabase()
+    rmSync(path.join(dataRoot, 'uploads'), { recursive: true, force: true })
+    await createWorkshop()
+    const seeded = await seedRows(variant)
+    seedFiles()
+    expect(seeded).toBeGreaterThan(70)
+
+    const before = await dumpAll()
+    const filesBefore = filesOf(path.join(dataRoot, 'uploads', ORG))
+
+    // Every column of every seeded row holds something, or the comparison
+    // below proves nothing about it.
+    const unfilled: string[] = []
+    for (const [table, rows] of before) {
+      const model = MODEL_OF.get(table) ?? table
+      if (NOT_IN_A_BACKUP.has(model)) continue
+      for (const row of rows) {
+        for (const [column, value] of Object.entries(row)) {
+          if (value === null) unfilled.push(`${model}.${column}`)
+        }
+      }
+    }
+    expect([...new Set(unfilled)].filter((column) => !(column in LEFT_EMPTY))).toEqual([])
+
+    const backup = await exportWorkshop()
+
+    // A workshop with nothing in it: the same people, and no record.
+    await emptyDatabase()
+    rmSync(path.join(dataRoot, 'uploads'), { recursive: true, force: true })
+    await createWorkshop()
+
+    await importCore.restoreFromDisk(
+      { organizationId: ORG, userId: OWNER },
+      { dataFile: backup.dataFile, filesDir: backup.filesDir }
+    )
+
+    const after = await dumpAll()
+    const differences = compare(before, after)
+    const expected = variant === 'zeros' ? { ...EXPECTED, ...EXPECTED_AT_ZERO } : EXPECTED
+    const unexpected = differences.filter((d) => !(d.where in expected))
+    const stale = Object.keys(expected).filter((key) => !differences.some((d) => d.where === key))
+
+    if (process.env.BACKUP_ROUND_TRIP_REPORT) {
+      writeFileSync(
+        `${process.env.BACKUP_ROUND_TRIP_REPORT}.${variant}.json`,
+        JSON.stringify({ seeded, unfilled, differences, stale }, null, 2)
+      )
+    }
+
+    expect(
+      unexpected.map((d) => `${d.where}: ${JSON.stringify(d.before)} -> ${JSON.stringify(d.after)}`)
+    ).toEqual([])
+    expect(stale, 'listed as expected to differ, but came back as it was').toEqual([])
+    expect(filesOf(path.join(dataRoot, 'uploads', ORG))).toEqual(filesBefore)
+  })
+
+  it('keeps a quote tied to its tire set and inspection when the backup carries only quotes', async () => {
+    // A selective export leaves the tire hotel and the inspections where they
+    // are. The quotes are replaced, and each still belongs to the set and the
+    // inspection it was raised from, because both are still in the workshop.
+    await emptyDatabase()
+    await createWorkshop()
+    await seedRows('filled')
+
+    const links = () =>
+      db.quote.findMany({
+        where: { organizationId: ORG },
+        select: { id: true, tireSetId: true, inspectionId: true },
+        orderBy: { id: 'asc' },
+      })
+    const before = await links()
+    expect(before.length).toBeGreaterThan(0)
+    expect(before.every((quote) => quote.tireSetId && quote.inspectionId)).toBe(true)
+
+    const backup = await exportWorkshop(['quotes'])
+    const carried = (JSON.parse(backup.text) as { data: Record<string, unknown> }).data
+    expect(carried.quotes).toBeTruthy()
+    expect(carried.tireSets).toBeUndefined()
+    expect(carried.inspections).toBeUndefined()
+
+    await importCore.restoreFromDisk(
+      { organizationId: ORG, userId: OWNER },
+      { dataFile: backup.dataFile, filesDir: backup.filesDir }
+    )
+
+    expect(await links()).toEqual(before)
+  })
+})

+ 2 - 2
src/__tests__/features/backup/inspection-restore-fields.test.ts

@@ -59,8 +59,8 @@ describe.each([
 describe('the links back to an inspection', () => {
   it('are written after the inspections exist, for jobs and for quotes', () => {
     const inspections = importSource.indexOf('tx.inspection.create(')
-    const jobLink = importSource.indexOf('data: { inspectionId: sr.inspectionId as string }')
-    const quoteLink = importSource.indexOf('data: { inspectionId: q.inspectionId as string }')
+    const jobLink = importSource.indexOf('inspectionId: sr.inspectionId as string')
+    const quoteLink = importSource.indexOf('inspectionId: q.inspectionId as string')
     expect(jobLink).toBeGreaterThan(inspections)
     expect(quoteLink).toBeGreaterThan(inspections)
   })

+ 187 - 0
src/__tests__/features/backup/restore-map-fields.test.ts

@@ -0,0 +1,187 @@
+import fs from 'node:fs'
+import path from 'node:path'
+import { describe, expect, it } from 'vitest'
+import { readPrismaSchema } from '@/__tests__/stubs/prisma-schema'
+import { layoutModels } from '@/lib/backup/layout'
+
+/**
+ * Restore completeness for every model a backup carries.
+ *
+ * The export writes whole rows. The import puts most of them back through
+ * hand-written field maps, and a column added to the schema but not to its
+ * map is dropped on restore without a word: an invoice marked paid by hand
+ * came back unpaid, every quote link a customer held stopped working, a
+ * customer's tax number and a payment's provider reference were gone. This
+ * reads the import source against the schema for every model in the backup
+ * layout, so the next forgotten column fails here. A column that is rightly
+ * not in its map goes in NOT_IN_THE_MAP with the reason.
+ *
+ * The round trip on a real database (src/__tests__/db/backup-round-trip) is
+ * the proof that what the maps write is right; this is the part that runs
+ * without a database.
+ */
+
+const ROOT = process.cwd()
+const importSource = fs.readFileSync(path.join(ROOT, 'src/lib/backup/import.ts'), 'utf-8')
+/** Comments dropped, so a bracket or a column name in prose is not read as code. */
+const importCode = importSource.replace(/\/\/.*$/gm, '')
+const schema = readPrismaSchema(ROOT)
+
+function scalarColumns(model: string): string[] {
+  const match = schema.match(new RegExp(`^model\\s+${model}\\s*\\{([\\s\\S]*?)^\\}`, 'm'))
+  expect(match, `model ${model} not found in schema`).toBeTruthy()
+  const columns: string[] = []
+  for (const line of match![1].split('\n')) {
+    const field = line.match(
+      /^\s{2}(\w+)\s+(String|Int|Float|Boolean|DateTime|Json|Bytes|BigInt)(\?|\[\])?/
+    )
+    if (field) columns.push(field[1])
+  }
+  expect(columns.length).toBeGreaterThan(0)
+  return columns
+}
+
+/** Every create call the import makes for a model, each to the parenthesis that closes it. */
+function restoreMaps(model: string): string[] {
+  const delegate = model.charAt(0).toLowerCase() + model.slice(1)
+  const maps: string[] = []
+  for (const site of importCode.matchAll(
+    new RegExp(`tx\\.${delegate}\\.(create|createMany)\\(`, 'g')
+  )) {
+    let depth = 0
+    for (let i = site.index + site[0].length - 1; i < importCode.length; i++) {
+      if (importCode[i] === '(') depth++
+      if (importCode[i] === ')' && --depth === 0) {
+        maps.push(importCode.slice(site.index, i + 1))
+        break
+      }
+    }
+  }
+  return maps
+}
+
+/**
+ * A create call fed by restoreRows or columnsOf, which write every column
+ * the row has (rows.ts): nothing to forget.
+ */
+function writesWholeRows(map: string): boolean {
+  return /data:\s*\w+ as never/.test(map) || map.includes('columnsOf(')
+}
+
+const WARRANTY =
+  'Written by the warrantyFromUntyped spread, which also reads backups from before the statement existed.'
+const LINKED_LATER =
+  'The row it points at is restored later, so the link is written once it exists. Checked below.'
+const IMPORT_BATCH =
+  'Spreadsheet import batches are not in a backup; the id would point at nothing.'
+
+/** Columns a map leaves out on purpose, by model, with what happens instead. */
+const NOT_IN_THE_MAP: Readonly<Record<string, Readonly<Record<string, string>>>> = {
+  ServiceRecord: {
+    warrantyStatus: WARRANTY,
+    warrantyMonths: WARRANTY,
+    warrantyMileage: WARRANTY,
+    warrantyNotes: WARRANTY,
+    inspectionId: LINKED_LATER,
+    tireSetId: LINKED_LATER,
+    importBatchId: IMPORT_BATCH,
+  },
+  Quote: {
+    warrantyStatus: WARRANTY,
+    warrantyMonths: WARRANTY,
+    warrantyMileage: WARRANTY,
+    warrantyNotes: WARRANTY,
+    inspectionId: LINKED_LATER,
+    tireSetId: LINKED_LATER,
+  },
+  Customer: { importBatchId: IMPORT_BATCH },
+  Vehicle: { importBatchId: IMPORT_BATCH },
+}
+
+describe.each([...layoutModels()].sort())('%s restore', (model) => {
+  const maps = restoreMaps(model)
+  const skipped = NOT_IN_THE_MAP[model] ?? {}
+
+  it('is written by the import', () => {
+    expect(maps.length, `the import never creates a ${model}`).toBeGreaterThan(0)
+  })
+
+  it('carries every schema column, so nothing is dropped on restore', () => {
+    const columns = scalarColumns(model)
+    for (const map of maps) {
+      if (writesWholeRows(map)) continue
+      for (const column of columns) {
+        if (column in skipped) continue
+        expect(map, `${model}.${column} is lost by a ${model} restore map`).toMatch(
+          new RegExp(`\\b${column}(:|,\\s)`)
+        )
+      }
+    }
+  })
+
+  it('lists only columns the model has and the map really leaves out', () => {
+    const columns = scalarColumns(model)
+    for (const column of Object.keys(skipped)) {
+      expect(columns, `${model}.${column} is listed but not in the schema`).toContain(column)
+      for (const map of maps) {
+        expect(map, `${model}.${column} is listed as left out but a map sets it`).not.toMatch(
+          new RegExp(`\\b${column}:`)
+        )
+      }
+    }
+  })
+})
+
+describe('the allowlist', () => {
+  it('names only models a backup carries', () => {
+    const models = layoutModels()
+    for (const model of Object.keys(NOT_IN_THE_MAP)) {
+      expect(models, `${model} is not in the backup layout`).toContain(model)
+    }
+  })
+})
+
+describe('documents', () => {
+  it.each([
+    'ServiceRecord',
+    'Quote',
+  ])('%s writes the warranty through the shared reader', (model) => {
+    for (const map of restoreMaps(model)) expect(map).toContain('...warrantyFromUntyped(')
+  })
+})
+
+describe('the links written after the rows they point at exist', () => {
+  const links = [
+    { link: 'inspectionId: sr.inspectionId as string', created: 'tx.inspection.create(' },
+    { link: 'inspectionId: q.inspectionId as string', created: 'tx.inspection.create(' },
+    { link: 'tireSetId: sr.tireSetId as string', created: 'tx.tireSet.create(' },
+    { link: 'tireSetId: q.tireSetId as string', created: 'tx.tireSet.create(' },
+  ]
+
+  it.each(links)('$link', ({ link, created }) => {
+    const at = importSource.indexOf(link)
+    expect(at, `${link} is never written`).toBeGreaterThan(-1)
+    expect(at).toBeGreaterThan(importSource.indexOf(created))
+  })
+
+  it('reach counter sales as well as vehicle jobs', () => {
+    expect(importSource).toContain('...linked(data.counterSales)')
+    expect(importSource).toContain('...aboutASet(data.counterSales)')
+  })
+
+  it('keep the row’s own last-changed time', () => {
+    // An update stamps updatedAt with now unless it is given; left out, every
+    // linked job and quote came back changed on the day of the restore.
+    for (const { link } of links) {
+      const at = importSource.indexOf(link)
+      expect(importSource.slice(at, at + 160), link).toMatch(/updatedAt: toSafeDate\(/)
+    }
+  })
+})
+
+describe('numbers', () => {
+  it('come back as they were, zero included', () => {
+    // `(value as number) || 1` turned a quantity of zero into one.
+    expect(importCode).not.toMatch(/as number\) \|\| (null|[1-9])/)
+  })
+})

+ 2 - 0
src/lib/backup/build-data.ts

@@ -203,6 +203,8 @@ const DATASETS: readonly Dataset[] = [
           laborItems: true,
           attachments: true,
           payments: true,
+          // A counter sale can be reported on like any other job.
+          statusReports: true,
           timeEntries: true,
           helpers: true,
         },

+ 228 - 58
src/lib/backup/import.ts

@@ -167,6 +167,15 @@ function storedTaxableSubtotalFromBackup(value: unknown): number | null {
   return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : null
 }
 
+/**
+ * A number as the backup holds it, zero included. The fallback is for a file
+ * that has none: written as `value || fallback`, a part line with a quantity
+ * of zero came back as one and an odometer at zero as unknown.
+ */
+function numberOr<T>(value: unknown, fallback: T): number | T {
+  return typeof value === 'number' && Number.isFinite(value) ? value : fallback
+}
+
 /** A foreign key from the backup, kept only when this import restored its target. */
 function keptReference(id: unknown, restored: ReadonlySet<string> | undefined): string | null {
   return typeof id === 'string' && id && restored?.has(id) ? id : null
@@ -202,6 +211,12 @@ async function importServiceRecordTree(
     technicianIds: ReadonlySet<string>
     /** The workshop's own statuses restored by this import. */
     workOrderStatusIds: ReadonlySet<string>
+    /**
+     * The people in the restored workshop. Whoever opened or unlocked a job is
+     * kept only when they are one of them: a backup brought to another install
+     * names accounts that do not exist there.
+     */
+    memberUserIds: ReadonlySet<string>
     /**
      * Designs and snapshots restored by this import. A reference to one the
      * backup did not carry is dropped rather than left dangling: the record
@@ -240,7 +255,7 @@ async function importServiceRecordTree(
       type: (sr.type as string) || 'maintenance',
       status: (sr.status as string) || 'completed',
       cost: (sr.cost as number) || 0,
-      mileage: (sr.mileage as number) || null,
+      mileage: numberOr(sr.mileage, null),
       serviceDate: toSafeDate(sr.serviceDate as string),
       startDateTime: startDT ?? undefined,
       endDateTime: endDT ?? undefined,
@@ -248,7 +263,7 @@ async function importServiceRecordTree(
       shopName: (sr.shopName as string) || null,
       techName: (sr.techName as string) || null,
       parts: (sr.parts as string) || null,
-      laborHours: (sr.laborHours as number) || null,
+      laborHours: numberOr(sr.laborHours, null),
       diagnosticNotes: (sr.diagnosticNotes as string) || null,
       invoiceNotes: (sr.invoiceNotes as string) || null,
       subtotal: (sr.subtotal as number) || 0,
@@ -259,6 +274,8 @@ async function importServiceRecordTree(
       taxableSubtotal: storedTaxableSubtotalFromBackup(sr.taxableSubtotal),
       totalAmount: (sr.totalAmount as number) || 0,
       invoiceNumber: (sr.invoiceNumber as string) || null,
+      invoiceDate: sr.invoiceDate ? toSafeDate(sr.invoiceDate as string) : null,
+      invoiceDueDate: sr.invoiceDueDate ? toSafeDate(sr.invoiceDueDate as string) : null,
       discountType: (sr.discountType as string) || null,
       discountValue: (sr.discountValue as number) || 0,
       discountAmount: (sr.discountAmount as number) || 0,
@@ -266,6 +283,8 @@ async function importServiceRecordTree(
       // which is read as the included warranty it was.
       ...warrantyFromUntyped(sr),
       warrantyExpiresAt: sr.warrantyExpiresAt ? toSafeDate(sr.warrantyExpiresAt as string) : null,
+      conditionMapOnInvoice:
+        typeof sr.conditionMapOnInvoice === 'boolean' ? sr.conditionMapOnInvoice : null,
       publicToken: (sr.publicToken as string) || null,
       // The link's own expiry answers travel with its token, or a restored
       // link would forget the date the workshop set on it.
@@ -278,6 +297,13 @@ async function importServiceRecordTree(
       // and its link stays open.
       manuallyPaid: sr.manuallyPaid === true,
       manuallyPaidAt: toSafeDate(sr.manuallyPaidAt as string) ?? null,
+      viewCount: (sr.viewCount as number) || 0,
+      lastViewedAt: sr.lastViewedAt ? toSafeDate(sr.lastViewedAt as string) : null,
+      sentAt: sr.sentAt ? toSafeDate(sr.sentAt as string) : null,
+      bookingSource: (sr.bookingSource as string) || null,
+      createdById: keptReference(sr.createdById, opts.memberUserIds),
+      editUnlockedAt: sr.editUnlockedAt ? toSafeDate(sr.editUnlockedAt as string) : null,
+      editUnlockedById: keptReference(sr.editUnlockedById, opts.memberUserIds),
       // Only a technician this import restored: a job pointing at one the
       // backup does not carry would be refused, and take the import with it.
       technicianId: keptReference(sr.technicianId, opts.technicianIds),
@@ -315,7 +341,7 @@ async function importServiceRecordTree(
         id: p.id as string,
         partNumber: (p.partNumber as string) || null,
         name: p.name as string,
-        quantity: (p.quantity as number) || 1,
+        quantity: numberOr(p.quantity, 1),
         unit: (p.unit as string) || null,
         category: (p.category as string) || null,
         unitPrice: (p.unitPrice as number) || 0,
@@ -345,10 +371,13 @@ async function importServiceRecordTree(
         cause: (c.cause as string) || null,
         correction: (c.correction as string) || null,
         confirmation: (c.confirmation as string) || null,
-        // When it was confirmed comes back; who does not. The id belongs to
-        // an account in the installation the backup was taken from, and the
-        // same person here has another one, if they are here at all.
+        // When it was confirmed always comes back; who, only when that person
+        // is in this workshop. A backup brought from another installation
+        // names an account that is not here.
         confirmedAt: c.confirmedAt ? (toSafeDate(c.confirmedAt as string) ?? null) : null,
+        confirmedById: keptReference(c.confirmedById, opts.memberUserIds),
+        createdAt: toSafeDate(c.createdAt as string),
+        updatedAt: toSafeDate(c.updatedAt as string),
         serviceRecordId: sr.id as string,
       })),
     })
@@ -457,6 +486,10 @@ async function importServiceRecordTree(
         date: toSafeDate(p.date as string),
         method: (p.method as string) || 'other',
         note: (p.note as string) || null,
+        // Which payment provider took it and under what reference. This pair
+        // is how a webhook delivered twice is told from a second payment.
+        provider: (p.provider as string) || null,
+        externalId: (p.externalId as string) || null,
         createdAt: toSafeDate(p.createdAt as string),
         updatedAt: toSafeDate(p.updatedAt as string),
         serviceRecordId: sr.id as string,
@@ -737,6 +770,8 @@ export async function restoreBackup(
                 d.template ?? {},
                 ctx.organizationId
               ) as Prisma.InputJsonValue,
+              // The rule that picks this design by itself, for counter sales.
+              autoRule: (d.autoRule as string) || null,
               createdAt: toSafeDate(d.createdAt as string),
               updatedAt: toSafeDate(d.updatedAt as string),
             })),
@@ -814,12 +849,22 @@ export async function restoreBackup(
             data: (data.customers as Record<string, unknown>[]).map(
               (c: Record<string, unknown>) => ({
                 id: c.id as string,
+                customerNumber: (c.customerNumber as string) || null,
                 name: c.name as string,
                 email: (c.email as string) || null,
                 phone: (c.phone as string) || null,
                 address: (c.address as string) || null,
                 company: (c.company as string) || null,
                 notes: (c.notes as string) || null,
+                // What the invoice prints and charges by: a tax number, and
+                // whether this customer pays tax at all.
+                taxId: (c.taxId as string) || null,
+                taxExempt: (c.taxExempt as boolean) || false,
+                // A customer who asked not to be reminded stays unreminded.
+                reminderOptOut: (c.reminderOptOut as boolean) || false,
+                // The Telegram chat the customer linked; without it their
+                // messages arrive from nobody.
+                telegramChatId: (c.telegramChatId as string) || null,
                 invoiceDesignId: keptReference(c.invoiceDesignId, designIds),
                 createdAt: toSafeDate(c.createdAt as string),
                 updatedAt: toSafeDate(c.updatedAt as string),
@@ -862,7 +907,7 @@ export async function restoreBackup(
                 color: (t.color as string) || '#3b82f6',
                 isActive: t.isActive !== false,
                 sortOrder: (t.sortOrder as number) || 0,
-                dailyCapacity: (t.dailyCapacity as number) || 480,
+                dailyCapacity: numberOr(t.dailyCapacity, 480),
                 skills: (t.skills as string) || null,
                 // memberId from old backups is ignored: it has no FK to users.
                 userId:
@@ -888,6 +933,17 @@ export async function restoreBackup(
           ).map((t) => t.id)
         )
 
+        // Who is in this workshop, for the jobs and quotes that name the
+        // person who opened or unlocked them.
+        const memberUserIds = new Set(
+          (
+            await tx.organizationMember.findMany({
+              where: { organizationId: ctx.organizationId },
+              select: { userId: true },
+            })
+          ).map((m) => m.userId)
+        )
+
         // Each technician's time off, for the technicians this restore has.
         const absences = ((data.technicians as Record<string, unknown>[] | undefined) ?? [])
           .flatMap((t) => (t.absences as Record<string, unknown>[] | undefined) ?? [])
@@ -910,7 +966,7 @@ export async function restoreBackup(
                 color: (b.color as string) || '#64748b',
                 isActive: b.isActive !== false,
                 sortOrder: (b.sortOrder as number) || 0,
-                dailyCapacity: (b.dailyCapacity as number) || 480,
+                dailyCapacity: numberOr(b.dailyCapacity, 480),
                 createdAt: toSafeDate(b.createdAt as string),
                 updatedAt: toSafeDate(b.updatedAt as string),
                 organizationId: ctx.organizationId,
@@ -954,6 +1010,7 @@ export async function restoreBackup(
                 label: def.label as string,
                 fieldType: (def.fieldType as string) || 'text',
                 options: (def.options as string) || null,
+                defaultValue: (def.defaultValue as string) || null,
                 required: (def.required as boolean) || false,
                 entityType: def.entityType as string,
                 sortOrder: (def.sortOrder as number) || 0,
@@ -988,6 +1045,7 @@ export async function restoreBackup(
               (p: Record<string, unknown>) => ({
                 id: p.id as string,
                 partNumber: (p.partNumber as string) || null,
+                barcode: (p.barcode as string) || null,
                 name: p.name as string,
                 description: (p.description as string) || null,
                 category: (p.category as string) || null,
@@ -1007,6 +1065,10 @@ export async function restoreBackup(
                 tags: Array.isArray(p.tags)
                   ? (p.tags as unknown[]).filter((tag): tag is string => typeof tag === 'string')
                   : [],
+                // When the low stock alert went out, so it is not sent again.
+                lowStockAlertedAt: p.lowStockAlertedAt
+                  ? (toSafeDate(p.lowStockAlertedAt as string) ?? null)
+                  : null,
                 createdAt: toSafeDate(p.createdAt as string),
                 updatedAt: toSafeDate(p.updatedAt as string),
                 userId: ctx.userId,
@@ -1060,9 +1122,17 @@ export async function restoreBackup(
                 tsn: (v.tsn as string) || null,
                 bodyType: (v.bodyType as string) || null,
                 purchaseDate: toSafeDate(v.purchaseDate as string) ?? null,
-                purchasePrice: (v.purchasePrice as number) || null,
+                purchasePrice: numberOr(v.purchasePrice, null),
                 imageUrl: rewriteFileUrl(v.imageUrl as string, ctx.organizationId),
                 isArchived: (v.isArchived as boolean) || false,
+                archiveReason: (v.archiveReason as string) || null,
+                soldReportedAt: v.soldReportedAt
+                  ? (toSafeDate(v.soldReportedAt as string) ?? null)
+                  : null,
+                maintenanceDismissed: (v.maintenanceDismissed as boolean) || false,
+                maintenanceDismissedAt: v.maintenanceDismissedAt
+                  ? (toSafeDate(v.maintenanceDismissedAt as string) ?? null)
+                  : null,
                 createdAt: toSafeDate(v.createdAt as string),
                 updatedAt: toSafeDate(v.updatedAt as string),
                 userId: ctx.userId,
@@ -1117,10 +1187,13 @@ export async function restoreBackup(
                   title: r.title as string,
                   description: (r.description as string) || null,
                   dueDate: toSafeDate(r.dueDate as string) ?? null,
-                  dueMileage: (r.dueMileage as number) || null,
+                  hasDueTime: (r.hasDueTime as boolean) || false,
+                  dueMileage: numberOr(r.dueMileage, null),
                   isCompleted: (r.isCompleted as boolean) || false,
                   notifyInApp: (r.notifyInApp as boolean) ?? true,
                   notifyEmail: (r.notifyEmail as boolean) ?? false,
+                  // Already announced: left out, a restore announced it again.
+                  notifiedAt: r.notifiedAt ? (toSafeDate(r.notifiedAt as string) ?? null) : null,
                   createdAt: toSafeDate(r.createdAt as string),
                   updatedAt: toSafeDate(r.updatedAt as string),
                   vehicleId: v.id as string,
@@ -1137,11 +1210,14 @@ export async function restoreBackup(
                 await importServiceRecordTree(tx, sr, {
                   organizationId: ctx.organizationId,
                   vehicleId: v.id as string,
-                  customerId: null,
+                  // Most vehicle jobs have none and follow the vehicle's
+                  // customer; one booked online names who booked it.
+                  customerId: (sr.customerId as string) || null,
                   workDayStartTime,
                   timeZone,
                   technicianIds,
                   workOrderStatusIds,
+                  memberUserIds,
                   designIds,
                   designSnapshotIds,
                   assetSnapshotIds,
@@ -1206,10 +1282,12 @@ export async function restoreBackup(
               title: r.title as string,
               description: (r.description as string) || null,
               dueDate: toSafeDate(r.dueDate as string) ?? null,
-              dueMileage: (r.dueMileage as number) || null,
+              hasDueTime: (r.hasDueTime as boolean) || false,
+              dueMileage: numberOr(r.dueMileage, null),
               isCompleted: (r.isCompleted as boolean) || false,
               notifyInApp: (r.notifyInApp as boolean) ?? true,
               notifyEmail: (r.notifyEmail as boolean) ?? false,
+              notifiedAt: r.notifiedAt ? (toSafeDate(r.notifiedAt as string) ?? null) : null,
               createdAt: toSafeDate(r.createdAt as string),
               updatedAt: toSafeDate(r.updatedAt as string),
               vehicleId: null,
@@ -1231,6 +1309,7 @@ export async function restoreBackup(
               timeZone,
               technicianIds,
               workOrderStatusIds,
+              memberUserIds,
               designIds,
               designSnapshotIds,
               assetSnapshotIds,
@@ -1353,6 +1432,22 @@ export async function restoreBackup(
                 totalAmount: (q.totalAmount as number) || 0,
                 notes: (q.notes as string) || null,
                 ...warrantyFromUntyped(q),
+                // The link the customer already holds. Left out, every shared
+                // quote answered "not found" after a restore.
+                publicToken: (q.publicToken as string) || null,
+                sharedAt: toSafeDate(q.sharedAt as string) ?? null,
+                shareExpiresAt: toSafeDate(q.shareExpiresAt as string) ?? null,
+                shareNeverExpires: q.shareNeverExpires === true,
+                shareRenewedAt: toSafeDate(q.shareRenewedAt as string) ?? null,
+                viewCount: (q.viewCount as number) || 0,
+                lastViewedAt: q.lastViewedAt ? toSafeDate(q.lastViewedAt as string) : null,
+                customerMessage: (q.customerMessage as string) || null,
+                sentAt: q.sentAt ? toSafeDate(q.sentAt as string) : null,
+                responseDismissedAt: q.responseDismissedAt
+                  ? toSafeDate(q.responseDismissedAt as string)
+                  : null,
+                editUnlockedAt: q.editUnlockedAt ? toSafeDate(q.editUnlockedAt as string) : null,
+                editUnlockedById: keptReference(q.editUnlockedById, memberUserIds),
                 convertedToId: (q.convertedToId as string) || null,
                 createdAt: toSafeDate(q.createdAt as string),
                 updatedAt: toSafeDate(q.updatedAt as string),
@@ -1371,7 +1466,7 @@ export async function restoreBackup(
                   id: p.id as string,
                   partNumber: (p.partNumber as string) || null,
                   name: p.name as string,
-                  quantity: (p.quantity as number) || 1,
+                  quantity: numberOr(p.quantity, 1),
                   unit: (p.unit as string) || null,
                   category: (p.category as string) || null,
                   unitPrice: (p.unitPrice as number) || 0,
@@ -1501,7 +1596,7 @@ export async function restoreBackup(
               data: {
                 id: insp.id as string,
                 status: (insp.status as string) || 'in_progress',
-                mileage: (insp.mileage as number) || null,
+                mileage: numberOr(insp.mileage, null),
                 notes: (insp.notes as string) || null,
                 startDateTime: toSafeDate(insp.startDateTime as string) ?? null,
                 endDateTime: toSafeDate(insp.endDateTime as string) ?? null,
@@ -1625,28 +1720,47 @@ export async function restoreBackup(
 
         // 10b. The jobs and quotes raised from an inspection point back at it.
         // Both were restored before the inspections existed, so the link is
-        // written now, and only where the inspection came back with the backup.
-        if (data.inspections?.length) {
+        // written now, to any inspection the workshop holds at this point:
+        // one that came back with the backup, or one that was never cleared
+        // because the backup did not carry inspections at all.
+        {
           const restoredInspections = new Set(
-            (data.inspections as Record<string, unknown>[]).map((insp) => insp.id as string)
+            (
+              await tx.inspection.findMany({
+                where: { organizationId: ctx.organizationId },
+                select: { id: true },
+              })
+            ).map((insp) => insp.id)
           )
           const linked = (rows: unknown) =>
             ((rows as Record<string, unknown>[] | undefined) ?? []).filter(
               (row) =>
                 typeof row.inspectionId === 'string' && restoredInspections.has(row.inspectionId)
             )
-          for (const vehicle of (data.vehicles as Record<string, unknown>[] | undefined) ?? []) {
-            for (const sr of linked(vehicle.serviceRecords)) {
-              await tx.serviceRecord.updateMany({
-                where: { id: sr.id as string, organizationId: ctx.organizationId },
-                data: { inspectionId: sr.inspectionId as string },
-              })
-            }
+          const jobs = [
+            ...((data.vehicles as Record<string, unknown>[] | undefined) ?? []).flatMap((vehicle) =>
+              linked(vehicle.serviceRecords)
+            ),
+            ...linked(data.counterSales),
+          ]
+          // The row's own "last changed" is written with the link: left to the
+          // database, every linked job and quote came back changed today.
+          for (const sr of jobs) {
+            await tx.serviceRecord.updateMany({
+              where: { id: sr.id as string, organizationId: ctx.organizationId },
+              data: {
+                inspectionId: sr.inspectionId as string,
+                updatedAt: toSafeDate(sr.updatedAt as string),
+              },
+            })
           }
           for (const q of linked(data.quotes)) {
             await tx.quote.updateMany({
               where: { id: q.id as string, organizationId: ctx.organizationId },
-              data: { inspectionId: q.inspectionId as string },
+              data: {
+                inspectionId: q.inspectionId as string,
+                updatedAt: toSafeDate(q.updatedAt as string),
+              },
             })
           }
         }
@@ -1654,8 +1768,8 @@ export async function restoreBackup(
         // 10c. The condition map's marks. They hang off the vehicle but name
         // the inspection check or the job they were drawn on, so they come
         // after both; a sheet the backup did not carry leaves the mark with no
-        // sheet rather than failing the import. The people who drew and cleared
-        // them have no account on this instance.
+        // sheet rather than failing the import. Who drew and cleared them
+        // comes back when that person is in this workshop.
         if (data.vehicles?.length) {
           const marks = (data.vehicles as Record<string, unknown>[]).flatMap((vehicle) =>
             ((vehicle.conditionMarks as Record<string, unknown>[] | undefined) ?? []).map(
@@ -1687,6 +1801,7 @@ export async function restoreBackup(
             const restoredInspectionIds = new Set(inspectionRows.map((row) => row.id))
             const restoredItemIds = new Set(itemRows.map((row) => row.id))
             const restoredJobIds = new Set(jobRows.map((row) => row.id))
+            const restoredMarkIds = new Set(marks.map(({ mark }) => mark.id as string))
             await tx.conditionMark.createMany({
               data: marks.map(({ mark, vehicleId: markVehicleId }) => ({
                 id: mark.id as string,
@@ -1707,9 +1822,12 @@ export async function restoreBackup(
                   (url) => rewriteFileUrl(url, ctx.organizationId) ?? url
                 ),
                 recordedAt: toSafeDate(mark.recordedAt as string),
-                recordedById: null,
+                recordedById: keptReference(mark.recordedById, memberUserIds),
                 resolvedAt: toSafeDate(mark.resolvedAt as string) ?? null,
-                resolvedById: null,
+                resolvedById: keptReference(mark.resolvedById, memberUserIds),
+                // The newer mark this one was recorded again as. Without it
+                // the same dent shows twice on the vehicle.
+                carriedToId: keptReference(mark.carriedToId, restoredMarkIds),
                 createdAt: toSafeDate(mark.createdAt as string),
                 updatedAt: toSafeDate(mark.updatedAt as string),
               })),
@@ -1732,7 +1850,9 @@ export async function restoreBackup(
                   log.metadata != null ? (log.metadata as Prisma.InputJsonValue) : Prisma.DbNull,
                 ip: (log.ip as string) || null,
                 userAgent: (log.userAgent as string) || null,
-                userId: ctx.userId,
+                // Who did it, when that person is in this workshop; an entry
+                // by somebody who is not goes to whoever restores, as before.
+                userId: keptReference(log.userId, memberUserIds) ?? ctx.userId,
                 organizationId: ctx.organizationId,
               })
             ),
@@ -1754,6 +1874,9 @@ export async function restoreBackup(
                 errorMessage: (msg.errorMessage as string) || null,
                 relatedEntityType: (msg.relatedEntityType as string) || null,
                 relatedEntityId: (msg.relatedEntityId as string) || null,
+                // Read stays read: left out, every text ever received came
+                // back as unread.
+                readAt: msg.readAt ? (toSafeDate(msg.readAt as string) ?? null) : null,
                 createdAt: toSafeDate(msg.createdAt as string),
                 updatedAt: toSafeDate(msg.updatedAt as string),
                 organizationId: ctx.organizationId,
@@ -2228,7 +2351,7 @@ export async function restoreBackup(
                 withRims: (set.withRims as boolean) || false,
                 rimType: (set.rimType as string) || null,
                 hasTpms: (set.hasTpms as boolean) || false,
-                quantity: (set.quantity as number) || 4,
+                quantity: numberOr(set.quantity, 4),
                 status: (set.status as string) || 'stored',
                 notes: (set.notes as string) || null,
                 checkedInAt: set.checkedInAt ? toSafeDate(set.checkedInAt as string) : null,
@@ -2243,6 +2366,30 @@ export async function restoreBackup(
               },
             })
 
+            // Movements first: a reading names the movement it was taken at.
+            const movements = set.movements as Record<string, unknown>[] | undefined
+            if (movements?.length) {
+              await tx.tireMovement.createMany({
+                data: movements.map((mv) => ({
+                  id: mv.id as string,
+                  type: mv.type as string,
+                  fromCode: (mv.fromCode as string) || null,
+                  toCode: (mv.toCode as string) || null,
+                  note: (mv.note as string) || null,
+                  createdAt: toSafeDate(mv.createdAt as string),
+                  tireSetId: set.id as string,
+                  // The codes above carry the history in readable form; the
+                  // links come back for the shelves that came across, and
+                  // who moved the set when that person is in this workshop.
+                  fromLocationId: keptReference(mv.fromLocationId, knownLocations),
+                  toLocationId: keptReference(mv.toLocationId, knownLocations),
+                  performedById: keptReference(mv.performedById, memberUserIds),
+                  organizationId: ctx.organizationId,
+                })),
+              })
+            }
+
+            const restoredMovements = new Set((movements ?? []).map((mv) => mv.id as string))
             const measurements = set.measurements as Record<string, unknown>[] | undefined
             if (measurements?.length) {
               await tx.tireMeasurement.createMany({
@@ -2257,9 +2404,10 @@ export async function restoreBackup(
                   measuredAt: toSafeDate(m.measuredAt as string),
                   tireSetId: set.id as string,
                   // The person who took the reading may not exist here, and a
-                  // wrong name on a condition record is worse than none.
-                  measuredById: null,
-                  movementId: null,
+                  // wrong name on a condition record is worse than none: kept
+                  // only when they are in this workshop.
+                  measuredById: keptReference(m.measuredById, memberUserIds),
+                  movementId: keptReference(m.movementId, restoredMovements),
                 })),
               })
 
@@ -2277,28 +2425,6 @@ export async function restoreBackup(
               }
             }
 
-            const movements = set.movements as Record<string, unknown>[] | undefined
-            if (movements?.length) {
-              await tx.tireMovement.createMany({
-                data: movements.map((mv) => ({
-                  id: mv.id as string,
-                  type: mv.type as string,
-                  fromCode: (mv.fromCode as string) || null,
-                  toCode: (mv.toCode as string) || null,
-                  note: (mv.note as string) || null,
-                  createdAt: toSafeDate(mv.createdAt as string),
-                  tireSetId: set.id as string,
-                  // The codes above carry the history in readable form, so the
-                  // relations are left off rather than pointed at shelves that
-                  // may not have come across.
-                  fromLocationId: null,
-                  toLocationId: null,
-                  performedById: null,
-                  organizationId: ctx.organizationId,
-                })),
-              })
-            }
-
             const attachments = set.attachments as Record<string, unknown>[] | undefined
             if (attachments?.length) {
               await tx.tireSetAttachment.createMany({
@@ -2315,7 +2441,7 @@ export async function restoreBackup(
                   tireSetId: set.id as string,
                   organizationId: ctx.organizationId,
                   // The uploader may not exist in the target organization.
-                  uploadedById: null,
+                  uploadedById: keptReference(att.uploadedById, memberUserIds),
                 })),
               })
             }
@@ -2329,7 +2455,7 @@ export async function restoreBackup(
                   status: (tr.status as string) || 'pending',
                   notes: (tr.notes as string) || null,
                   completedAt: tr.completedAt ? toSafeDate(tr.completedAt as string) : null,
-                  completedById: null,
+                  completedById: keptReference(tr.completedById, memberUserIds),
                   createdAt: toSafeDate(tr.createdAt as string),
                   updatedAt: toSafeDate(tr.updatedAt as string),
                   tireSetId: set.id as string,
@@ -2339,6 +2465,50 @@ export async function restoreBackup(
             }
           }
         }
+
+        // The jobs and quotes raised for a stored set point back at it. Both
+        // were restored before the sets existed, so the link is written now,
+        // to any set the workshop holds at this point: one that came back with
+        // the backup, or one that was never cleared because the backup did not
+        // carry the tire hotel.
+        {
+          const restoredTireSets = new Set(
+            (
+              await tx.tireSet.findMany({
+                where: { organizationId: ctx.organizationId },
+                select: { id: true },
+              })
+            ).map((set) => set.id)
+          )
+          const aboutASet = (rows: unknown) =>
+            ((rows as Record<string, unknown>[] | undefined) ?? []).filter(
+              (row) => typeof row.tireSetId === 'string' && restoredTireSets.has(row.tireSetId)
+            )
+          const jobs = [
+            ...((data.vehicles as Record<string, unknown>[] | undefined) ?? []).flatMap((vehicle) =>
+              aboutASet(vehicle.serviceRecords)
+            ),
+            ...aboutASet(data.counterSales),
+          ]
+          for (const sr of jobs) {
+            await tx.serviceRecord.updateMany({
+              where: { id: sr.id as string, organizationId: ctx.organizationId },
+              data: {
+                tireSetId: sr.tireSetId as string,
+                updatedAt: toSafeDate(sr.updatedAt as string),
+              },
+            })
+          }
+          for (const q of aboutASet(data.quotes)) {
+            await tx.quote.updateMany({
+              where: { id: q.id as string, organizationId: ctx.organizationId },
+              data: {
+                tireSetId: q.tireSetId as string,
+                updatedAt: toSafeDate(q.updatedAt as string),
+              },
+            })
+          }
+        }
       },
       // A restore of years of records takes minutes, not Prisma's five seconds.
       { maxWait: 30_000, timeout: TRANSACTION_TIMEOUT_MS }

+ 2 - 0
src/lib/backup/manifest.ts

@@ -442,6 +442,8 @@ export const EXCLUDED_MODELS: Readonly<Record<string, string>> = {
   RepairDataCache:
     "Labor times and fluid specs kept from a repair data source while the workshop's key is on a paid plan. They belong to the connection, which a backup does not carry, and may only be kept while that plan lasts; the guide fetches them again when needed.",
   OrganizationMember: 'Membership of user accounts; people are restored by inviting them.',
+  MemberSignature:
+    "A member's own signature, which belongs to the membership a backup does not carry. What an issued document was signed with is in the backup, as a frozen copy on the document; the member draws a new one after joining again.",
   PlatformMailUsage:
     "Today's count of mail sent through the platform address, for the daily cap; it starts again every day.",
   PlatformSmsUsage:

+ 4 - 0
src/lib/backup/tenancy.ts

@@ -79,6 +79,10 @@ function checkedReferences(model: string, row: Row): [string, Reference][] {
       continue
     }
     const target = column.charAt(0).toUpperCase() + column.slice(1, -2)
+    // A column named after its own model is the row's id somewhere else
+    // (`telegramMessageId` on a Telegram message is Telegram's number for
+    // it), never a link to another row; read as one, it was always cleared.
+    if (target === model) continue
     if (TENANCY_SCHEMA[target] && !UNCHECKED_TARGETS.has(target)) {
       out.push([column, { target, required: false }])
     }