61 lines
2.0 KiB
JavaScript
61 lines
2.0 KiB
JavaScript
/// <reference path="../pb_data/types.d.ts" />
|
|
//
|
|
// M2 — Add integer-minutes `estimatedDuration` column to `repairOrders`.
|
|
//
|
|
// Today the duration is stored as `estimatedTime` (a HOURS STRING such as
|
|
// "0.3"), converted back to minutes via Math.round(hours*60) on read. That
|
|
// round-trip is lossy: 15 min → "0.3" → 18 min, drifting the due time every
|
|
// reload. This migration adds a numeric `estimatedDuration` (integer minutes)
|
|
// column and backfills it from `estimatedTime`.
|
|
//
|
|
// The frontend still writes BOTH columns after this migration (for backward
|
|
// compat with older clients), but reads prefer `estimatedDuration` when
|
|
// present. A follow-up cleanup can drop the `estimatedTime` writes once all
|
|
// clients are upgraded.
|
|
//
|
|
migrate(
|
|
(db) => {
|
|
const col = db.findCollectionByNameOrId('repairOrders');
|
|
if (!col) {
|
|
console.warn('[M2] repairOrders collection not found — skipping');
|
|
return;
|
|
}
|
|
|
|
if (!col.fields.find((f) => f.name === 'estimatedDuration')) {
|
|
const field = new NumberField({
|
|
name: 'estimatedDuration',
|
|
required: false,
|
|
min: 0,
|
|
});
|
|
col.fields.push(field);
|
|
db.save(col);
|
|
console.log('[M2] added estimatedDuration field');
|
|
}
|
|
|
|
const records = Array.from(db.findRecordsByFilter(
|
|
"repairOrders",
|
|
"1=1",
|
|
"id",
|
|
500
|
|
));
|
|
let backfilled = 0;
|
|
for (const rec of records) {
|
|
if (rec.get('estimatedDuration') != null) continue; // don't overwrite
|
|
const raw = rec.get('estimatedTime');
|
|
const hours = parseFloat(String(raw || '0')) || 0;
|
|
rec.set('estimatedDuration', Math.round(hours * 60));
|
|
db.saveRecord(rec);
|
|
backfilled++;
|
|
}
|
|
console.log(`[M2] backfilled ${backfilled} records`);
|
|
},
|
|
(db) => {
|
|
const col = db.findCollectionByNameOrId('repairOrders');
|
|
if (!col) return;
|
|
const idx = col.fields.findIndex((f) => f.name === 'estimatedDuration');
|
|
if (idx >= 0) {
|
|
col.fields.splice(idx, 1);
|
|
db.save(col);
|
|
}
|
|
}
|
|
); |