Просмотр исходного кода

Restore a vehicle without a decodable year as unknown (0) (#635)

* Restore a vehicle without a decodable year as unknown (0)

A backup's vehicle rows are read without validation, and every field
but one has a fallback: mileage falls back to 0, nullable columns to
null, prices through numberOr. The year, the other required integer,
was cast as-is, so a row without a decodable year (a hand-edited or
foreign backup: year missing, null or written as text) threw a Prisma
error mid-transaction and rolled back the entire restore after minutes
of inserts.

The year now restores through a small coercion that keeps positive
integers (a text year is read as its number, as the vehicle form's own
coercion does) and turns anything else into the app's unknown-year
marker, 0, which the display already treats as "no year".

* Bound the restored vehicle year to the column's range

A year above the signed 32-bit maximum of the year column (a hand-edited
"2147483648") still failed the vehicle insert and rolled back the whole
restore. Such values now restore as 0, the unknown-year marker, and the
regression test gains an oversized numeric year and an oversized text
year.
Shadoweee77 3 дней назад
Родитель
Сommit
c7c0ec00c1
2 измененных файлов с 46 добавлено и 1 удалено
  1. 31 0
      src/__tests__/lib/backup/import-from-disk.test.ts
  2. 15 1
      src/lib/backup/import.ts

+ 31 - 0
src/__tests__/lib/backup/import-from-disk.test.ts

@@ -121,6 +121,37 @@ describe('restoreFromDisk', () => {
     expect(releaseFilesNotRestored).not.toHaveBeenCalled()
   })
 
+  it('restores a vehicle year as its number, or the unknown marker 0', async () => {
+    const dir = path.join(root, 'restored')
+    const at = '2026-01-01T00:00:00Z'
+    writeBackup(dir, {
+      version: 2,
+      data: {
+        vehicles: [
+          { id: 'v1', make: 'Ford', model: 'Edge', year: 2021, createdAt: at, updatedAt: at },
+          { id: 'v2', make: 'Ford', model: 'Focus', createdAt: at, updatedAt: at },
+          { id: 'v3', make: 'VW', model: 'Golf', year: '1998', createdAt: at, updatedAt: at },
+          { id: 'v4', make: 'Opel', model: 'Astra', year: 0, createdAt: at, updatedAt: at },
+          // Above the year column's signed 32-bit maximum: a hand-edited
+          // backup must restore the vehicle as unknown, not roll the
+          // restore back when the insert rejects the value.
+          { id: 'v5', make: 'Ford', model: 'S-Max', year: 2147483648, createdAt: at, updatedAt: at },
+          { id: 'v6', make: 'Skoda', model: 'Octavia', year: '3000000000', createdAt: at, updatedAt: at },
+        ],
+      },
+    })
+
+    await restoreFromDisk(actor, { dataFile: path.join(dir, 'stage', 'data.json'), filesDir: null })
+
+    // A missing, text, zero or out-of-column-range year all restore as
+    // the app's unknown-year marker instead of failing the restore on
+    // the vehicle's row.
+    const years = state.calls
+      .filter((c) => c.model === 'vehicle' && c.method === 'create')
+      .map((c) => c.args.data.year)
+    expect(years).toEqual([2021, 0, 1998, 0, 0, 0])
+  })
+
   it('refuses a file that is not a backup before any row is touched', async () => {
     const dir = path.join(root, 'restored')
     writeBackup(dir, { version: 3, data: {} })

+ 15 - 1
src/lib/backup/import.ts

@@ -177,6 +177,20 @@ function numberOr<T>(value: unknown, fallback: T): number | T {
   return typeof value === 'number' && Number.isFinite(value) ? value : fallback
 }
 
+/**
+ * A vehicle's year as the backup holds it. The column is a non-null
+ * integer, and 0 is the app's unknown-year marker, so a row without a
+ * decodable year restores as 0 instead of failing the whole restore
+ * mid-transaction; a year written as text (a hand-edited backup) is read
+ * as its number, as the vehicle form's own coercion does. Values above
+ * the column's signed 32-bit maximum are rejected the same way: they
+ * would still fail the insert and roll the restore back.
+ */
+function restoredVehicleYear(value: unknown): number {
+  const n = typeof value === 'number' ? value : typeof value === 'string' ? Number(value) : NaN
+  return Number.isInteger(n) && n > 0 && n <= 2147483647 ? n : 0
+}
+
 /** 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
@@ -1112,7 +1126,7 @@ export async function restoreBackup(
                 id: v.id as string,
                 make: v.make as string,
                 model: v.model as string,
-                year: v.year as number,
+                year: restoredVehicleYear(v.year),
                 vin: (v.vin as string) || null,
                 licensePlate: normalizePlate(v.licensePlate as string | null | undefined),
                 color: (v.color as string) || null,