|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#29520 codemod] The "Column creation helper" section of `SqlDriver`: the |
| 5 | + * bodies of `decorateHashShadowDuplicate`, `schemaBytesPerChar`, |
| 6 | + * `rethrowWithRowSizeExplanation` moved out of `sql-driver.ts` whole by |
| 7 | + * `scripts/codemod-split-sql-driver.mjs`. |
| 8 | + * |
| 9 | + * A MOVE, not a redesign. Each function below is the body of the `SqlDriver` |
| 10 | + * method of the same name exactly as it stood: not re-indented, not |
| 11 | + * formatted, not edited. The method stays on the class with its docblock and |
| 12 | + * signature, so the published `dist/*.d.ts`, an instance spy and a subclass |
| 13 | + * override are what they were; the docblock there is the contract. |
| 14 | + * |
| 15 | + * The bodies reach the class through {@link ColumnCreationHost}: exactly the |
| 16 | + * `SqlDriver` members they read. Each method hands its body one literal of the |
| 17 | + * members that body reads, plus a `.call`: methods go as closures that call |
| 18 | + * them on the driver at call time, so a subclass override or an instance spy |
| 19 | + * is still the one reached; fields and accessors go as GETTERS, read when the |
| 20 | + * body reads them, as they were on the class. |
| 21 | + * |
| 22 | + * The module imports `sql-driver.ts` for types only, so the two modules form |
| 23 | + * no evaluation cycle. `index.ts` re-exports nothing from this module. |
| 24 | + */ |
| 25 | + |
| 26 | +import type { SqlDriver } from './sql-driver.js'; |
| 27 | + |
| 28 | +/** |
| 29 | + * The `SqlDriver` members the functions below read — every one of them, and |
| 30 | + * no other. Each is typed AS the class member (an indexed access on the |
| 31 | + * class), so a change to a member's signature on the class is a change here |
| 32 | + * too. `readonly` members are the getters. |
| 33 | + */ |
| 34 | +export interface ColumnCreationHost { |
| 35 | + explainHashShadowDuplicate: SqlDriver['explainHashShadowDuplicate']; |
| 36 | + explainRowSizeOverflow: SqlDriver['explainRowSizeOverflow']; |
| 37 | + readonly knex: SqlDriver['knex']; |
| 38 | + logDurabilityFailure: SqlDriver['logDurabilityFailure']; |
| 39 | +} |
| 40 | + |
| 41 | +/** The body of `SqlDriver#decorateHashShadowDuplicate`; the method's docblock there is its contract. */ |
| 42 | +export async function decorateHashShadowDuplicate(this: Pick<ColumnCreationHost, 'explainHashShadowDuplicate' | 'logDurabilityFailure'>, tableName: string, values: Record<string, unknown>, error: unknown): Promise<unknown> { |
| 43 | + let explanation: string | null = null; |
| 44 | + try { |
| 45 | + explanation = await this.explainHashShadowDuplicate(tableName, values, error); |
| 46 | + } catch { |
| 47 | + // The disambiguation is a diagnostic. If it cannot be made, the original |
| 48 | + // failure is still the failure — never mask it with this one. |
| 49 | + return error; |
| 50 | + } |
| 51 | + if (!explanation) return error; |
| 52 | + this.logDurabilityFailure(explanation); |
| 53 | + return Object.assign(new Error(`${explanation} (server said: ${String((error as { message?: string })?.message ?? error)})`), { |
| 54 | + code: (error as { code?: string })?.code, |
| 55 | + cause: error, |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | +/** The body of `SqlDriver#schemaBytesPerChar`; the method's docblock there is its contract. */ |
| 60 | +export async function schemaBytesPerChar(this: Pick<ColumnCreationHost, 'knex'>): Promise<{ bytesPerChar: number; charset: string } | null> { |
| 61 | + try { |
| 62 | + const res: any = await this.knex.raw( |
| 63 | + 'select cs.CHARACTER_SET_NAME as charset, cs.MAXLEN as maxlen ' + |
| 64 | + 'from information_schema.CHARACTER_SETS cs ' + |
| 65 | + 'where cs.CHARACTER_SET_NAME = @@character_set_database', |
| 66 | + ); |
| 67 | + const row = (Array.isArray(res) ? res[0]?.[0] : (res?.rows?.[0] ?? res?.[0])) ?? null; |
| 68 | + const maxlen = Number(row?.maxlen ?? row?.MAXLEN); |
| 69 | + const charset = String(row?.charset ?? row?.CHARSET ?? ''); |
| 70 | + if (!Number.isInteger(maxlen) || maxlen <= 0 || !charset) return null; |
| 71 | + return { bytesPerChar: maxlen, charset }; |
| 72 | + } catch { |
| 73 | + // Introspection is a nicety here; the advice below holds without it. |
| 74 | + return null; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | +/** The body of `SqlDriver#rethrowWithRowSizeExplanation`; the method's docblock there is its contract. */ |
| 79 | +export async function rethrowWithRowSizeExplanation(this: Pick<ColumnCreationHost, 'explainRowSizeOverflow' | 'logDurabilityFailure'>, tableName: string, fields: Record<string, any> | undefined, keyedColumns: ReadonlyMap<string, { unique: boolean }>, addedColumns: string[] | null, e: any): Promise<never> { |
| 80 | + const overflow = await this.explainRowSizeOverflow(tableName, fields, keyedColumns, addedColumns, e); |
| 81 | + if (!overflow) throw e; |
| 82 | + const said = String(e?.sqlMessage ?? e?.message ?? e); |
| 83 | + this.logDurabilityFailure(overflow, said); |
| 84 | + throw Object.assign(new Error(`${overflow} (server said: ${said})`), { |
| 85 | + code: (e as { code?: string }).code, |
| 86 | + cause: e, |
| 87 | + }); |
| 88 | + } |
0 commit comments