From df4fdc335f0d6282eda60d9c2d2e0480518cb446 Mon Sep 17 00:00:00 2001 From: KM Koushik Date: Tue, 17 Nov 2026 10:24:09 +1100 Subject: [PATCH 1/2] feat: admin stats endpoint for the HQ growth view GET /api/admin/stats?days=N (default 90, capped at 365) behind the admin API token: totals, zero-filled daily counts, and distinct active users per ISO week and month, read in one read-only snapshot with a 5s statement timeout. Adds a nullable User.createdAt defaulting to now(); the migration backfills existing rows with their earliest recorded activity and leaves the rest null. --- .../migration.sql | 47 +++ prisma/schema.prisma | 3 + src/pages/add.tsx | 1 + src/pages/api/admin/stats.ts | 16 + src/server/api/services/adminStats.ts | 359 ++++++++++++++++ src/store/addStore.ts | 1 + src/tests/addFlow.test.ts | 1 + src/tests/adminStats.integration.test.ts | 382 ++++++++++++++++++ src/tests/adminStats.test.ts | 152 +++++++ src/tests/defaultSplit.test.ts | 1 + src/tests/editSnapshot.test.ts | 1 + src/tests/invites.test.ts | 1 + src/tests/quickWins.test.ts | 1 + src/tests/recurrence.test.ts | 1 + src/tests/splitRoundTrip.test.ts | 1 + src/tests/splits.test.ts | 1 + 16 files changed, 969 insertions(+) create mode 100644 prisma/migrations/20261117120000_add_user_created_at/migration.sql create mode 100644 src/pages/api/admin/stats.ts create mode 100644 src/server/api/services/adminStats.ts create mode 100644 src/tests/adminStats.integration.test.ts create mode 100644 src/tests/adminStats.test.ts diff --git a/prisma/migrations/20261117120000_add_user_created_at/migration.sql b/prisma/migrations/20261117120000_add_user_created_at/migration.sql new file mode 100644 index 0000000..b705d9b --- /dev/null +++ b/prisma/migrations/20261117120000_add_user_created_at/migration.sql @@ -0,0 +1,47 @@ +-- Users had no creation time. Add one, nullable: new rows get the time they are created, rows from +-- before this migration get an estimate (their earliest recorded activity) or stay null without any. + +-- AlterTable +ALTER TABLE "User" ADD COLUMN "createdAt" TIMESTAMP(3); + +-- Backfill: the earliest timestamp tied to each user, in one UPDATE. Every source is at or after +-- the user existed, so the estimate errs late, never early: +-- - expense times are when it was added to SplitPro, not its expense date +-- - `emailVerified` is their latest email sign-in +-- - a session's `expires` minus 30 days (the shortest session length, web; app sessions last +-- 180) is at or after it was last renewed; capped at now +-- Accounts (OAuth links) and verification tokens have no timestamps. Each table is read once and +-- grouped by user: most of these user columns have no index, so per-user lookups would scan. +WITH "firstSeen" AS ( + SELECT "userId", MIN("at") AS "at" + FROM ( + SELECT "addedBy" AS "userId", "createdAt" AS "at" FROM "Expense" + UNION ALL SELECT "paidBy", "createdAt" FROM "Expense" + UNION ALL + SELECT ep."userId", e."createdAt" + FROM "ExpenseParticipant" ep + JOIN "Expense" e ON e."id" = ep."expenseId" + UNION ALL SELECT "createdById", "createdAt" FROM "ExpenseNote" + UNION ALL SELECT "userId", "createdAt" FROM "Group" + UNION ALL SELECT "userId", "createdAt" FROM "Friendship" + UNION ALL SELECT "friendId", "createdAt" FROM "Friendship" + UNION ALL SELECT "userId", "createdAt" FROM "Balance" + UNION ALL SELECT "friendId", "createdAt" FROM "Balance" + UNION ALL SELECT "createdById", "createdAt" FROM "RecurringExpense" + UNION ALL SELECT "userId", "createdAt" FROM "DevicePushToken" + UNION ALL SELECT "userId", "createdAt" FROM "Feedback" + UNION ALL + SELECT "userId", LEAST("expires" - INTERVAL '30 days', NOW() AT TIME ZONE 'UTC') + FROM "Session" + UNION ALL SELECT "id", "emailVerified" FROM "User" + ) AS "seen" + WHERE "userId" IS NOT NULL AND "at" IS NOT NULL + GROUP BY "userId" +) +UPDATE "User" u +SET "createdAt" = f."at" +FROM "firstSeen" f +WHERE f."userId" = u."id"; + +-- Only rows created from now on get a default +ALTER TABLE "User" ALTER COLUMN "createdAt" SET DEFAULT CURRENT_TIMESTAMP; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index a1bc809..4384500 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -53,6 +53,9 @@ model User { /// Set when the person deleted their account. The row stays, without their name or email, so /// expenses and balances they share with others stay intact. deletedAt DateTime? + /// When the row was created. Rows from before this column existed hold an estimate (their + /// earliest recorded activity) or null; see migration 20261117120000_add_user_created_at. + createdAt DateTime? @default(now()) accounts Account[] sessions Session[] groups Group[] @relation("GroupCreatedBy") diff --git a/src/pages/add.tsx b/src/pages/add.tsx index 61ec7a3..3325413 100644 --- a/src/pages/add.tsx +++ b/src/pages/add.tsx @@ -53,6 +53,7 @@ const AddPage: NextPageWithUser<{ ...user, emailVerified: null, deletedAt: null, + createdAt: null, name: user.name ?? null, email: user.email ?? null, image: user.image ?? null, diff --git a/src/pages/api/admin/stats.ts b/src/pages/api/admin/stats.ts new file mode 100644 index 0000000..51b9166 --- /dev/null +++ b/src/pages/api/admin/stats.ts @@ -0,0 +1,16 @@ +import { adminApiRoute } from '~/server/adminApi'; +import { adminStatsQuery, getAdminStats } from '~/server/api/services/adminStats'; +import { db } from '~/server/db'; + +// Read by `adminApiRoute`, once the request is authorised +export const config = { api: { bodyParser: false } }; + +/** + * GET /api/admin/stats?days=<1-365, default 90> + * Read-only growth numbers: totals, daily counts for the last `days` UTC days, and distinct active + * users for the last 12 ISO weeks and 6 months. See `adminStats.ts` for what each number means. + */ +export default adminApiRoute(['GET'], async (req, res) => { + const { days } = adminStatsQuery.parse(req.query); + res.status(200).json(await getAdminStats(db, { days })); +}); diff --git a/src/server/api/services/adminStats.ts b/src/server/api/services/adminStats.ts new file mode 100644 index 0000000..6a6ac3a --- /dev/null +++ b/src/server/api/services/adminStats.ts @@ -0,0 +1,359 @@ +import { Prisma, type PrismaClient } from '@prisma/client'; +import { z } from 'zod'; + +/** + * Read-only growth numbers for the internal admin API (GET /api/admin/stats). + * + * Definitions, all in UTC: + * - A *user* is a live account (`deletedAt` null) that has signed in at least once: it has a linked + * OAuth account or a verified email. Rows made when someone was invited or added to an expense, + * who never signed in, are counted apart as `invitedUsers`. Deleted (anonymised) rows are never + * counted as users. + * - *New users* are users whose `User.createdAt` falls on that day (`newUsersMethod`). Rows from + * before that column existed hold an estimate, their earliest recorded activity, so days before + * `newUsersExactSince` are approximate and users with no activity at all are missing from them. + * Someone invited first and signing in later counts on the day they were invited. + * - An *expense event* is an expense that isn't deleted and was added by a person: occurrences the + * recurring runner created on its own (`recurringId` set and not the rule's first date) are + * left out of activity and counted as `recurringCreated` instead. Its time is `Expense.createdAt`. + * - *Active users* (`activeUsers`) are distinct users who added, paid for or took part in an expense + * event in the period, settlements included. `activeCreators` counts only those who added one, + * the stricter signal that they opened the app. Weekly and monthly numbers are distinct users in + * that ISO week or calendar month, not sums of days. + * - There's no record of which app an expense was added from, so there is no per-platform split of + * expenses (`platforms` is null). Push devices by platform are in the totals. + */ + +export const MAX_DAYS = 365; +export const DEFAULT_DAYS = 90; +export const WEEKS = 12; +export const MONTHS = 6; +const STATEMENT_TIMEOUT_MS = 5000; +const DAY_MS = 24 * 60 * 60 * 1000; +const CREATED_AT_MIGRATION = '20261117120000_add_user_created_at'; + +export const adminStatsQuery = z.object({ + days: z.coerce + .number() + .int() + .min(1) + .default(DEFAULT_DAYS) + .transform((days) => Math.min(days, MAX_DAYS)), +}); + +type Db = Pick; +type Tx = Prisma.TransactionClient; + +const startOfUtcDay = (date: Date) => + new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate())); + +const addDays = (date: Date, days: number) => new Date(date.getTime() + days * DAY_MS); + +/** `YYYY-MM-DD` of a UTC date. */ +export const dayKey = (date: Date) => date.toISOString().slice(0, 10); + +/** `YYYY-MM` of a UTC date. */ +export const monthKey = (date: Date) => date.toISOString().slice(0, 7); + +/** ISO week label, like `2026-W47`. */ +export const isoWeekKey = (date: Date) => { + const day = startOfUtcDay(date); + // The Thursday of its week decides the ISO year + const thursday = addDays(day, 3 - ((day.getUTCDay() + 6) % 7)); + const yearStart = new Date(Date.UTC(thursday.getUTCFullYear(), 0, 1)); + const week = 1 + Math.floor((thursday.getTime() - yearStart.getTime()) / (7 * DAY_MS)); + return `${thursday.getUTCFullYear()}-W${String(week).padStart(2, '0')}`; +}; + +/** + * The periods the stats cover, ending today (UTC). Each list is oldest first. + */ +export const statsPeriods = (now: Date, days: number) => { + const today = startOfUtcDay(now); + const monday = addDays(today, -((today.getUTCDay() + 6) % 7)); + + const dayStarts = Array.from({ length: days }, (_, i) => addDays(today, i - days + 1)); + const weekStarts = Array.from({ length: WEEKS }, (_, i) => addDays(monday, (i - WEEKS + 1) * 7)); + const monthStarts = Array.from( + { length: MONTHS }, + (_, i) => new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth() + i - MONTHS + 1, 1)), + ); + + const firstDay = dayStarts[0]!; + const since = [firstDay, weekStarts[0]!, monthStarts[0]!].reduce((a, b) => (a < b ? a : b)); + + return { dayStarts, weekStarts, monthStarts, firstDay, since, end: addDays(today, 1) }; +}; + +/** + * The counts for each key, in order: the matching row's `fields` as numbers, 0 where it has none. + */ +export const zeroFill = ( + keys: string[], + rows: ({ key: string } & Record)[], + fields: readonly K[], +) => { + const byKey = new Map(rows.map((row) => [row.key, row])); + return keys.map( + (key) => + Object.fromEntries( + fields.map((field) => [field, Number(byKey.get(key)?.[field] ?? 0)]), + ) as Record, + ); +}; + +/** Timestamps are stored as UTC `timestamp(3)`, so compare against UTC wall time. */ +const ts = (date: Date) => Prisma.sql`${date.toISOString().slice(0, 23)}::timestamp`; + +/** + * Users who have signed in at least once: `u` is the "User" row, joined with `SIGNED_IN_JOIN`. A join + * on the few users with an OAuth link, so it is hashed once rather than looked up per row. + */ +const SIGNED_IN_JOIN = Prisma.sql`LEFT JOIN (SELECT DISTINCT "userId" FROM "Account") acc + ON acc."userId" = u."id"`; +const SIGNED_IN = Prisma.sql`(u."emailVerified" IS NOT NULL OR acc."userId" IS NOT NULL)`; + +/** Expenses the recurring runner created on its own: `e` is the "Expense", `r` its rule. */ +const AUTO_RECURRING = Prisma.sql`(e."recurringId" IS NOT NULL + AND e."occurrenceDate" IS DISTINCT FROM r."startDate")`; + +type CountRow = Record; +type KeyedRow = CountRow & { key: string }; + +const getTotals = async (tx: Tx) => { + const [row] = await tx.$queryRaw` + SELECT + users.*, + expenses.*, + (SELECT COUNT(*) FROM "Group") AS "groups", + (SELECT COUNT(*) FROM "RecurringExpense" WHERE "status" = 'ACTIVE') AS "activeRecurringRules", + (SELECT COUNT(*) FROM "PushNotification" p + JOIN "User" u ON u."id" = p."userId" WHERE u."deletedAt" IS NULL) AS "webPushUsers" + FROM ( + SELECT + COUNT(*) FILTER (WHERE u."deletedAt" IS NULL AND ${SIGNED_IN}) AS "users", + COUNT(*) FILTER (WHERE u."deletedAt" IS NULL AND NOT ${SIGNED_IN}) AS "invitedUsers", + COUNT(*) FILTER (WHERE u."deletedAt" IS NOT NULL) AS "deletedUsers" + FROM "User" u + ${SIGNED_IN_JOIN} + ) AS users, ( + SELECT + COUNT(*) FILTER (WHERE "splitType" <> 'SETTLEMENT') AS "expenses", + COUNT(*) FILTER (WHERE "splitType" = 'SETTLEMENT') AS "settlements" + FROM "Expense" + WHERE "deletedAt" IS NULL + ) AS expenses + `; + + const devices = await tx.$queryRaw<{ platform: string; devices: bigint; users: bigint }[]>` + SELECT d."platform", COUNT(*) AS "devices", COUNT(DISTINCT d."userId") AS "users" + FROM "DevicePushToken" d + JOIN "User" u ON u."id" = d."userId" + WHERE u."deletedAt" IS NULL + GROUP BY d."platform" + ORDER BY d."platform" + `; + + const n = (key: string) => Number(row?.[key] ?? 0); + return { + users: n('users'), + invitedUsers: n('invitedUsers'), + deletedUsers: n('deletedUsers'), + groups: n('groups'), + expenses: n('expenses'), + settlements: n('settlements'), + activeRecurringRules: n('activeRecurringRules'), + pushDevices: { + /** Native app installs with push on, by platform (`ios`, `android`) */ + native: Object.fromEntries( + devices.map(({ platform, devices, users }) => [ + platform, + { devices: Number(devices), users: Number(users) }, + ]), + ), + /** Users with browser push on: one subscription per user */ + webPushUsers: n('webPushUsers'), + }, + }; +}; + +const getDailyCounts = async (tx: Tx, firstDay: Date, end: Date) => { + const expenses = await tx.$queryRaw` + SELECT + to_char(date_trunc('day', e."createdAt"), 'YYYY-MM-DD') AS "key", + COUNT(*) FILTER (WHERE e."splitType" <> 'SETTLEMENT' AND NOT ${AUTO_RECURRING}) + AS "expensesCreated", + COUNT(*) FILTER (WHERE e."splitType" = 'SETTLEMENT' AND NOT ${AUTO_RECURRING}) + AS "settlements", + COUNT(*) FILTER (WHERE ${AUTO_RECURRING}) AS "recurringCreated" + FROM "Expense" e + LEFT JOIN "RecurringExpense" r ON r."id" = e."recurringId" + WHERE e."deletedAt" IS NULL AND e."createdAt" >= ${ts(firstDay)} AND e."createdAt" < ${ts(end)} + GROUP BY 1 + `; + + const groups = await tx.$queryRaw` + SELECT to_char(date_trunc('day', "createdAt"), 'YYYY-MM-DD') AS "key", COUNT(*) AS "groupsCreated" + FROM "Group" + WHERE "createdAt" >= ${ts(firstDay)} AND "createdAt" < ${ts(end)} + GROUP BY 1 + `; + + const users = await tx.$queryRaw` + SELECT + to_char(date_trunc('day', u."createdAt"), 'YYYY-MM-DD') AS "key", + COUNT(*) FILTER (WHERE ${SIGNED_IN}) AS "newUsers", + COUNT(*) FILTER (WHERE NOT ${SIGNED_IN}) AS "newInvitedUsers" + FROM "User" u + ${SIGNED_IN_JOIN} + WHERE u."deletedAt" IS NULL AND u."createdAt" >= ${ts(firstDay)} AND u."createdAt" < ${ts(end)} + GROUP BY 1 + `; + + return { expenses, groups, users }; +}; + +/** + * Distinct active users per day, ISO week and month, from one scan of the expense events since + * `since`. + */ +const getActiveUsers = async ( + tx: Tx, + { + since, + firstDay, + firstWeek, + firstMonth, + end, + }: { since: Date; firstDay: Date; firstWeek: Date; firstMonth: Date; end: Date }, +) => + tx.$queryRaw<(KeyedRow & { unit: 'day' | 'week' | 'month' })[]>` + WITH "events" AS ( + SELECT e."id", e."createdAt", e."addedBy", e."paidBy" + FROM "Expense" e + LEFT JOIN "RecurringExpense" r ON r."id" = e."recurringId" + WHERE e."deletedAt" IS NULL + AND e."createdAt" >= ${ts(since)} AND e."createdAt" < ${ts(end)} + AND NOT ${AUTO_RECURRING} + ), + "involved" AS ( + SELECT "createdAt" AS "at", "addedBy" AS "userId", TRUE AS "creator" FROM "events" + UNION ALL SELECT "createdAt", "paidBy", FALSE FROM "events" + UNION ALL + SELECT ev."createdAt", ep."userId", FALSE + FROM "events" ev + JOIN "ExpenseParticipant" ep ON ep."expenseId" = ev."id" + ), + "active" AS MATERIALIZED ( + SELECT i."at", i."userId", i."creator" + FROM "involved" i + JOIN "User" u ON u."id" = i."userId" + ${SIGNED_IN_JOIN} + WHERE u."deletedAt" IS NULL AND ${SIGNED_IN} + ) + SELECT 'day' AS "unit", to_char(date_trunc('day', "at"), 'YYYY-MM-DD') AS "key", + COUNT(DISTINCT "userId") AS "activeUsers", + COUNT(DISTINCT "userId") FILTER (WHERE "creator") AS "activeCreators" + FROM "active" WHERE "at" >= ${ts(firstDay)} GROUP BY 2 + UNION ALL + SELECT 'week', to_char(date_trunc('week', "at"), 'YYYY-MM-DD'), + COUNT(DISTINCT "userId"), COUNT(DISTINCT "userId") FILTER (WHERE "creator") + FROM "active" WHERE "at" >= ${ts(firstWeek)} GROUP BY 2 + UNION ALL + SELECT 'month', to_char(date_trunc('month', "at"), 'YYYY-MM'), + COUNT(DISTINCT "userId"), COUNT(DISTINCT "userId") FILTER (WHERE "creator") + FROM "active" WHERE "at" >= ${ts(firstMonth)} GROUP BY 2 + `; + +/** When `User.createdAt` started being set on create, or null if unknown (no migration table). */ +const getCreatedAtExactSince = async (tx: Tx) => { + const [table] = await tx.$queryRaw<{ exists: boolean }[]>` + SELECT to_regclass('"_prisma_migrations"') IS NOT NULL AS "exists" + `; + if (!table?.exists) { + return null; + } + const [row] = await tx.$queryRaw<{ finishedAt: Date | null }[]>` + SELECT "finished_at" AS "finishedAt" FROM "_prisma_migrations" + WHERE "migration_name" = ${CREATED_AT_MIGRATION} AND "rolled_back_at" IS NULL + ORDER BY "finished_at" DESC NULLS LAST LIMIT 1 + `; + return row?.finishedAt ?? null; +}; + +const ACTIVE_FIELDS = ['activeUsers', 'activeCreators'] as const; + +/** How `newUsers` is counted, with the date it became exact. */ +export const newUsersMethod = (exactSince: Date | null) => + `createdAt (estimated before ${exactSince ? dayKey(exactSince) : CREATED_AT_MIGRATION})`; + +export const getAdminStats = async ( + db: Db, + { days, now = new Date() }: { days: number; now?: Date }, +) => { + const periods = statsPeriods(now, Math.min(Math.max(1, Math.trunc(days)), MAX_DAYS)); + const { firstDay, since, end } = periods; + + // One read-only snapshot, so the numbers agree with each other. Each statement is capped, and + // nothing waits long behind a lock. + const data = await db.$transaction( + async (tx) => { + await tx.$executeRaw`SET TRANSACTION READ ONLY`; + await tx.$executeRawUnsafe(`SET LOCAL statement_timeout = ${STATEMENT_TIMEOUT_MS}`); + await tx.$executeRawUnsafe(`SET LOCAL lock_timeout = 1000`); + + const totals = await getTotals(tx); + const daily = await getDailyCounts(tx, firstDay, end); + const active = await getActiveUsers(tx, { + since, + firstDay, + firstWeek: periods.weekStarts[0]!, + firstMonth: periods.monthStarts[0]!, + end, + }); + const exactSince = await getCreatedAtExactSince(tx); + return { totals, daily, active, exactSince }; + }, + { isolationLevel: Prisma.TransactionIsolationLevel.RepeatableRead, timeout: 20_000 }, + ); + + const activeOf = (unit: string) => data.active.filter((row) => unit === row.unit); + const dayKeys = periods.dayStarts.map(dayKey); + + const newUsers = zeroFill(dayKeys, data.daily.users, ['newUsers', 'newInvitedUsers']); + const expenses = zeroFill(dayKeys, data.daily.expenses, [ + 'expensesCreated', + 'settlements', + 'recurringCreated', + ]); + const groups = zeroFill(dayKeys, data.daily.groups, ['groupsCreated']); + const activeDays = zeroFill(dayKeys, activeOf('day'), ACTIVE_FIELDS); + const activeWeeks = zeroFill(periods.weekStarts.map(dayKey), activeOf('week'), ACTIVE_FIELDS); + const monthKeys = periods.monthStarts.map(monthKey); + const activeMonths = zeroFill(monthKeys, activeOf('month'), ACTIVE_FIELDS); + + return { + generatedAt: now.toISOString(), + days: dayKeys.length, + newUsersMethod: newUsersMethod(data.exactSince), + /** Days before this are estimates from earliest activity; null if unknown */ + newUsersExactSince: data.exactSince?.toISOString() ?? null, + totals: data.totals, + daily: dayKeys.map((date, i) => ({ + date, + ...newUsers[i]!, + ...activeDays[i]!, + ...expenses[i]!, + ...groups[i]!, + })), + weekly: periods.weekStarts.map((start, i) => ({ + week: isoWeekKey(start), + start: dayKey(start), + ...activeWeeks[i]!, + })), + monthly: monthKeys.map((month, i) => ({ month, ...activeMonths[i]! })), + platforms: null, + }; +}; + +export type AdminStats = Awaited>; diff --git a/src/store/addStore.ts b/src/store/addStore.ts index fcc95be..c719495 100644 --- a/src/store/addStore.ts +++ b/src/store/addStore.ts @@ -1008,6 +1008,7 @@ export const inviteParticipant = async ( image: null, currency: 'USD', deletedAt: null, + createdAt: null, }); try { diff --git a/src/tests/addFlow.test.ts b/src/tests/addFlow.test.ts index c365ce1..a3ee285 100644 --- a/src/tests/addFlow.test.ts +++ b/src/tests/addFlow.test.ts @@ -29,6 +29,7 @@ const createUser = (id: number): User => ({ email: `user-${id}@example.com`, emailVerified: null, deletedAt: null, + createdAt: null, image: null, currency: 'USD', }); diff --git a/src/tests/adminStats.integration.test.ts b/src/tests/adminStats.integration.test.ts new file mode 100644 index 0000000..3832bf9 --- /dev/null +++ b/src/tests/adminStats.integration.test.ts @@ -0,0 +1,382 @@ +import assert from 'node:assert/strict'; +import { execFileSync } from 'node:child_process'; +import { readFileSync } from 'node:fs'; +import { after, before, test } from 'node:test'; +import { type PrismaClient } from '@prisma/client'; + +import type * as AdminStats from '~/server/api/services/adminStats'; + +const databaseUrl = process.env.TEST_DATABASE_URL; + +/** + * Totals cover the whole database, so these tests get a schema of their own, migrated from + * scratch, and drop it at the end. + */ +const schema = `admin_stats_${Date.now()}_${Math.round(Math.random() * 1e6)}`; +const schemaUrl = () => { + const url = new URL(databaseUrl!); + url.searchParams.set('schema', schema); + return url.toString(); +}; + +let db: PrismaClient; +let stats: typeof AdminStats; + +before(async () => { + if (!databaseUrl) { + return; + } + assert.match(new URL(databaseUrl).pathname, /test/i, 'Refusing to use a non-test database'); + process.env.DATABASE_URL = schemaUrl(); + process.env.SKIP_ENV_VALIDATION = '1'; + + execFileSync('pnpm', ['exec', 'prisma', 'migrate', 'deploy'], { + env: { ...process.env, DATABASE_URL: schemaUrl() }, + stdio: 'ignore', + }); + + [{ db }, stats] = await Promise.all([ + import('~/server/db'), + import('~/server/api/services/adminStats'), + ]); +}); + +after(async () => { + if (!databaseUrl) { + return; + } + await db.$executeRawUnsafe(`DROP SCHEMA IF EXISTS "${schema}" CASCADE`); + await db.$disconnect(); +}); + +const clearAll = () => + db.$executeRawUnsafe( + `TRUNCATE "User", "Account", "Session", "Group", "Expense", "RecurringExpense", + "PushNotification" RESTART IDENTITY CASCADE`, + ); + +// A Wednesday +const NOW = new Date('2026-11-18T12:00:00Z'); +const at = (iso: string) => new Date(`${iso}Z`); + +void test('admin stats: an empty database', { skip: !databaseUrl }, async () => { + await clearAll(); + const result = await stats.getAdminStats(db, { days: 7, now: NOW }); + + assert.equal(result.totals.users, 0); + assert.equal(result.totals.expenses, 0); + assert.deepEqual(result.totals.pushDevices, { native: {}, webPushUsers: 0 }); + assert.equal(result.daily.length, 7); + assert.equal(result.daily[0]!.date, '2026-11-12'); + assert.equal(result.daily.at(-1)!.date, '2026-11-18'); + assert.ok( + result.daily.every(({ date: _date, ...counts }) => + Object.values(counts).every((count) => 0 === count), + ), + ); + assert.equal(result.weekly.length, 12); + assert.equal(result.weekly.at(-1)!.week, '2026-W47'); + assert.deepEqual( + result.monthly.map(({ month }) => month), + ['2026-06', '2026-07', '2026-08', '2026-09', '2026-10', '2026-11'], + ); + assert.ok(result.weekly.every(({ activeUsers }) => 0 === activeUsers)); + assert.equal(result.platforms, null); + assert.match(result.newUsersMethod, /^createdAt \(estimated before \d{4}-\d{2}-\d{2}\)$/); +}); + +void test( + 'admin stats: counts, deleted users, dedup and zero-filling', + { skip: !databaseUrl }, + async () => { + await clearAll(); + + const user = (id: number, data: Record) => + db.user.create({ data: { id, email: `u${id}@example.com`, name: `U${id}`, ...data } }); + // Signed in by email, by OAuth, never signed in (invited), deleted, and one from before + // createdAt existed that the backfill had no estimate for + const alice = await user(1, { emailVerified: NOW, createdAt: at('2026-11-16T10:00:00') }); + const bob = await user(2, { createdAt: at('2026-11-16T11:00:00') }); + await db.account.create({ + data: { userId: bob.id, type: 'oauth', provider: 'google', providerAccountId: 'bob' }, + }); + const ivy = await user(3, { createdAt: at('2026-11-17T09:00:00') }); + const dan = await user(4, { + email: null, + name: 'Deleted user', + deletedAt: NOW, + createdAt: at('2026-11-17T10:00:00'), + }); + const old = await user(5, { emailVerified: at('2025-01-01T00:00:00'), createdAt: null }); + + const expense = ( + id: string, + createdAt: Date, + addedBy: number, + paidBy: number, + participants: number[], + extra: Record = {}, + ) => + db.expense.create({ + data: { + id, + addedBy, + paidBy, + name: id, + category: 'general', + amount: 100, + currency: 'USD', + createdAt, + expenseDate: createdAt, + expenseParticipants: { + create: participants.map((userId) => ({ userId, amount: 0 })), + }, + ...extra, + }, + }); + + await expense('e1', at('2026-11-16T09:00:00'), alice.id, alice.id, [alice.id, bob.id, ivy.id]); + await expense('settle', at('2026-11-17T09:00:00'), bob.id, bob.id, [bob.id, alice.id], { + splitType: 'SETTLEMENT', + }); + await expense('gone', at('2026-11-17T10:00:00'), old.id, old.id, [old.id], { deletedAt: NOW }); + await expense('e2', at('2026-11-18T08:00:00'), alice.id, bob.id, [alice.id, bob.id, dan.id]); + await expense('october', at('2026-10-05T08:00:00'), bob.id, bob.id, [bob.id]); + + // An occurrence the recurring runner added on its own isn't activity + const rule = await db.recurringExpense.create({ + data: { + frequency: 'DAILY', + timezone: 'UTC', + startDate: at('2026-11-01T00:00:00'), + nextOccurrenceDate: at('2026-11-19T00:00:00'), + nextOccurrenceAt: at('2026-11-19T00:00:00'), + createdById: old.id, + }, + }); + await expense('auto', at('2026-11-18T00:05:00'), old.id, old.id, [old.id], { + recurringId: rule.id, + occurrenceDate: at('2026-11-18T00:00:00'), + }); + + await db.group.create({ + data: { + publicId: 'g1', + name: 'Trip', + userId: alice.id, + createdAt: at('2026-11-17T12:00:00'), + }, + }); + await db.devicePushToken.createMany({ + data: [ + { userId: alice.id, token: 't1', platform: 'ios' }, + { userId: bob.id, token: 't2', platform: 'android' }, + { userId: dan.id, token: 't3', platform: 'ios' }, + ], + }); + await db.pushNotification.create({ data: { userId: alice.id, subscription: '{}' } }); + + const result = await stats.getAdminStats(db, { days: 3, now: NOW }); + + assert.deepEqual(result.totals, { + users: 3, + invitedUsers: 1, + deletedUsers: 1, + groups: 1, + expenses: 4, + settlements: 1, + activeRecurringRules: 1, + pushDevices: { + native: { android: { devices: 1, users: 1 }, ios: { devices: 1, users: 1 } }, + webPushUsers: 1, + }, + }); + + const zeros = { settlements: 0, recurringCreated: 0, groupsCreated: 0, newInvitedUsers: 0 }; + assert.deepEqual(result.daily, [ + // Ivy took part but never signed in, so isn't an active user + { + ...zeros, + date: '2026-11-16', + newUsers: 2, + activeUsers: 2, + activeCreators: 1, + expensesCreated: 1, + }, + // The deleted user doesn't count as new, the deleted expense isn't counted + { + ...zeros, + date: '2026-11-17', + newUsers: 0, + newInvitedUsers: 1, + activeUsers: 2, + activeCreators: 1, + expensesCreated: 0, + settlements: 1, + groupsCreated: 1, + }, + // Dan is deleted, and the recurring occurrence doesn't make its creator active + { + ...zeros, + date: '2026-11-18', + newUsers: 0, + activeUsers: 2, + activeCreators: 1, + expensesCreated: 1, + recurringCreated: 1, + }, + ]); + + // Six active user-days in the week, but two people + const thisWeek = result.weekly.at(-1)!; + assert.deepEqual(thisWeek, { + week: '2026-W47', + start: '2026-11-16', + activeUsers: 2, + activeCreators: 2, + }); + assert.equal(result.weekly.find(({ week }) => '2026-W41' === week)?.activeUsers, 1); + assert.equal( + result.weekly.reduce((sum, { activeUsers }) => sum + activeUsers, 0), + 3, + ); + assert.deepEqual(result.monthly.slice(-2), [ + { month: '2026-10', activeUsers: 1, activeCreators: 1 }, + { month: '2026-11', activeUsers: 2, activeCreators: 2 }, + ]); + }, +); + +void test( + 'admin stats: every way a user is created sets createdAt', + { skip: !databaseUrl }, + async () => { + await clearAll(); + const { authAdapter } = await import('~/server/auth'); + const startedAt = Date.now() - 60_000; + + // Invites and adding someone to an expense by email + const invited = await db.user.create({ + data: { email: 'invited@example.com', name: 'invited' }, + }); + // The Splitwise import + await db.user.createMany({ data: [{ email: 'imported@example.com', name: 'Imported' }] }); + // NextAuth sign-ups on the web, and the app's email, Google and Apple sign-ins + // next-auth types the adapter as `any` here, see mobileAuth.ts + const createUser = authAdapter.createUser as unknown as (user: { + email: string; + emailVerified: Date; + name: null; + image: null; + }) => Promise<{ createdAt: Date | null }>; + const signedUp = await createUser({ + email: 'signedup@example.com', + emailVerified: new Date(), + name: null, + image: null, + }); + + const rows = await db.user.findMany({ + where: { + email: { in: ['invited@example.com', 'imported@example.com', 'signedup@example.com'] }, + }, + }); + assert.equal(rows.length, 3); + rows.forEach(({ email, createdAt }) => { + assert.ok(createdAt, email ?? ''); + assert.ok(createdAt.getTime() >= startedAt, email ?? ''); + assert.ok(createdAt.getTime() <= Date.now() + 60_000, email ?? ''); + }); + assert.ok(invited.createdAt); + assert.ok(signedUp.createdAt); + }, +); + +/** The backfill statement of the createdAt migration, run again on rows without one. */ +const backfillSql = () => { + const sql = readFileSync( + 'prisma/migrations/20261117120000_add_user_created_at/migration.sql', + 'utf8', + ) + .split('\n') + .filter((line) => !line.trim().startsWith('--')) + .join('\n'); + const statement = sql.split(';').find((part) => part.trim().startsWith('WITH')); + assert.ok(statement, 'backfill statement'); + return statement; +}; + +void test( + 'admin stats: the createdAt backfill estimates from earliest activity', + { skip: !databaseUrl }, + async () => { + await clearAll(); + const make = (id: number, data: Record = {}) => + db.user.create({ data: { id, email: `b${id}@example.com`, ...data } }); + + const payer = await make(1, { emailVerified: at('2026-09-01T00:00:00') }); + const participant = await make(2); + const sessionOnly = await make(3); + // Nothing tied to them + await make(4); + const friendOnly = await make(5); + const renewed = await make(6); + + await db.expense.create({ + data: { + id: 'x1', + addedBy: payer.id, + paidBy: payer.id, + name: 'x1', + category: 'general', + amount: 100, + currency: 'USD', + createdAt: at('2026-03-01T10:00:00'), + // Before the user existed: the expense date must not be used + expenseDate: at('2020-01-01T00:00:00'), + expenseParticipants: { + create: [ + { userId: payer.id, amount: 0 }, + { userId: participant.id, amount: 0 }, + ], + }, + }, + }); + await db.friendship.create({ + data: { userId: payer.id, friendId: friendOnly.id, createdAt: at('2026-02-01T00:00:00') }, + }); + await db.session.create({ + data: { sessionToken: 's1', userId: sessionOnly.id, expires: at('2026-07-31T00:00:00') }, + }); + // A session renewed today, which expires after the backfill runs: capped at now + await db.session.create({ + data: { + sessionToken: 's2', + userId: renewed.id, + expires: new Date(Date.now() + 180 * 86400e3), + }, + }); + + await db.$executeRawUnsafe(`UPDATE "User" SET "createdAt" = NULL`); + await db.$executeRawUnsafe(backfillSql()); + + const rows = await db.user.findMany({ select: { id: true, createdAt: true } }); + const renewedAt = rows.find(({ id }) => renewed.id === id)?.createdAt?.getTime() ?? 0; + assert.ok(Math.abs(Date.now() - renewedAt) < 60_000, 'capped at now'); + + const createdAt = Object.fromEntries( + rows + .filter(({ id }) => renewed.id !== id) + .map((row) => [row.id, row.createdAt?.toISOString() ?? null]), + ); + assert.deepEqual(createdAt, { + // Friendship (Feb) is earlier than the expense and emailVerified + 1: '2026-02-01T00:00:00.000Z', + 2: '2026-03-01T10:00:00.000Z', + 3: '2026-07-01T00:00:00.000Z', + // Nothing tied to them: left null + 4: null, + 5: '2026-02-01T00:00:00.000Z', + }); + }, +); diff --git a/src/tests/adminStats.test.ts b/src/tests/adminStats.test.ts new file mode 100644 index 0000000..bae305f --- /dev/null +++ b/src/tests/adminStats.test.ts @@ -0,0 +1,152 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; + +process.env.SKIP_ENV_VALIDATION = '1'; +process.env.DATABASE_URL ??= 'postgresql://unused:unused@localhost:1/unused'; +const TOKEN = 'b'.repeat(40); +process.env.ADMIN_API_TOKEN = TOKEN; + +const { adminStatsQuery, isoWeekKey, newUsersMethod, statsPeriods, zeroFill } = await import( + '~/server/api/services/adminStats' +); +const { default: statsRoute } = await import('~/pages/api/admin/stats'); +const { env } = await import('~/env'); + +void describe('adminStatsQuery', () => { + void it('defaults to 90 days and caps at 365', () => { + assert.equal(adminStatsQuery.parse({}).days, 90); + assert.equal(adminStatsQuery.parse({ days: '30' }).days, 30); + assert.equal(adminStatsQuery.parse({ days: '9999' }).days, 365); + }); + + void it('rejects days that are not a positive whole number', () => { + ['0', '-1', '1.5', 'abc'].forEach((days) => { + assert.equal(adminStatsQuery.safeParse({ days }).success, false, days); + }); + }); +}); + +void describe('statsPeriods', () => { + // A Wednesday, late in the day + const now = new Date('2026-11-18T23:30:00Z'); + + void it('covers the last `days` UTC days, ending today', () => { + const { dayStarts, end } = statsPeriods(now, 3); + assert.deepEqual( + dayStarts.map((day) => day.toISOString()), + ['2026-11-16T00:00:00.000Z', '2026-11-17T00:00:00.000Z', '2026-11-18T00:00:00.000Z'], + ); + assert.equal(end.toISOString(), '2026-11-19T00:00:00.000Z'); + }); + + void it('covers 12 ISO weeks from Monday and 6 calendar months', () => { + const { weekStarts, monthStarts, since } = statsPeriods(now, 1); + assert.equal(weekStarts.length, 12); + assert.equal(weekStarts.at(-1)!.toISOString(), '2026-11-16T00:00:00.000Z'); + assert.equal(weekStarts[0]!.toISOString(), '2026-08-31T00:00:00.000Z'); + assert.deepEqual( + monthStarts.map((month) => month.toISOString().slice(0, 7)), + ['2026-06', '2026-07', '2026-08', '2026-09', '2026-10', '2026-11'], + ); + // Data is read from the earliest of the three + assert.equal(since.toISOString(), '2026-06-01T00:00:00.000Z'); + }); + + void it('crosses year ends', () => { + const { monthStarts } = statsPeriods(new Date('2027-02-10T00:00:00Z'), 1); + assert.equal(monthStarts[0]!.toISOString().slice(0, 7), '2026-09'); + }); +}); + +void describe('isoWeekKey', () => { + void it('uses the ISO year of the week', () => { + assert.equal(isoWeekKey(new Date('2026-11-16T00:00:00Z')), '2026-W47'); + // Thursday 1 January 2026 starts week 1 + assert.equal(isoWeekKey(new Date('2025-12-29T00:00:00Z')), '2026-W01'); + // Friday 1 January 2027 is still in 2026's last week + assert.equal(isoWeekKey(new Date('2027-01-01T00:00:00Z')), '2026-W53'); + }); +}); + +void describe('zeroFill', () => { + void it('keeps every key, in order, with zeros where there is no row', () => { + const filled = zeroFill( + ['2026-11-16', '2026-11-17', '2026-11-18'], + [ + { key: '2026-11-17', newUsers: 3n, extra: 'ignored' }, + { key: '2026-11-01', newUsers: 9n }, + ], + ['newUsers', 'groupsCreated'] as const, + ); + assert.deepEqual(filled, [ + { newUsers: 0, groupsCreated: 0 }, + { newUsers: 3, groupsCreated: 0 }, + { newUsers: 0, groupsCreated: 0 }, + ]); + }); +}); + +void describe('newUsersMethod', () => { + void it('says from when createdAt is exact', () => { + assert.equal( + newUsersMethod(new Date('2026-11-20T08:00:00Z')), + 'createdAt (estimated before 2026-11-20)', + ); + assert.match(newUsersMethod(null), /^createdAt \(estimated before 20261117120000_/); + }); +}); + +void describe('GET /api/admin/stats', () => { + const call = async (authorization?: string, method = 'GET') => { + const sent = { status: 0, body: undefined as unknown }; + const res = { + setHeader: () => res, + status: (status: number) => { + sent.status = status; + return res; + }, + json: (value: unknown) => { + sent.body = value; + return res; + }, + }; + const req = { method, query: {}, headers: { authorization } }; + await statsRoute(req as never, res as never); + return sent; + }; + + void it('needs the admin token', async () => { + assert.equal((await call()).status, 401); + assert.equal((await call('Bearer wrong')).status, 401); + assert.equal((await call(`Bearer ${TOKEN}`, 'POST')).status, 405); + }); + + void it('is not found when no admin token is set', async () => { + const mutableEnv = env as { ADMIN_API_TOKEN?: string }; + mutableEnv.ADMIN_API_TOKEN = undefined; + try { + assert.equal((await call(`Bearer ${TOKEN}`)).status, 404); + } finally { + mutableEnv.ADMIN_API_TOKEN = TOKEN; + } + }); + + void it('rejects bad query params before touching the db', async () => { + const sent = { status: 0 }; + const res = { + setHeader: () => res, + status: (status: number) => { + sent.status = status; + return res; + }, + json: () => res, + }; + const req = { + method: 'GET', + query: { days: 'lots' }, + headers: { authorization: `Bearer ${TOKEN}` }, + }; + await statsRoute(req as never, res as never); + assert.equal(sent.status, 400); + }); +}); diff --git a/src/tests/defaultSplit.test.ts b/src/tests/defaultSplit.test.ts index 05e21d2..07c0f35 100644 --- a/src/tests/defaultSplit.test.ts +++ b/src/tests/defaultSplit.test.ts @@ -33,6 +33,7 @@ const user = (id: number, name: string): User => ({ email: `${name.toLowerCase()}@example.com`, emailVerified: null, deletedAt: null, + createdAt: null, image: null, currency: 'INR', }); diff --git a/src/tests/editSnapshot.test.ts b/src/tests/editSnapshot.test.ts index f47a3c3..611d526 100644 --- a/src/tests/editSnapshot.test.ts +++ b/src/tests/editSnapshot.test.ts @@ -20,6 +20,7 @@ const createUser = (id: number): User => ({ email: `user-${id}@example.com`, emailVerified: null, deletedAt: null, + createdAt: null, image: null, currency: 'USD', }); diff --git a/src/tests/invites.test.ts b/src/tests/invites.test.ts index a936794..c64c02d 100644 --- a/src/tests/invites.test.ts +++ b/src/tests/invites.test.ts @@ -11,6 +11,7 @@ const createUser = (id: number, name: string): User => ({ email: `${name.toLowerCase()}@example.com`, emailVerified: null, deletedAt: null, + createdAt: null, image: null, currency: 'USD', }); diff --git a/src/tests/quickWins.test.ts b/src/tests/quickWins.test.ts index a7f51e6..058fdf1 100644 --- a/src/tests/quickWins.test.ts +++ b/src/tests/quickWins.test.ts @@ -21,6 +21,7 @@ const me: User = { email: 'me@example.com', emailVerified: null, deletedAt: null, + createdAt: null, image: null, currency: 'INR', }; diff --git a/src/tests/recurrence.test.ts b/src/tests/recurrence.test.ts index 29de9cb..4b26aaf 100644 --- a/src/tests/recurrence.test.ts +++ b/src/tests/recurrence.test.ts @@ -239,6 +239,7 @@ void describe('repeat in the add form', () => { email: 'me@example.com', emailVerified: null, deletedAt: null, + createdAt: null, image: null, currency: 'USD', }; diff --git a/src/tests/splitRoundTrip.test.ts b/src/tests/splitRoundTrip.test.ts index 7e056d1..8c907a0 100644 --- a/src/tests/splitRoundTrip.test.ts +++ b/src/tests/splitRoundTrip.test.ts @@ -21,6 +21,7 @@ const createUser = (id: number): User => ({ email: `user-${id}@example.com`, emailVerified: null, deletedAt: null, + createdAt: null, image: null, currency: 'USD', }); diff --git a/src/tests/splits.test.ts b/src/tests/splits.test.ts index e9ca093..c602b35 100644 --- a/src/tests/splits.test.ts +++ b/src/tests/splits.test.ts @@ -12,6 +12,7 @@ const createUser = (id: number): User => ({ email: `user-${id}@example.com`, emailVerified: null, deletedAt: null, + createdAt: null, image: null, currency: 'USD', }); From 930a409b357ee55f6c543b967b11d86642efc913 Mon Sep 17 00:00:00 2001 From: KM Koushik Date: Tue, 17 Nov 2026 10:36:12 +1100 Subject: [PATCH 2/2] fix: backfill User.createdAt in batches after startup, from trustworthy sources The SQL migration is now DDL only (instant, lock_timeout 5s). Existing rows are estimated by a data migration that runs in the background after the server starts: 500 users per short transaction, only NULLs, statement and lock timeouts, retried on the next start if it fails. Estimates only use times the user already existed at (expenses they added, their balances, friendships, sessions, push devices, app feedback, emailVerified), capped at the migration time. --- .../migration.sql | 52 +--- src/instrumentation.ts | 11 + src/migrations/backfillUserCreatedAt.ts | 89 ++++++ src/migrations/index.ts | 78 +++++ src/server/api/services/adminStats.ts | 37 +-- src/server/userCreatedAt.ts | 25 ++ src/tests/adminStats.integration.test.ts | 282 +++++++++++++----- 7 files changed, 440 insertions(+), 134 deletions(-) create mode 100644 src/migrations/backfillUserCreatedAt.ts create mode 100644 src/migrations/index.ts create mode 100644 src/server/userCreatedAt.ts diff --git a/prisma/migrations/20261117120000_add_user_created_at/migration.sql b/prisma/migrations/20261117120000_add_user_created_at/migration.sql index b705d9b..b415498 100644 --- a/prisma/migrations/20261117120000_add_user_created_at/migration.sql +++ b/prisma/migrations/20261117120000_add_user_created_at/migration.sql @@ -1,47 +1,13 @@ --- Users had no creation time. Add one, nullable: new rows get the time they are created, rows from --- before this migration get an estimate (their earliest recorded activity) or stay null without any. +-- Fail fast rather than queue every query on "User" behind this migration while the table is busy +SET lock_timeout = '5s'; --- AlterTable -ALTER TABLE "User" ADD COLUMN "createdAt" TIMESTAMP(3); +-- Users had no creation time. Both statements only change the catalog, so they are instant: +-- existing rows stay NULL (estimated later by the backfillUserCreatedAt data migration in +-- src/migrations, which never touches a set value) and new rows get the time they are created. +-- If it times out, run `prisma migrate resolve --rolled-back 20261117120000_add_user_created_at` +-- and deploy again: both statements are safe to repeat. --- Backfill: the earliest timestamp tied to each user, in one UPDATE. Every source is at or after --- the user existed, so the estimate errs late, never early: --- - expense times are when it was added to SplitPro, not its expense date --- - `emailVerified` is their latest email sign-in --- - a session's `expires` minus 30 days (the shortest session length, web; app sessions last --- 180) is at or after it was last renewed; capped at now --- Accounts (OAuth links) and verification tokens have no timestamps. Each table is read once and --- grouped by user: most of these user columns have no index, so per-user lookups would scan. -WITH "firstSeen" AS ( - SELECT "userId", MIN("at") AS "at" - FROM ( - SELECT "addedBy" AS "userId", "createdAt" AS "at" FROM "Expense" - UNION ALL SELECT "paidBy", "createdAt" FROM "Expense" - UNION ALL - SELECT ep."userId", e."createdAt" - FROM "ExpenseParticipant" ep - JOIN "Expense" e ON e."id" = ep."expenseId" - UNION ALL SELECT "createdById", "createdAt" FROM "ExpenseNote" - UNION ALL SELECT "userId", "createdAt" FROM "Group" - UNION ALL SELECT "userId", "createdAt" FROM "Friendship" - UNION ALL SELECT "friendId", "createdAt" FROM "Friendship" - UNION ALL SELECT "userId", "createdAt" FROM "Balance" - UNION ALL SELECT "friendId", "createdAt" FROM "Balance" - UNION ALL SELECT "createdById", "createdAt" FROM "RecurringExpense" - UNION ALL SELECT "userId", "createdAt" FROM "DevicePushToken" - UNION ALL SELECT "userId", "createdAt" FROM "Feedback" - UNION ALL - SELECT "userId", LEAST("expires" - INTERVAL '30 days', NOW() AT TIME ZONE 'UTC') - FROM "Session" - UNION ALL SELECT "id", "emailVerified" FROM "User" - ) AS "seen" - WHERE "userId" IS NOT NULL AND "at" IS NOT NULL - GROUP BY "userId" -) -UPDATE "User" u -SET "createdAt" = f."at" -FROM "firstSeen" f -WHERE f."userId" = u."id"; +-- AlterTable +ALTER TABLE "User" ADD COLUMN IF NOT EXISTS "createdAt" TIMESTAMP(3); --- Only rows created from now on get a default ALTER TABLE "User" ALTER COLUMN "createdAt" SET DEFAULT CURRENT_TIMESTAMP; diff --git a/src/instrumentation.ts b/src/instrumentation.ts index 991eb34..fa3c8e0 100644 --- a/src/instrumentation.ts +++ b/src/instrumentation.ts @@ -10,6 +10,17 @@ export async function register() { validateAuthEnv(); const { env } = await import('./env'); + + if ('phase-production-build' !== process.env.NEXT_PHASE) { + const [{ runDataMigrations }, { db }] = await Promise.all([ + import('./migrations'), + import('./server/db'), + ]); + // Not awaited: the server starts serving (and passes its healthcheck) while these run. + // It logs its own errors and never rejects. + void runDataMigrations(db); + } + if (!env.RECURRING_RUNNER_DISABLED && 'phase-production-build' !== process.env.NEXT_PHASE) { const { startRecurringRunner } = await import('./server/api/services/recurringRunner'); startRecurringRunner( diff --git a/src/migrations/backfillUserCreatedAt.ts b/src/migrations/backfillUserCreatedAt.ts new file mode 100644 index 0000000..527c4cc --- /dev/null +++ b/src/migrations/backfillUserCreatedAt.ts @@ -0,0 +1,89 @@ +import { type PrismaClient } from '@prisma/client'; + +import { getCreatedAtExactSince } from '~/server/userCreatedAt'; + +export const BACKFILL_BATCH_SIZE = 500; +const STATEMENT_TIMEOUT_MS = 5000; +const LOCK_TIMEOUT_MS = 2000; + +type Db = Pick; + +/** + * Estimates `User.createdAt` for users from before the column existed, the rows still NULL. + * + * The estimate is the earliest of the timestamps that are written at the moment the user did or + * was linked to something, so they already existed then: + * - expenses they added (`addedBy`), notes they wrote, recurring rules they set up + * - their balances and friendships, created when the user was first involved + * - their push devices' `updatedAt` (a token moving to another user keeps its `createdAt`) + * - their sessions' expiry minus 30 days, the shortest session length (app sessions last longer) + * - `emailVerified`, set when they sign in by email + * - feedback they sent from the app (imported feedback has external, email-matched times) + * Left out because they can predate the user: expenses they only pay for or share (payers and + * participants can be edited in later), groups they own (ownership moves when an owner leaves) + * and group membership (no timestamp). Estimates are capped at when the column was added. + * + * Users with none of these stay NULL. Works through users in id order, `batchSize` per short + * transaction, and only ever fills NULLs: safe to run again and never overwrites an exact time. + */ +export const backfillUserCreatedAt = async ( + db: Db, + { batchSize = BACKFILL_BATCH_SIZE }: { batchSize?: number } = {}, +) => { + const cap = (await getCreatedAtExactSince(db)) ?? new Date(); + const capAt = cap.toISOString().slice(0, 23); + + let afterId = 0; + let batches = 0; + let updated = 0; + + for (;;) { + const ids = ( + await db.$queryRaw<{ id: number }[]>` + SELECT "id" FROM "User" + WHERE "createdAt" IS NULL AND "id" > ${afterId} + ORDER BY "id" LIMIT ${batchSize} + ` + ).map(({ id }) => id); + + if (0 === ids.length) { + return { batches, updated }; + } + + afterId = ids.at(-1)!; + batches += 1; + updated += await db.$transaction(async (tx) => { + await tx.$executeRawUnsafe(`SET LOCAL statement_timeout = ${STATEMENT_TIMEOUT_MS}`); + await tx.$executeRawUnsafe(`SET LOCAL lock_timeout = ${LOCK_TIMEOUT_MS}`); + return tx.$executeRaw` + WITH "seen" AS ( + SELECT "addedBy" AS "userId", "createdAt" AS "at" FROM "Expense" + WHERE "addedBy" = ANY(${ids}) + UNION ALL SELECT "createdById", "createdAt" FROM "ExpenseNote" + WHERE "createdById" = ANY(${ids}) + UNION ALL SELECT "createdById", "createdAt" FROM "RecurringExpense" + WHERE "createdById" = ANY(${ids}) + UNION ALL SELECT "userId", "createdAt" FROM "Balance" WHERE "userId" = ANY(${ids}) + UNION ALL SELECT "userId", "createdAt" FROM "Friendship" WHERE "userId" = ANY(${ids}) + UNION ALL SELECT "friendId", "createdAt" FROM "Friendship" WHERE "friendId" = ANY(${ids}) + UNION ALL SELECT "userId", "updatedAt" FROM "DevicePushToken" WHERE "userId" = ANY(${ids}) + UNION ALL SELECT "userId", "expires" - INTERVAL '30 days' FROM "Session" + WHERE "userId" = ANY(${ids}) + UNION ALL SELECT "userId", "createdAt" FROM "Feedback" + WHERE "userId" = ANY(${ids}) AND "source" <> 'import' + UNION ALL SELECT "id", "emailVerified" FROM "User" WHERE "id" = ANY(${ids}) + ), + "estimate" AS ( + SELECT "userId", LEAST(MIN("at"), ${capAt}::timestamp) AS "at" + FROM "seen" + WHERE "at" IS NOT NULL + GROUP BY "userId" + ) + UPDATE "User" u + SET "createdAt" = e."at" + FROM "estimate" e + WHERE u."id" = e."userId" AND u."createdAt" IS NULL + `; + }); + } +}; diff --git a/src/migrations/index.ts b/src/migrations/index.ts new file mode 100644 index 0000000..d5e57c3 --- /dev/null +++ b/src/migrations/index.ts @@ -0,0 +1,78 @@ +import { type PrismaClient } from '@prisma/client'; + +import { backfillUserCreatedAt } from './backfillUserCreatedAt'; + +/** + * Data migrations: changes to existing rows that are too slow or too involved for a SQL migration, + * run in the background after the server starts (see `instrumentation.ts`). + * + * Like upstream SplitPro's runner, the version reached is kept in AppMetadata and each migration + * runs once, in order, setting the version when it completes. The key is our own, not upstream's + * `schema_version`, whose runner treats a missing value as a pre-2.0.0 database. + * + * A failing migration is logged and leaves the version as it was, so it runs again on the next + * start; it never stops the server. Migrations must be safe to run again. + */ + +export const DATA_VERSION_KEY = 'cloud_data_version'; + +type Db = Pick; + +export type DataMigration = { + /** Semver-like, compared as strings like upstream's runner */ + version: string; + name: string; + run: (db: Db) => Promise; +}; + +export const USER_CREATED_AT_BACKFILL_VERSION = '1.0.0'; + +export const DATA_MIGRATIONS: DataMigration[] = [ + { + version: USER_CREATED_AT_BACKFILL_VERSION, + name: 'Estimate User.createdAt', + run: (db) => backfillUserCreatedAt(db), + }, +]; + +const getVersion = async (db: Db) => + (await db.appMetadata.findUnique({ where: { key: DATA_VERSION_KEY } }))?.value ?? null; + +const setVersion = (db: Db, version: string) => + db.appMetadata.upsert({ + where: { key: DATA_VERSION_KEY }, + update: { value: version }, + create: { key: DATA_VERSION_KEY, value: version }, + }); + +/** Only an error's name and Prisma or Node code: messages can contain data. */ +const safeErrorDetails = (error: unknown) => { + if (!(error instanceof Error)) { + return { name: typeof error }; + } + const { code } = error as { code?: unknown }; + return { name: error.name, code: 'string' === typeof code ? code : undefined }; +}; + +/** Runs the pending data migrations. Never throws. */ +export const runDataMigrations = async (db: Db, migrations = DATA_MIGRATIONS) => { + try { + const current = await getVersion(db); + const pending = migrations.filter(({ version }) => null === current || version > current); + + // One after another: each one starts from the previous one's result + for (const migration of pending) { + const startedAt = Date.now(); + const result = await migration.run(db); + await setVersion(db, migration.version); + console.log(`Data migration ${migration.version} (${migration.name}) done`, { + ms: Date.now() - startedAt, + result, + }); + } + return true; + } catch (error) { + console.error('Data migration failed, will retry on next start', safeErrorDetails(error)); + return false; + } +}; diff --git a/src/server/api/services/adminStats.ts b/src/server/api/services/adminStats.ts index 6a6ac3a..a2970f1 100644 --- a/src/server/api/services/adminStats.ts +++ b/src/server/api/services/adminStats.ts @@ -1,6 +1,9 @@ import { Prisma, type PrismaClient } from '@prisma/client'; import { z } from 'zod'; +import { DATA_VERSION_KEY, USER_CREATED_AT_BACKFILL_VERSION } from '~/migrations'; +import { CREATED_AT_MIGRATION, getCreatedAtExactSince } from '~/server/userCreatedAt'; + /** * Read-only growth numbers for the internal admin API (GET /api/admin/stats). * @@ -10,9 +13,10 @@ import { z } from 'zod'; * who never signed in, are counted apart as `invitedUsers`. Deleted (anonymised) rows are never * counted as users. * - *New users* are users whose `User.createdAt` falls on that day (`newUsersMethod`). Rows from - * before that column existed hold an estimate, their earliest recorded activity, so days before - * `newUsersExactSince` are approximate and users with no activity at all are missing from them. - * Someone invited first and signing in later counts on the day they were invited. + * before that column existed hold an estimate (see `backfillUserCreatedAt`) once + * `newUsersBackfilled`, so days before `newUsersExactSince` are approximate and users without + * any signal are missing from them. Someone invited first and signing in later counts on the + * day they were invited. * - An *expense event* is an expense that isn't deleted and was added by a person: occurrences the * recurring runner created on its own (`recurringId` set and not the rule's first date) are * left out of activity and counted as `recurringCreated` instead. Its time is `Expense.createdAt`. @@ -30,7 +34,6 @@ export const WEEKS = 12; export const MONTHS = 6; const STATEMENT_TIMEOUT_MS = 5000; const DAY_MS = 24 * 60 * 60 * 1000; -const CREATED_AT_MIGRATION = '20261117120000_add_user_created_at'; export const adminStatsQuery = z.object({ days: z.coerce @@ -265,24 +268,11 @@ const getActiveUsers = async ( FROM "active" WHERE "at" >= ${ts(firstMonth)} GROUP BY 2 `; -/** When `User.createdAt` started being set on create, or null if unknown (no migration table). */ -const getCreatedAtExactSince = async (tx: Tx) => { - const [table] = await tx.$queryRaw<{ exists: boolean }[]>` - SELECT to_regclass('"_prisma_migrations"') IS NOT NULL AS "exists" - `; - if (!table?.exists) { - return null; - } - const [row] = await tx.$queryRaw<{ finishedAt: Date | null }[]>` - SELECT "finished_at" AS "finishedAt" FROM "_prisma_migrations" - WHERE "migration_name" = ${CREATED_AT_MIGRATION} AND "rolled_back_at" IS NULL - ORDER BY "finished_at" DESC NULLS LAST LIMIT 1 - `; - return row?.finishedAt ?? null; -}; - const ACTIVE_FIELDS = ['activeUsers', 'activeCreators'] as const; +const isBackfilled = (dataVersion: string | null) => + null !== dataVersion && dataVersion >= USER_CREATED_AT_BACKFILL_VERSION; + /** How `newUsers` is counted, with the date it became exact. */ export const newUsersMethod = (exactSince: Date | null) => `createdAt (estimated before ${exactSince ? dayKey(exactSince) : CREATED_AT_MIGRATION})`; @@ -312,7 +302,10 @@ export const getAdminStats = async ( end, }); const exactSince = await getCreatedAtExactSince(tx); - return { totals, daily, active, exactSince }; + const [version] = await tx.$queryRaw<{ value: string }[]>` + SELECT "value" FROM "AppMetadata" WHERE "key" = ${DATA_VERSION_KEY} + `; + return { totals, daily, active, exactSince, dataVersion: version?.value ?? null }; }, { isolationLevel: Prisma.TransactionIsolationLevel.RepeatableRead, timeout: 20_000 }, ); @@ -338,6 +331,8 @@ export const getAdminStats = async ( newUsersMethod: newUsersMethod(data.exactSince), /** Days before this are estimates from earliest activity; null if unknown */ newUsersExactSince: data.exactSince?.toISOString() ?? null, + /** Whether the estimates for users from before then have been filled in yet */ + newUsersBackfilled: isBackfilled(data.dataVersion), totals: data.totals, daily: dayKeys.map((date, i) => ({ date, diff --git a/src/server/userCreatedAt.ts b/src/server/userCreatedAt.ts new file mode 100644 index 0000000..22e5040 --- /dev/null +++ b/src/server/userCreatedAt.ts @@ -0,0 +1,25 @@ +import { type Prisma, type PrismaClient } from '@prisma/client'; + +/** The migration that added `User.createdAt`: rows created since then hold their exact time. */ +export const CREATED_AT_MIGRATION = '20261117120000_add_user_created_at'; + +type Client = Pick; + +/** + * When `User.createdAt` started being set on create: the time the migration finished. Null when + * it's unknown (a database set up without migrations). + */ +export const getCreatedAtExactSince = async (client: Client) => { + const [table] = await client.$queryRaw<{ exists: boolean }[]>` + SELECT to_regclass('"_prisma_migrations"') IS NOT NULL AS "exists" + `; + if (!table?.exists) { + return null; + } + const [row] = await client.$queryRaw<{ finishedAt: Date | null }[]>` + SELECT "finished_at" AS "finishedAt" FROM "_prisma_migrations" + WHERE "migration_name" = ${CREATED_AT_MIGRATION} AND "rolled_back_at" IS NULL + ORDER BY "finished_at" DESC NULLS LAST LIMIT 1 + `; + return row?.finishedAt ?? null; +}; diff --git a/src/tests/adminStats.integration.test.ts b/src/tests/adminStats.integration.test.ts index 3832bf9..35126aa 100644 --- a/src/tests/adminStats.integration.test.ts +++ b/src/tests/adminStats.integration.test.ts @@ -1,9 +1,10 @@ import assert from 'node:assert/strict'; import { execFileSync } from 'node:child_process'; -import { readFileSync } from 'node:fs'; import { after, before, test } from 'node:test'; import { type PrismaClient } from '@prisma/client'; +import type * as Migrations from '~/migrations'; +import type * as Backfill from '~/migrations/backfillUserCreatedAt'; import type * as AdminStats from '~/server/api/services/adminStats'; const databaseUrl = process.env.TEST_DATABASE_URL; @@ -21,6 +22,8 @@ const schemaUrl = () => { let db: PrismaClient; let stats: typeof AdminStats; +let migrations: typeof Migrations; +let backfill: typeof Backfill; before(async () => { if (!databaseUrl) { @@ -35,9 +38,11 @@ before(async () => { stdio: 'ignore', }); - [{ db }, stats] = await Promise.all([ + [{ db }, stats, migrations, backfill] = await Promise.all([ import('~/server/db'), import('~/server/api/services/adminStats'), + import('~/migrations'), + import('~/migrations/backfillUserCreatedAt'), ]); }); @@ -52,7 +57,7 @@ after(async () => { const clearAll = () => db.$executeRawUnsafe( `TRUNCATE "User", "Account", "Session", "Group", "Expense", "RecurringExpense", - "PushNotification" RESTART IDENTITY CASCADE`, + "PushNotification", "Feedback", "AppMetadata" RESTART IDENTITY CASCADE`, ); // A Wednesday @@ -83,6 +88,7 @@ void test('admin stats: an empty database', { skip: !databaseUrl }, async () => assert.ok(result.weekly.every(({ activeUsers }) => 0 === activeUsers)); assert.equal(result.platforms, null); assert.match(result.newUsersMethod, /^createdAt \(estimated before \d{4}-\d{2}-\d{2}\)$/); + assert.equal(result.newUsersBackfilled, false); }); void test( @@ -292,91 +298,227 @@ void test( }, ); -/** The backfill statement of the createdAt migration, run again on rows without one. */ -const backfillSql = () => { - const sql = readFileSync( - 'prisma/migrations/20261117120000_add_user_created_at/migration.sql', - 'utf8', - ) - .split('\n') - .filter((line) => !line.trim().startsWith('--')) - .join('\n'); - const statement = sql.split(';').find((part) => part.trim().startsWith('WITH')); - assert.ok(statement, 'backfill statement'); - return statement; -}; +const expenseBy = ( + id: string, + addedBy: number, + createdAt: Date, + { paidBy = addedBy, participants = [addedBy] }: { paidBy?: number; participants?: number[] } = {}, +) => + db.expense.create({ + data: { + id, + addedBy, + paidBy, + name: id, + category: 'general', + amount: 100, + currency: 'USD', + createdAt, + expenseDate: createdAt, + expenseParticipants: { create: participants.map((userId) => ({ userId, amount: 0 })) }, + }, + }); + +/** A user from before `createdAt` existed: no value until the backfill fills one in. */ +const legacyUser = (id: number, data: Record = {}) => + db.user.create({ data: { id, email: `b${id}@example.com`, createdAt: null, ...data } }); + +const createdAtById = async () => + Object.fromEntries( + (await db.user.findMany({ select: { id: true, createdAt: true }, orderBy: { id: 'asc' } })).map( + (row) => [row.id, row.createdAt?.toISOString() ?? null], + ), + ); void test( - 'admin stats: the createdAt backfill estimates from earliest activity', + 'createdAt backfill: estimates only from times the user already existed at', { skip: !databaseUrl }, async () => { await clearAll(); - const make = (id: number, data: Record = {}) => - db.user.create({ data: { id, email: `b${id}@example.com`, ...data } }); - - const payer = await make(1, { emailVerified: at('2026-09-01T00:00:00') }); - const participant = await make(2); - const sessionOnly = await make(3); - // Nothing tied to them - await make(4); - const friendOnly = await make(5); - const renewed = await make(6); - - await db.expense.create({ + const cap = ( + await db.$queryRaw<{ finished_at: Date }[]>` + SELECT "finished_at" FROM "_prisma_migrations" + WHERE "migration_name" = '20261117120000_add_user_created_at' + ` + )[0]!.finished_at; + + const alice = await legacyUser(1, { emailVerified: at('2026-09-01T00:00:00') }); + // Invited recently, then added as payer and participant to an expense from 2020 + const invited = await legacyUser(2); + const appUser = await legacyUser(3); + const sessionOnly = await legacyUser(4); + const renewed = await legacyUser(5); + const groupOwner = await legacyUser(6); + const deviceUser = await legacyUser(7); + + await expenseBy('old', alice.id, at('2020-01-01T00:00:00'), { + paidBy: invited.id, + participants: [alice.id, invited.id], + }); + await db.balance.create({ data: { - id: 'x1', - addedBy: payer.id, - paidBy: payer.id, - name: 'x1', - category: 'general', - amount: 100, + userId: invited.id, + friendId: alice.id, currency: 'USD', - createdAt: at('2026-03-01T10:00:00'), - // Before the user existed: the expense date must not be used - expenseDate: at('2020-01-01T00:00:00'), - expenseParticipants: { - create: [ - { userId: payer.id, amount: 0 }, - { userId: participant.id, amount: 0 }, - ], - }, + amount: 0, + createdAt: at('2026-11-10T00:00:00'), }, }); - await db.friendship.create({ - data: { userId: payer.id, friendId: friendOnly.id, createdAt: at('2026-02-01T00:00:00') }, + + // Imported feedback carries an external time; feedback sent from the app counts + await db.feedback.createMany({ + data: [ + { + userId: appUser.id, + email: 'x@example.com', + message: 'old', + source: 'import', + createdAt: at('2019-05-01T00:00:00'), + }, + { + userId: appUser.id, + email: 'x@example.com', + message: 'new', + source: 'ios', + createdAt: at('2026-04-01T00:00:00'), + }, + ], }); - await db.session.create({ - data: { sessionToken: 's1', userId: sessionOnly.id, expires: at('2026-07-31T00:00:00') }, + + await db.session.createMany({ + data: [ + { sessionToken: 's1', userId: sessionOnly.id, expires: at('2026-07-31T00:00:00') }, + // Renewed now by the app, expiring months after the column was added: capped + { sessionToken: 's2', userId: renewed.id, expires: new Date(Date.now() + 180 * 86400e3) }, + ], }); - // A session renewed today, which expires after the backfill runs: capped at now - await db.session.create({ + + // Groups change owner when one leaves, so owning an old group says nothing + await db.group.create({ data: { - sessionToken: 's2', - userId: renewed.id, - expires: new Date(Date.now() + 180 * 86400e3), + publicId: 'old-group', + name: 'Old', + userId: groupOwner.id, + createdAt: at('2019-01-01T00:00:00'), }, }); - await db.$executeRawUnsafe(`UPDATE "User" SET "createdAt" = NULL`); - await db.$executeRawUnsafe(backfillSql()); + // A token registered by someone else long ago, now moved to this user + await db.devicePushToken.create({ + data: { + userId: deviceUser.id, + token: 'tok', + platform: 'ios', + createdAt: at('2018-01-01T00:00:00'), + }, + }); + const { updatedAt: movedAt } = await db.devicePushToken.update({ + where: { token: 'tok' }, + data: { updatedAt: at('2026-10-01T00:00:00') }, + }); - const rows = await db.user.findMany({ select: { id: true, createdAt: true } }); - const renewedAt = rows.find(({ id }) => renewed.id === id)?.createdAt?.getTime() ?? 0; - assert.ok(Math.abs(Date.now() - renewedAt) < 60_000, 'capped at now'); + const result = await backfill.backfillUserCreatedAt(db); + assert.deepEqual(result, { batches: 1, updated: 6 }); - const createdAt = Object.fromEntries( - rows - .filter(({ id }) => renewed.id !== id) - .map((row) => [row.id, row.createdAt?.toISOString() ?? null]), - ); + const createdAt = await createdAtById(); assert.deepEqual(createdAt, { - // Friendship (Feb) is earlier than the expense and emailVerified - 1: '2026-02-01T00:00:00.000Z', - 2: '2026-03-01T10:00:00.000Z', - 3: '2026-07-01T00:00:00.000Z', - // Nothing tied to them: left null - 4: null, - 5: '2026-02-01T00:00:00.000Z', + // Added the 2020 expense themselves + 1: '2020-01-01T00:00:00.000Z', + // Their balance, not the old expense they were added to + 2: '2026-11-10T00:00:00.000Z', + 3: '2026-04-01T00:00:00.000Z', + 4: '2026-07-01T00:00:00.000Z', + 5: cap.toISOString(), + 6: null, + 7: movedAt.toISOString(), }); }, ); + +void test( + 'createdAt backfill: keeps exact times, changes nothing when run again, covers every batch', + { skip: !databaseUrl }, + async () => { + await clearAll(); + + // Created after the column was added: its exact time stays, despite older activity + const exact = await db.user.create({ data: { id: 1, email: 'exact@example.com' } }); + assert.ok(exact.createdAt); + await expenseBy('older', exact.id, at('2020-01-01T00:00:00')); + + // Seven legacy users with a signal, one without, across batches of two + await Promise.all( + [2, 3, 4, 5, 6, 7, 8].map((id) => + legacyUser(id, { emailVerified: at(`2026-0${id}-01T00:00:00`) }), + ), + ); + await legacyUser(9); + + const first = await backfill.backfillUserCreatedAt(db, { batchSize: 2 }); + assert.deepEqual(first, { batches: 4, updated: 7 }); + + const after = await createdAtById(); + assert.equal(after[1], exact.createdAt.toISOString()); + [2, 3, 4, 5, 6, 7, 8].forEach((id) => { + assert.equal(after[id], `2026-0${id}-01T00:00:00.000Z`, `user ${id}`); + }); + assert.equal(after[9], null); + + // Only user 9 is left to look at, and it still has nothing + const second = await backfill.backfillUserCreatedAt(db, { batchSize: 2 }); + assert.deepEqual(second, { batches: 1, updated: 0 }); + assert.deepEqual(await createdAtById(), after); + }, +); + +void test( + 'data migrations: run once, and a failure is logged without stopping startup', + { skip: !databaseUrl }, + async () => { + await clearAll(); + const runs: string[] = []; + const errors: unknown[] = []; + const originalError = console.error; + const originalLog = console.log; + console.error = (...args: unknown[]) => errors.push(args); + console.log = () => undefined; + + try { + const ok = { version: '1.0.0', name: 'ok', run: async () => runs.push('ok') }; + const failing = { + version: '1.1.0', + name: 'failing', + run: async () => { + runs.push('failing'); + throw new Error('boom user@example.com'); + }, + }; + + assert.equal(await migrations.runDataMigrations(db, [ok, failing]), false); + const version = () => + db.appMetadata.findUnique({ where: { key: migrations.DATA_VERSION_KEY } }); + assert.equal((await version())?.value, '1.0.0'); + assert.ok(!JSON.stringify(errors).includes('user@example.com')); + + // Next start: only the failed one runs again + assert.equal( + await migrations.runDataMigrations(db, [ + ok, + { ...failing, run: async () => runs.push('fixed') }, + ]), + true, + ); + assert.deepEqual(runs, ['ok', 'failing', 'fixed']); + assert.equal((await version())?.value, '1.1.0'); + + // The real list marks the backfill done for the stats + await db.appMetadata.deleteMany(); + assert.equal(await migrations.runDataMigrations(db), true); + const result = await stats.getAdminStats(db, { days: 1, now: NOW }); + assert.equal(result.newUsersBackfilled, true); + } finally { + console.error = originalError; + console.log = originalLog; + } + }, +);