mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
fix(release): harden v1.31 arrival contracts
This commit is contained in:
1 parent
44add3bc1e
commit
4591899657
98 files changed
+5560
-1600
No files matched your search
@@ -19,7 +19,7 @@ vi.mock("@/lib/db", () => ({
|
||||
prisma: {
|
||||
measurement: {
|
||||
findMany: vi.fn(),
|
||||
createMany: vi.fn(),
|
||||
createManyAndReturn: vi.fn(),
|
||||
updateMany: vi.fn(),
|
||||
},
|
||||
$transaction: vi.fn(async (fn: unknown) => {
|
||||
@@ -129,13 +129,26 @@ beforeEach(() => {
|
||||
resetAt: Date.now() + 60_000,
|
||||
});
|
||||
vi.mocked(prisma.measurement.findMany).mockResolvedValue([]);
|
||||
vi.mocked(prisma.measurement.createMany).mockResolvedValue({ count: 0 });
|
||||
vi.mocked(prisma.measurement.createManyAndReturn).mockImplementation((async (
|
||||
args: unknown,
|
||||
) => {
|
||||
const { data } = args as {
|
||||
data:
|
||||
| { type: unknown; source: string; externalId: string | null }
|
||||
| Array<{ type: unknown; source: string; externalId: string | null }>;
|
||||
};
|
||||
const rows = Array.isArray(data) ? data : [data];
|
||||
return rows.map(({ type, source, externalId }) => ({
|
||||
type,
|
||||
source,
|
||||
externalId,
|
||||
}));
|
||||
}) as never);
|
||||
vi.mocked(prisma.measurement.updateMany).mockResolvedValue({ count: 1 });
|
||||
});
|
||||
|
||||
describe("POST /api/measurements/batch — aggregated HR wire contract (iOS #34)", () => {
|
||||
it("inserts a fresh 10-min HR bucket as a PULSE row", async () => {
|
||||
vi.mocked(prisma.measurement.createMany).mockResolvedValue({ count: 1 });
|
||||
const res = await POST(
|
||||
makeRequest({ entries: [hrBucketEntry(HR_BUCKET_ID, 72)] }),
|
||||
);
|
||||
@@ -146,7 +159,8 @@ describe("POST /api/measurements/batch — aggregated HR wire contract (iOS #34)
|
||||
expect(data.entries[0].status).toBe("inserted");
|
||||
|
||||
// Stored as a PULSE row carrying the 10-min average value.
|
||||
const createArg = vi.mocked(prisma.measurement.createMany).mock.calls[0][0];
|
||||
const createArg = vi.mocked(prisma.measurement.createManyAndReturn).mock
|
||||
.calls[0][0];
|
||||
const rows = (createArg as { data: { type: string; value: number }[] })
|
||||
.data;
|
||||
expect(rows[0].type).toBe("PULSE");
|
||||
@@ -154,7 +168,6 @@ describe("POST /api/measurements/batch — aggregated HR wire contract (iOS #34)
|
||||
});
|
||||
|
||||
it("persists per-bucket min/max on a fresh 10-min HR bucket (iOS #34 ext)", async () => {
|
||||
vi.mocked(prisma.measurement.createMany).mockResolvedValue({ count: 1 });
|
||||
const res = await POST(
|
||||
makeRequest({
|
||||
entries: [
|
||||
@@ -163,7 +176,8 @@ describe("POST /api/measurements/batch — aggregated HR wire contract (iOS #34)
|
||||
}),
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
const createArg = vi.mocked(prisma.measurement.createMany).mock.calls[0][0];
|
||||
const createArg = vi.mocked(prisma.measurement.createManyAndReturn).mock
|
||||
.calls[0][0];
|
||||
const rows = (
|
||||
createArg as {
|
||||
data: { value: number; valueMin: number; valueMax: number }[];
|
||||
@@ -184,12 +198,11 @@ describe("POST /api/measurements/batch — aggregated HR wire contract (iOS #34)
|
||||
])(
|
||||
"v1.30.8 — drops an out-of-range or mis-ordered spread to null but keeps the average %o",
|
||||
async (spread) => {
|
||||
vi.mocked(prisma.measurement.createMany).mockResolvedValue({ count: 1 });
|
||||
const res = await POST(
|
||||
makeRequest({ entries: [hrBucketEntry(HR_BUCKET_ID, 72, spread)] }),
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
const createArg = vi.mocked(prisma.measurement.createMany).mock
|
||||
const createArg = vi.mocked(prisma.measurement.createManyAndReturn).mock
|
||||
.calls[0][0];
|
||||
const rows = (
|
||||
createArg as {
|
||||
@@ -231,7 +244,6 @@ describe("POST /api/measurements/batch — aggregated HR wire contract (iOS #34)
|
||||
});
|
||||
|
||||
it("leaves min/max null on a per-sample PULSE row even if sent", async () => {
|
||||
vi.mocked(prisma.measurement.createMany).mockResolvedValue({ count: 1 });
|
||||
const SAMPLE_ID = "C0FFEE00-0000-4000-8000-000000000000";
|
||||
const res = await POST(
|
||||
makeRequest({
|
||||
@@ -239,7 +251,8 @@ describe("POST /api/measurements/batch — aggregated HR wire contract (iOS #34)
|
||||
}),
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
const createArg = vi.mocked(prisma.measurement.createMany).mock.calls[0][0];
|
||||
const createArg = vi.mocked(prisma.measurement.createManyAndReturn).mock
|
||||
.calls[0][0];
|
||||
const rows = (
|
||||
createArg as {
|
||||
data: { valueMin: number | null; valueMax: number | null }[];
|
||||
@@ -273,7 +286,7 @@ describe("POST /api/measurements/batch — aggregated HR wire contract (iOS #34)
|
||||
expect(prisma.measurement.updateMany).toHaveBeenCalledTimes(1);
|
||||
const updateArg = vi.mocked(prisma.measurement.updateMany).mock.calls[0][0];
|
||||
expect((updateArg as { data: { value: number } }).data.value).toBe(78);
|
||||
expect(prisma.measurement.createMany).not.toHaveBeenCalled();
|
||||
expect(prisma.measurement.createManyAndReturn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("skips a malformed aggregated HR bucket externalId", async () => {
|
||||
@@ -293,7 +306,7 @@ describe("POST /api/measurements/batch — aggregated HR wire contract (iOS #34)
|
||||
expect(data.skipped).toEqual([
|
||||
{ index: 0, reason: "malformed_hr_bucket_id" },
|
||||
]);
|
||||
expect(prisma.measurement.createMany).not.toHaveBeenCalled();
|
||||
expect(prisma.measurement.createManyAndReturn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps the per-sample uuid HR path immutable (duplicate, not overwrite)", async () => {
|
||||
@@ -406,7 +419,7 @@ describe("POST /api/measurements/batch — stats tombstone resurrection", () =>
|
||||
expect(data.updated).toBe(0);
|
||||
expect(data.entries[0].status).toBe("duplicate");
|
||||
expect(prisma.measurement.updateMany).not.toHaveBeenCalled();
|
||||
expect(prisma.measurement.createMany).not.toHaveBeenCalled();
|
||||
expect(prisma.measurement.createManyAndReturn).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -421,8 +434,6 @@ describe("POST /api/measurements/batch — stats tombstone resurrection", () =>
|
||||
*/
|
||||
describe("POST /api/measurements/batch — intra-batch `stats:` supersession", () => {
|
||||
it("keeps only the LAST entry when one batch carries the same stats: key twice", async () => {
|
||||
vi.mocked(prisma.measurement.createMany).mockResolvedValue({ count: 1 });
|
||||
|
||||
const res = await POST(
|
||||
makeRequest({
|
||||
entries: [
|
||||
@@ -435,7 +446,8 @@ describe("POST /api/measurements/batch — intra-batch `stats:` supersession", (
|
||||
const { data } = await readJson(res);
|
||||
|
||||
// Exactly ONE row reaches the insert, carrying the NEWER value.
|
||||
const createArg = vi.mocked(prisma.measurement.createMany).mock.calls[0][0];
|
||||
const createArg = vi.mocked(prisma.measurement.createManyAndReturn).mock
|
||||
.calls[0][0];
|
||||
const rows = (createArg as { data: { value: number }[] }).data;
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0].value).toBe(78);
|
||||
@@ -479,7 +491,6 @@ describe("POST /api/measurements/batch — intra-batch `stats:` supersession", (
|
||||
it("leaves DISTINCT stats: keys in one batch untouched", async () => {
|
||||
const OTHER =
|
||||
"stats:HKQuantityTypeIdentifierHeartRate:2026-06-21T14:10:00.000Z";
|
||||
vi.mocked(prisma.measurement.createMany).mockResolvedValue({ count: 2 });
|
||||
|
||||
const res = await POST(
|
||||
makeRequest({
|
||||
@@ -489,7 +500,8 @@ describe("POST /api/measurements/batch — intra-batch `stats:` supersession", (
|
||||
expect(res.status).toBe(200);
|
||||
const { data } = await readJson(res);
|
||||
|
||||
const createArg = vi.mocked(prisma.measurement.createMany).mock.calls[0][0];
|
||||
const createArg = vi.mocked(prisma.measurement.createManyAndReturn).mock
|
||||
.calls[0][0];
|
||||
expect((createArg as { data: unknown[] }).data).toHaveLength(2);
|
||||
expect(data.inserted).toBe(2);
|
||||
expect(data.duplicates).toBe(0);
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
vi.mock("@/lib/db", () => ({
|
||||
prisma: {
|
||||
measurement: {
|
||||
findMany: vi.fn(),
|
||||
createManyAndReturn: vi.fn(),
|
||||
updateMany: vi.fn(),
|
||||
},
|
||||
$transaction: vi.fn(async (fn: unknown) => {
|
||||
if (typeof fn === "function") {
|
||||
return (fn as (tx: unknown) => unknown)(prisma);
|
||||
}
|
||||
}),
|
||||
},
|
||||
}));
|
||||
vi.mock("@/lib/auth/session", () => ({ getSession: vi.fn() }));
|
||||
vi.mock("@/lib/auth/audit", () => ({
|
||||
auditLog: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
vi.mock("@/lib/logging/transports", () => ({ emitIfSampled: vi.fn() }));
|
||||
vi.mock("@/lib/db-compat", () => ({
|
||||
ensureDbCompatibility: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
vi.mock("@/lib/rate-limit", () => ({ checkRateLimit: vi.fn() }));
|
||||
vi.mock("@/lib/jobs/pr-detection", () => ({
|
||||
enqueuePrDetection: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
vi.mock("@/lib/jobs/reminder-satisfy", () => ({
|
||||
enqueueReminderSatisfy: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
vi.mock("@/lib/cache/invalidate", () => ({
|
||||
invalidateUserMeasurements: vi.fn(),
|
||||
}));
|
||||
vi.mock("@/lib/insights/comprehensive-generate", () => ({
|
||||
invalidateStatusInsightsForTypes: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
vi.mock("@/lib/rollups/measurement-rollups", () => ({
|
||||
recomputeBucketsForMeasurement: vi.fn().mockResolvedValue(undefined),
|
||||
collapseToTypeDayKeys: vi.fn(() => []),
|
||||
}));
|
||||
vi.mock("@/lib/daily/morning-refresh-trigger", () => ({
|
||||
maybeEnqueueMorningRefresh: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
vi.mock("@/lib/arrivals/emit-shared", () => ({
|
||||
emitDataArrival: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
vi.mock("next/headers", () => ({
|
||||
headers: vi.fn(async () => ({ get: () => null })),
|
||||
cookies: vi.fn(async () => ({
|
||||
get: () => undefined,
|
||||
set: () => {},
|
||||
delete: () => {},
|
||||
})),
|
||||
}));
|
||||
|
||||
import { POST } from "../route";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { getSession } from "@/lib/auth/session";
|
||||
import { checkRateLimit } from "@/lib/rate-limit";
|
||||
import { maybeEnqueueMorningRefresh } from "@/lib/daily/morning-refresh-trigger";
|
||||
import { emitDataArrival } from "@/lib/arrivals/emit-shared";
|
||||
|
||||
const session = {
|
||||
session: { id: "session-1", expiresAt: new Date("2026-08-01T00:00:00Z") },
|
||||
user: { id: "user-1", username: "testuser", role: "USER" as const },
|
||||
};
|
||||
|
||||
function entry(
|
||||
hkIdentifier: string,
|
||||
value: number,
|
||||
unit: string,
|
||||
endDate: string,
|
||||
externalId: string,
|
||||
sleepStage?: number,
|
||||
) {
|
||||
return {
|
||||
hkIdentifier,
|
||||
value,
|
||||
unit,
|
||||
startDate: new Date(Date.parse(endDate) - 60_000).toISOString(),
|
||||
endDate,
|
||||
externalId,
|
||||
...(sleepStage === undefined ? {} : { sleepStage }),
|
||||
};
|
||||
}
|
||||
|
||||
function request(entries: unknown[]): NextRequest {
|
||||
return new NextRequest("http://localhost/api/measurements/batch", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ entries }),
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(getSession).mockResolvedValue(session as never);
|
||||
vi.mocked(checkRateLimit).mockResolvedValue({
|
||||
allowed: true,
|
||||
remaining: 59,
|
||||
resetAt: Date.now() + 60_000,
|
||||
});
|
||||
vi.mocked(prisma.measurement.findMany).mockResolvedValue([]);
|
||||
vi.mocked(prisma.measurement.updateMany).mockResolvedValue({ count: 0 });
|
||||
});
|
||||
|
||||
describe("POST /api/measurements/batch — exact INSERT RETURNING identity", () => {
|
||||
it("emits and refreshes only exact inserted rows when mixed kinds lose insert races", async () => {
|
||||
const weightAt = "2026-07-19T07:00:00.000Z";
|
||||
const insertedSleepAt = "2026-07-19T06:30:00.000Z";
|
||||
const racedSleepAt = "2026-07-19T06:45:00.000Z";
|
||||
const racedBpAt = "2026-07-19T07:15:00.000Z";
|
||||
const entries = [
|
||||
entry(
|
||||
"HKQuantityTypeIdentifierBodyMass",
|
||||
80,
|
||||
"kg",
|
||||
weightAt,
|
||||
"weight-inserted",
|
||||
),
|
||||
entry(
|
||||
"HKCategoryTypeIdentifierSleepAnalysis",
|
||||
90,
|
||||
"min",
|
||||
insertedSleepAt,
|
||||
"sleep-inserted",
|
||||
3,
|
||||
),
|
||||
entry(
|
||||
"HKCategoryTypeIdentifierSleepAnalysis",
|
||||
45,
|
||||
"min",
|
||||
racedSleepAt,
|
||||
"sleep-raced",
|
||||
3,
|
||||
),
|
||||
entry(
|
||||
"HKQuantityTypeIdentifierBloodPressureSystolic",
|
||||
120,
|
||||
"mmHg",
|
||||
racedBpAt,
|
||||
"bp-raced",
|
||||
),
|
||||
];
|
||||
|
||||
vi.mocked(prisma.measurement.createManyAndReturn).mockImplementation(
|
||||
(async (args: unknown) => {
|
||||
const { data } = args as {
|
||||
data:
|
||||
| { type: unknown; source: string; externalId: string | null }
|
||||
| Array<{
|
||||
type: unknown;
|
||||
source: string;
|
||||
externalId: string | null;
|
||||
}>;
|
||||
};
|
||||
const rows = Array.isArray(data) ? data : [data];
|
||||
return rows
|
||||
.filter(
|
||||
(row) =>
|
||||
typeof row.externalId === "string" &&
|
||||
["weight-inserted", "sleep-inserted"].includes(row.externalId),
|
||||
)
|
||||
.reverse()
|
||||
.map(({ type, source, externalId }) => ({
|
||||
type,
|
||||
source,
|
||||
externalId,
|
||||
}));
|
||||
}) as never,
|
||||
);
|
||||
|
||||
const response = await POST(request(entries));
|
||||
expect(response.status).toBe(200);
|
||||
const body = (await response.json()) as {
|
||||
data: {
|
||||
inserted: number;
|
||||
duplicates: number;
|
||||
entries: Array<{ index: number; status: string }>;
|
||||
};
|
||||
};
|
||||
|
||||
expect(body.data.inserted).toBe(2);
|
||||
expect(body.data.duplicates).toBe(2);
|
||||
expect(body.data.entries.map(({ status }) => status)).toEqual([
|
||||
"inserted",
|
||||
"inserted",
|
||||
"duplicate",
|
||||
"duplicate",
|
||||
]);
|
||||
expect(prisma.measurement.createManyAndReturn).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
skipDuplicates: true,
|
||||
select: { type: true, source: true, externalId: true },
|
||||
}),
|
||||
);
|
||||
expect(maybeEnqueueMorningRefresh).toHaveBeenCalledWith("user-1", [
|
||||
new Date(insertedSleepAt),
|
||||
]);
|
||||
expect(emitDataArrival).toHaveBeenCalledTimes(1);
|
||||
expect(emitDataArrival).toHaveBeenCalledWith({
|
||||
userId: "user-1",
|
||||
kind: "weight",
|
||||
newestSampleAt: new Date(weightAt),
|
||||
insertedCount: 1,
|
||||
source: "batch",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -12,7 +12,7 @@ vi.mock("@/lib/db", () => ({
|
||||
prisma: {
|
||||
measurement: {
|
||||
findMany: vi.fn(),
|
||||
createMany: vi.fn(),
|
||||
createManyAndReturn: vi.fn(),
|
||||
},
|
||||
$transaction: vi.fn(async (fn: unknown) => {
|
||||
if (typeof fn === "function") {
|
||||
@@ -83,7 +83,7 @@ function validStepEntry(externalId: string) {
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
|
||||
vi.mocked(checkRateLimit).mockResolvedValue({
|
||||
allowed: true,
|
||||
@@ -91,12 +91,25 @@ beforeEach(() => {
|
||||
resetAt: Date.now() + 60_000,
|
||||
});
|
||||
vi.mocked(prisma.measurement.findMany).mockResolvedValue([]);
|
||||
vi.mocked(prisma.measurement.createMany).mockResolvedValue({ count: 0 });
|
||||
vi.mocked(prisma.measurement.createManyAndReturn).mockImplementation((async (
|
||||
args: unknown,
|
||||
) => {
|
||||
const { data } = args as {
|
||||
data:
|
||||
| { type: unknown; source: string; externalId: string | null }
|
||||
| Array<{ type: unknown; source: string; externalId: string | null }>;
|
||||
};
|
||||
const rows = Array.isArray(data) ? data : [data];
|
||||
return rows.map(({ type, source, externalId }) => ({
|
||||
type,
|
||||
source,
|
||||
externalId,
|
||||
}));
|
||||
}) as never);
|
||||
});
|
||||
|
||||
describe("POST /api/measurements/batch — PR detection enqueue (v1.4.25 W16c)", () => {
|
||||
it("enqueues with silent=false for a small healthy batch", async () => {
|
||||
vi.mocked(prisma.measurement.createMany).mockResolvedValue({ count: 1 });
|
||||
const res = await POST(makeRequest({ entries: [validStepEntry("ext-a")] }));
|
||||
expect(res.status).toBe(200);
|
||||
expect(enqueuePrDetection).toHaveBeenCalledTimes(1);
|
||||
@@ -109,7 +122,6 @@ describe("POST /api/measurements/batch — PR detection enqueue (v1.4.25 W16c)",
|
||||
const big = Array.from({ length: 51 }, (_, i) =>
|
||||
validStepEntry(`ext-${i}`),
|
||||
);
|
||||
vi.mocked(prisma.measurement.createMany).mockResolvedValue({ count: 51 });
|
||||
const res = await POST(makeRequest({ entries: big }));
|
||||
expect(res.status).toBe(200);
|
||||
expect(enqueuePrDetection).toHaveBeenCalledWith("user-1", {
|
||||
|
||||
@@ -179,39 +179,6 @@ type BatchEntry = z.infer<typeof batchEntrySchema>;
|
||||
*/
|
||||
type EntryStatus = "inserted" | "updated" | "duplicate" | "skipped";
|
||||
|
||||
/**
|
||||
* v1.31.0 — the rows this write actually INSERTED, in the shape the arrival
|
||||
* kind grouper takes.
|
||||
*
|
||||
* Restricting to `status === "inserted"` is what keeps a re-sync silent: an
|
||||
* `updated` row is the `stats:` overwrite contract re-stating a value the
|
||||
* record already had, and a `duplicate` row landed nothing. The per-entry
|
||||
* statuses are reconciled against the insert race upstream, so the count is
|
||||
* accurate; in a rare concurrent-write tie it can be off on WHICH row, which
|
||||
* cannot change the outcome because the day-scoped singleton key collapses the
|
||||
* result either way.
|
||||
*/
|
||||
function insertedArrivalRows(
|
||||
prepared: ReadonlyArray<{
|
||||
index: number;
|
||||
row: Prisma.MeasurementCreateManyInput;
|
||||
}>,
|
||||
results: ReadonlyArray<{ status: EntryStatus } | undefined>,
|
||||
): Array<{ type: MeasurementType; measuredAt: Date }> {
|
||||
const rows: Array<{ type: MeasurementType; measuredAt: Date }> = [];
|
||||
for (const p of prepared) {
|
||||
if (results[p.index]?.status !== "inserted") continue;
|
||||
rows.push({
|
||||
type: p.row.type as MeasurementType,
|
||||
measuredAt:
|
||||
p.row.measuredAt instanceof Date
|
||||
? p.row.measuredAt
|
||||
: new Date(p.row.measuredAt as string),
|
||||
});
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
// v1.5.0 issue #213 — `stats:*` externalIds carry per-day cumulative
|
||||
// totals that the iOS HealthKit observer re-posts as the day progresses.
|
||||
// Treating those as immutable duplicates freezes the day's tile at the
|
||||
@@ -410,12 +377,15 @@ async function postBatch(request: NextRequest): Promise<Response> {
|
||||
// composite-key duplicate). Surfaced as a dedicated wide-event count so
|
||||
// an operator can see how often the standalone-pair mirror collapses.
|
||||
let crossSourceMergedCount = 0;
|
||||
// Exact prepared rows returned by INSERT ... RETURNING. This is the only
|
||||
// source for post-write arrival and sleep side effects: preflight checks can
|
||||
// lose a unique-key race after they run.
|
||||
const insertedPrepared: Prepared[] = [];
|
||||
|
||||
if (prepared.length > 0) {
|
||||
// Pre-flight duplicate detection so we can return per-entry status
|
||||
// (createMany with skipDuplicates does the dedup but doesn't tell
|
||||
// us *which* rows it skipped). We look up existing rows under the
|
||||
// composite unique key first, then createMany the survivors.
|
||||
// Pre-flight duplicate detection classifies rows already present before
|
||||
// this request. The later INSERT ... RETURNING remains authoritative when
|
||||
// a competing request wins the unique-key race after this probe.
|
||||
// v1.8.6 W6 — `source` is now part of the dedup key, so the lookup
|
||||
// matches on (type, source, externalId) per incoming row rather than
|
||||
// a single hardcoded `APPLE_HEALTH`. A `MANUAL` row and an
|
||||
@@ -538,7 +508,8 @@ async function postBatch(request: NextRequest): Promise<Response> {
|
||||
);
|
||||
}
|
||||
|
||||
const toInsert: Prisma.MeasurementCreateManyInput[] = [];
|
||||
const toInsert: Prepared[] = [];
|
||||
const insertKeys = new Set<string>();
|
||||
const toOverwrite: Prepared[] = [];
|
||||
for (const p of prepared) {
|
||||
const key = `${p.row.type}::${p.row.source}::${p.row.externalId}`;
|
||||
@@ -554,7 +525,7 @@ async function postBatch(request: NextRequest): Promise<Response> {
|
||||
duplicateCount += 1;
|
||||
continue;
|
||||
}
|
||||
if (existingSet.has(key)) {
|
||||
if (existingSet.has(key) || insertKeys.has(key)) {
|
||||
// v1.5.0 issue #213 — per-day cumulative `stats:*` rows are
|
||||
// intentionally overwritten on a re-post so today's tile reflects
|
||||
// the latest HealthKit total instead of freezing at the
|
||||
@@ -584,7 +555,8 @@ async function postBatch(request: NextRequest): Promise<Response> {
|
||||
crossSourceMergedCount += 1;
|
||||
} else {
|
||||
results[p.index] = { index: p.index, status: "inserted" };
|
||||
toInsert.push(p.row);
|
||||
toInsert.push(p);
|
||||
insertKeys.add(key);
|
||||
// Track this row so a same-reading sibling later in the SAME
|
||||
// batch collapses against it.
|
||||
if (isMergeableSource(p.row.source as string)) {
|
||||
@@ -643,64 +615,58 @@ async function postBatch(request: NextRequest): Promise<Response> {
|
||||
}
|
||||
|
||||
if (toInsert.length > 0) {
|
||||
// Chunk to keep individual SQL statements bounded — 200 rows per
|
||||
// chunk leaves headroom under Postgres' 65k-parameter cap even
|
||||
// with 9-column inserts. `skipDuplicates: true` guards against
|
||||
// races where two batch retries land in the same tick — the
|
||||
// composite unique index does the actual dedup; skipDuplicates
|
||||
// turns the duplicate-PG error into a no-op insert.
|
||||
// PostgreSQL RETURNING identifies the exact winner when another request
|
||||
// races the same composite unique key. An aggregate insert count can say
|
||||
// how many rows landed but not which prepared rows won, which would make
|
||||
// arrival and morning-refresh side effects attach to arbitrary rows.
|
||||
const CHUNK = 200;
|
||||
const chunks: Prisma.MeasurementCreateManyInput[][] = [];
|
||||
for (let i = 0; i < toInsert.length; i += CHUNK) {
|
||||
chunks.push(toInsert.slice(i, i + CHUNK));
|
||||
}
|
||||
const insertedIndexes = new Set<number>();
|
||||
await prisma.$transaction(async (tx) => {
|
||||
for (const chunk of chunks) {
|
||||
const result = await tx.measurement.createMany({
|
||||
data: chunk,
|
||||
for (let i = 0; i < toInsert.length; i += CHUNK) {
|
||||
const chunk = toInsert.slice(i, i + CHUNK);
|
||||
const pendingByKey = new Map<string, Prepared>();
|
||||
for (const p of chunk) {
|
||||
pendingByKey.set(
|
||||
`${p.row.type}::${p.row.source}::${p.row.externalId}`,
|
||||
p,
|
||||
);
|
||||
}
|
||||
|
||||
const inserted = await tx.measurement.createManyAndReturn({
|
||||
data: chunk.map((p) => p.row),
|
||||
skipDuplicates: true,
|
||||
select: { type: true, source: true, externalId: true },
|
||||
});
|
||||
insertedCount += result.count;
|
||||
// Count only returned identities that map to an attempted row, so
|
||||
// aggregate counts and per-entry statuses share one source of truth.
|
||||
for (const row of inserted) {
|
||||
const preparedRow = pendingByKey.get(
|
||||
`${row.type}::${row.source}::${row.externalId}`,
|
||||
);
|
||||
if (!preparedRow) continue;
|
||||
insertedIndexes.add(preparedRow.index);
|
||||
insertedCount += 1;
|
||||
insertedPrepared.push(preparedRow);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// v1.4.25 W10 reconcile (senior-dev H-1): the previous "stored
|
||||
// vs not-stored" check was an effective no-op. Under standard
|
||||
// Postgres semantics, `skipDuplicates` absorbs duplicate-key
|
||||
// conflicts but the row is STILL present in the table (written
|
||||
// by the other batch that won the race). So every row we
|
||||
// attempted is in the DB after the call, and the
|
||||
// `!stored.has(...)` branch never fired — leaving the aggregate
|
||||
// `inserted` / `duplicate` counts inconsistent with the per-
|
||||
// entry statuses under contention.
|
||||
//
|
||||
// Pragmatic fix: trust the `createMany` return count. The DB
|
||||
// round-trip cannot distinguish a row this request wrote from
|
||||
// a row the racing request wrote (the unique index sees both
|
||||
// as the same key), so we cannot identify the SPECIFIC raced
|
||||
// rows. What we CAN do is preserve count integrity for the
|
||||
// iOS sync cursor: `insertedCount` is already the truth (it
|
||||
// came from `createMany.count`); we only need to downgrade
|
||||
// enough per-entry "inserted" statuses to "duplicate" so the
|
||||
// per-entry envelope sums match. Order doesn't matter — the
|
||||
// client checkpoints past both statuses, and the DB state for
|
||||
// either outcome is identical (the row is now stored,
|
||||
// single-copy).
|
||||
const racedDuplicates = toInsert.length - insertedCount;
|
||||
if (racedDuplicates > 0) {
|
||||
let downgraded = 0;
|
||||
for (const p of prepared) {
|
||||
if (downgraded >= racedDuplicates) break;
|
||||
if (results[p.index]?.status === "inserted") {
|
||||
results[p.index] = { index: p.index, status: "duplicate" };
|
||||
duplicateCount += 1;
|
||||
downgraded += 1;
|
||||
}
|
||||
}
|
||||
for (const p of toInsert) {
|
||||
if (insertedIndexes.has(p.index)) continue;
|
||||
results[p.index] = { index: p.index, status: "duplicate" };
|
||||
duplicateCount += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const freshlyInsertedRows = insertedPrepared.map((p) => ({
|
||||
type: p.row.type as MeasurementType,
|
||||
measuredAt:
|
||||
p.row.measuredAt instanceof Date
|
||||
? p.row.measuredAt
|
||||
: new Date(p.row.measuredAt as string),
|
||||
}));
|
||||
|
||||
const skipped = results
|
||||
.filter((r) => r.status === "skipped")
|
||||
.map((r) => ({ index: r.index, reason: r.reason ?? "unknown" }));
|
||||
@@ -813,13 +779,10 @@ async function postBatch(request: NextRequest): Promise<Response> {
|
||||
invalidateUserMeasurements(user.id, { evict: true });
|
||||
|
||||
// v1.5.0 — refresh the persistent rollup table for every distinct
|
||||
// (type, day) the batch touched. We use the `prepared` rows
|
||||
// because `createMany` does not return the inserted rows; the
|
||||
// (type, measuredAt) tuples on `prepared` are exactly the row
|
||||
// shape we need. Collapsed by day so a 500-row Apple Health
|
||||
// batch fires N recomputes (typically <30, one per type-day
|
||||
// pair) instead of one per row. Best-effort — a populator hiccup
|
||||
// never fails the user's ingest.
|
||||
// (type, day) the batch touched. Updates must participate as well as
|
||||
// inserts, so this deliberately retains the prepared-row scope. Collapsed
|
||||
// by day so a 500-row Apple Health batch fires only one recompute per
|
||||
// type/day pair. Best-effort — a populator hiccup never fails ingest.
|
||||
try {
|
||||
const insertedKeys = collapseToTypeDayKeys(
|
||||
prepared.map((p) => ({
|
||||
@@ -855,13 +818,9 @@ async function postBatch(request: NextRequest): Promise<Response> {
|
||||
// months of nights never re-triggers for an old night. Fire-and-forget.
|
||||
void maybeEnqueueMorningRefresh(
|
||||
user.id,
|
||||
prepared
|
||||
.filter((p) => p.row.type === "SLEEP_DURATION")
|
||||
.map((p) =>
|
||||
p.row.measuredAt instanceof Date
|
||||
? p.row.measuredAt
|
||||
: new Date(p.row.measuredAt as string),
|
||||
),
|
||||
freshlyInsertedRows
|
||||
.filter((row) => row.type === "SLEEP_DURATION")
|
||||
.map((row) => row.measuredAt),
|
||||
).catch(() => {});
|
||||
|
||||
// v1.31.0 — the measurement arm of the data-arrival spine.
|
||||
@@ -881,9 +840,7 @@ async function postBatch(request: NextRequest): Promise<Response> {
|
||||
// `stats:` overwrite contract re-stating a value the record already had,
|
||||
// which is not news. If nothing was inserted the classifier returns `noop`
|
||||
// and nothing is enqueued.
|
||||
for (const [kind, group] of groupRowsByArrivalKind(
|
||||
insertedArrivalRows(prepared, results),
|
||||
)) {
|
||||
for (const [kind, group] of groupRowsByArrivalKind(freshlyInsertedRows)) {
|
||||
void emitDataArrival({
|
||||
userId: user.id,
|
||||
kind,
|
||||
|
||||
Reference in new issue
Block a user