|
| 1 | +import type { Pool } from 'pg'; |
| 2 | + |
| 3 | +import type { CardRef, FrameRef } from '../src/lib/chat/widgets'; |
| 4 | +import type { StoredCard } from '../src/lib/studio/cardRefMatch.ts'; |
| 5 | + |
| 6 | +/** |
| 7 | + * The database side of the card reference backfill |
| 8 | + * (scripts/backfill-card-refs.ts): the counts it reports, the stored |
| 9 | + * `widgets` arrays it reads, and the write that gives a matched card its |
| 10 | + * `ref` and `frame`. The matching itself is src/lib/studio/cardRefMatch.ts. |
| 11 | + */ |
| 12 | + |
| 13 | +/** The tables whose `data` holds Studio cards under `widgets`. */ |
| 14 | +export type CardTable = 'conversation' | 'share'; |
| 15 | + |
| 16 | +// A `search` share holds no cards, so only the Studio kinds are read. |
| 17 | +const ROWS: Record<CardTable, string> = { |
| 18 | + conversation: 'conversation', |
| 19 | + share: `share WHERE kind IN ('conversation', 'canvas', 'card')`, |
| 20 | +}; |
| 21 | + |
| 22 | +export type CardCounts = { |
| 23 | + // Shares by kind and mode. |
| 24 | + shares: { kind: string; mode: string | null; count: number }[]; |
| 25 | + // Per table: data cards with no component reference, and cards drawn |
| 26 | + // from a derived (`ops_`) frame by the week the row was created. |
| 27 | + tables: Record< |
| 28 | + CardTable, |
| 29 | + { rows: number; withoutRef: number; derivedByWeek: { week: string; count: number }[] } |
| 30 | + >; |
| 31 | +}; |
| 32 | + |
| 33 | +const tableCounts = async ( |
| 34 | + pool: Pool, |
| 35 | + table: CardTable, |
| 36 | +): Promise<CardCounts['tables'][CardTable]> => { |
| 37 | + const rows = await pool.query<{ count: number }>( |
| 38 | + `SELECT count(*)::int AS count FROM ${ROWS[table]}`, |
| 39 | + ); |
| 40 | + const withoutRef = await pool.query<{ count: number }>( |
| 41 | + `SELECT count(*)::int AS count |
| 42 | + FROM (SELECT data FROM ${ROWS[table]}) s, jsonb_array_elements(s.data->'widgets') w |
| 43 | + WHERE w->>'kind' <> 'text' AND NOT (w ? 'ref')`, |
| 44 | + ); |
| 45 | + const derived = await pool.query<{ week: string; count: number }>( |
| 46 | + `SELECT to_char(date_trunc('week', s.created_at), 'YYYY-MM-DD') AS week, count(*)::int AS count |
| 47 | + FROM (SELECT data, created_at FROM ${ROWS[table]}) s, |
| 48 | + jsonb_array_elements(s.data->'widgets') w |
| 49 | + WHERE w->'frame'->>'spec_id' LIKE 'ops\\_%' |
| 50 | + GROUP BY 1 ORDER BY 1`, |
| 51 | + ); |
| 52 | + |
| 53 | + return { |
| 54 | + rows: rows.rows[0]?.count ?? 0, |
| 55 | + withoutRef: withoutRef.rows[0]?.count ?? 0, |
| 56 | + derivedByWeek: derived.rows, |
| 57 | + }; |
| 58 | +}; |
| 59 | + |
| 60 | +/** The counts that size the backfill, over `share` and `conversation`. */ |
| 61 | +export const readCardCounts = async (pool: Pool): Promise<CardCounts> => { |
| 62 | + const shares = await pool.query<CardCounts['shares'][number]>( |
| 63 | + 'SELECT kind, mode, count(*)::int AS count FROM share GROUP BY 1, 2 ORDER BY 1, 2', |
| 64 | + ); |
| 65 | + |
| 66 | + return { |
| 67 | + shares: shares.rows, |
| 68 | + tables: { |
| 69 | + share: await tableCounts(pool, 'share'), |
| 70 | + conversation: await tableCounts(pool, 'conversation'), |
| 71 | + }, |
| 72 | + }; |
| 73 | +}; |
| 74 | + |
| 75 | +/** The ids of a table's rows that hold cards, in id order. */ |
| 76 | +export const readCardRowIds = async (pool: Pool, table: CardTable): Promise<string[]> => { |
| 77 | + const { rows } = await pool.query<{ id: string }>(`SELECT id FROM ${ROWS[table]} ORDER BY id`); |
| 78 | + |
| 79 | + return rows.map((row) => row.id); |
| 80 | +}; |
| 81 | + |
| 82 | +/** One row's stored `widgets`, or undefined when the row is gone. */ |
| 83 | +export const readWidgets = async (pool: Pool, table: CardTable, id: string): Promise<unknown> => { |
| 84 | + const { rows } = await pool.query<{ widgets: unknown }>( |
| 85 | + `SELECT data->'widgets' AS widgets FROM ${table} WHERE id = $1`, |
| 86 | + [id], |
| 87 | + ); |
| 88 | + |
| 89 | + return rows[0]?.widgets; |
| 90 | +}; |
| 91 | + |
| 92 | +/** A matched card of one row: where it sits in `widgets`, the card as it |
| 93 | + * was read, and what the match gives it. */ |
| 94 | +export type CardPatch = { index: number; card: StoredCard; ref: CardRef; frame: FrameRef }; |
| 95 | + |
| 96 | +/** |
| 97 | + * Gives each patched card of one row its `ref` and `frame`, in one |
| 98 | + * transaction, and resolves how many cards it wrote. |
| 99 | + * |
| 100 | + * A card is written only while the stored card at its index still equals |
| 101 | + * the card the match read. That card had no `ref`, so a card that has one |
| 102 | + * is never written, a second run writes nothing, and a row saved again |
| 103 | + * since the read keeps the cards it changed. No other member of `data` and |
| 104 | + * no other column is written. |
| 105 | + */ |
| 106 | +export const writeCardRefs = async ( |
| 107 | + pool: Pool, |
| 108 | + table: CardTable, |
| 109 | + id: string, |
| 110 | + patches: CardPatch[], |
| 111 | +): Promise<number> => { |
| 112 | + const client = await pool.connect(); |
| 113 | + |
| 114 | + try { |
| 115 | + await client.query('BEGIN'); |
| 116 | + |
| 117 | + let written = 0; |
| 118 | + |
| 119 | + for (const { index, card, ref, frame } of patches) { |
| 120 | + const result = await client.query( |
| 121 | + `UPDATE ${table} |
| 122 | + SET data = jsonb_set(data, ARRAY['widgets', ($2::int)::text], |
| 123 | + (data->'widgets'->$2::int) || $3::jsonb) |
| 124 | + WHERE id = $1 AND data->'widgets'->$2::int = $4::jsonb`, |
| 125 | + [id, index, JSON.stringify({ ref, frame }), JSON.stringify(card)], |
| 126 | + ); |
| 127 | + |
| 128 | + written += result.rowCount ?? 0; |
| 129 | + } |
| 130 | + |
| 131 | + await client.query('COMMIT'); |
| 132 | + |
| 133 | + return written; |
| 134 | + } catch (error) { |
| 135 | + await client.query('ROLLBACK'); |
| 136 | + |
| 137 | + throw error; |
| 138 | + } finally { |
| 139 | + client.release(); |
| 140 | + } |
| 141 | +}; |
0 commit comments