• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vzakharchenko / forge-sql-orm / 19835613480

01 Dec 2025 07:55PM UTC coverage: 85.065% (+0.04%) from 85.029%
19835613480

push

github

web-flow
Merge pull request #1149 from vzakharchenko/improveMigrationGenerator

improved migration generator

665 of 845 branches covered (78.7%)

Branch coverage included in aggregate %.

122 of 148 new or added lines in 4 files covered. (82.43%)

1 existing line in 1 file now uncovered.

1243 of 1398 relevant lines covered (88.91%)

20.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.58
/src/utils/sqlUtils.ts
1
import {
2
  and,
3
  AnyColumn,
4
  Column,
5
  gte,
6
  ilike,
7
  isNotNull,
8
  isTable,
9
  ne,
10
  not,
11
  notInArray,
12
  SQL,
13
  sql,
14
  StringChunk,
15
} from "drizzle-orm";
16
import { AnyMySqlTable, MySqlCustomColumn } from "drizzle-orm/mysql-core/index";
17
import { DateTime } from "luxon";
18
import { PrimaryKeyBuilder } from "drizzle-orm/mysql-core/primary-keys";
19
import { AnyIndexBuilder } from "drizzle-orm/mysql-core/indexes";
20
import { CheckBuilder } from "drizzle-orm/mysql-core/checks";
21
import { ForeignKeyBuilder } from "drizzle-orm/mysql-core/foreign-keys";
22
import { UniqueConstraintBuilder } from "drizzle-orm/mysql-core/unique-constraint";
23
import { SelectedFields } from "drizzle-orm/mysql-core/query-builders/select.types";
24
import { MySqlTable } from "drizzle-orm/mysql-core";
25
import { isSQLWrapper } from "drizzle-orm/sql/sql";
26
import { clusterStatementsSummary, slowQuery } from "../core/SystemTables";
27
import { ForgeSqlOperation } from "../core/ForgeSQLQueryBuilder";
28
import { ColumnDataType } from "drizzle-orm/column-builder";
29
import { AnyMySqlColumn } from "drizzle-orm/mysql-core/columns/common";
30
import type { ColumnBaseConfig } from "drizzle-orm/column";
31

32
/**
33
 * Interface representing table metadata information
34
 */
35
export interface MetadataInfo {
36
  /** The name of the table */
37
  tableName: string;
38
  /** Record of column names and their corresponding column definitions */
39
  columns: Record<string, AnyColumn>;
40
  /** Array of index builders */
41
  indexes: AnyIndexBuilder[];
42
  /** Array of check constraint builders */
43
  checks: CheckBuilder[];
44
  /** Array of foreign key builders */
45
  foreignKeys: ForeignKeyBuilder[];
46
  /** Array of primary key builders */
47
  primaryKeys: PrimaryKeyBuilder[];
48
  /** Array of unique constraint builders */
49
  uniqueConstraints: UniqueConstraintBuilder[];
50
  /** Array of all extra builders */
51
  extras: any[];
52
}
53

54
/**
55
 * Interface for config builder data
56
 */
57
interface ConfigBuilderData {
58
  value?: any;
59
  [key: string]: any;
60
}
61

62
/**
63
 * Parses a date string into a Date object using the specified format
64
 * @param value - The date string to parse or Date
65
 * @param format - The format to use for parsing
66
 * @returns Date object
67
 */
68

69
export const parseDateTime = (value: string | Date, format: string): Date => {
7✔
70
  let result: Date;
71
  if (value instanceof Date) {
23✔
72
    result = value;
2✔
73
  } else {
74
    // 1. Try to parse using the provided format (strict mode)
75
    const dt = DateTime.fromFormat(value, format);
21✔
76
    if (dt.isValid) {
21✔
77
      result = dt.toJSDate();
7✔
78
    } else {
79
      // 2. Try to parse as SQL string
80
      const sqlDt = DateTime.fromSQL(value);
14✔
81
      if (sqlDt.isValid) {
14✔
82
        result = sqlDt.toJSDate();
4✔
83
      } else {
84
        // 3. Try to parse as RFC2822 string
85
        const isoDt = DateTime.fromRFC2822(value);
10✔
86
        if (isoDt.isValid) {
10!
87
          result = isoDt.toJSDate();
×
88
        } else {
89
          // 4. Fallback: use native Date constructor
90
          result = new Date(value);
10✔
91
        }
92
      }
93
    }
94
  }
95
  // 4. Ensure the result is a valid Date object
96
  if (Number.isNaN(result.getTime())) {
23✔
97
    result = new Date(value);
7✔
98
  }
99
  return result;
23✔
100
};
101

102
/**
103
 * Parses a string value into DateTime using multiple format parsers
104
 */
105
function parseStringToDateTime(value: string): DateTime | null {
106
  const parsers = [DateTime.fromISO, DateTime.fromRFC2822, DateTime.fromSQL, DateTime.fromHTTP];
3✔
107

108
  for (const parser of parsers) {
3✔
109
    const dt = parser(value);
8✔
110
    if (dt.isValid) {
8✔
111
      return dt;
2✔
112
    }
113
  }
114

115
  // Try parsing as number string
116
  const parsed = Number(value);
1✔
117
  if (!Number.isNaN(parsed)) {
1!
NEW
118
    return DateTime.fromMillis(parsed);
×
119
  }
120

121
  return null;
1✔
122
}
123

124
/**
125
 * Converts a value to DateTime
126
 */
127
function valueToDateTime(value: Date | string | number): DateTime {
128
  let dt: DateTime | null = null;
35✔
129

130
  if (value instanceof Date) {
35✔
131
    dt = DateTime.fromJSDate(value);
29✔
132
  } else if (typeof value === "string") {
6✔
133
    dt = parseStringToDateTime(value);
3✔
134
  } else if (typeof value === "number") {
3✔
135
    dt = DateTime.fromMillis(value);
1✔
136
  } else {
137
    throw new Error("Unsupported type");
2✔
138
  }
139

140
  if (!dt?.isValid) {
33✔
141
    throw new Error("Invalid Date");
1✔
142
  }
143

144
  return dt;
32✔
145
}
146

147
/**
148
 * Validates timestamp range for Atlassian Forge compatibility
149
 */
150
function validateTimestampRange(dt: DateTime): void {
151
  const minDate = DateTime.fromSeconds(1);
7✔
152
  const maxDate = DateTime.fromMillis(2147483647 * 1000); // 2038-01-19 03:14:07.999 UTC
7✔
153

154
  if (dt < minDate) {
7✔
155
    throw new Error(
1✔
156
      "Atlassian Forge does not support zero or negative timestamps. Allowed range: from '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999'.",
157
    );
158
  }
159

160
  if (dt > maxDate) {
6✔
161
    throw new Error(
1✔
162
      "Atlassian Forge does not support timestamps beyond 2038-01-19 03:14:07.999999. Please use a smaller date within the supported range.",
163
    );
164
  }
165
}
166

167
/**
168
 * Helper function to validate and format a date-like value using Luxon DateTime.
169
 * @param value - Date object, ISO/RFC2822/SQL/HTTP string, or timestamp (number|string).
170
 * @param format - DateTime format string (Luxon format tokens).
171
 * @param isTimeStamp - Whether to validate timestamp range
172
 * @returns Formatted date string.
173
 * @throws Error if value cannot be parsed as a valid date.
174
 */
175
export function formatDateTime(
176
  value: Date | string | number,
177
  format: string,
178
  isTimeStamp: boolean,
179
): string {
180
  const dt = valueToDateTime(value);
35✔
181

182
  if (isTimeStamp) {
35✔
183
    validateTimestampRange(dt);
7✔
184
  }
185

186
  return dt.toFormat(format);
30✔
187
}
188

189
/**
190
 * Gets primary keys from the schema.
191
 * @template T - The type of the table schema
192
 * @param {T} table - The table schema
193
 * @returns {[string, AnyColumn][]} Array of primary key name and column pairs
194
 */
195
export function getPrimaryKeys<T extends AnyMySqlTable>(table: T): [string, AnyColumn][] {
196
  const { columns, primaryKeys } = getTableMetadata(table);
14✔
197

198
  // First try to find primary keys in columns
199
  const columnPrimaryKeys = Object.entries(columns).filter(([, column]) => column.primary) as [
38✔
200
    string,
201
    AnyColumn,
202
  ][];
203

204
  if (columnPrimaryKeys.length > 0) {
14✔
205
    return columnPrimaryKeys;
10✔
206
  }
207

208
  // If no primary keys found in columns, check primary key builders
209
  if (Array.isArray(primaryKeys) && primaryKeys.length > 0) {
4!
210
    // Collect all primary key columns from all primary key builders
211
    const primaryKeyColumns = new Set<[string, AnyColumn]>();
4✔
212

213
    for (const primaryKeyBuilder of primaryKeys) {
4✔
214
      // Get primary key columns from each builder
215
      for (const [name, column1] of Object.entries(columns).filter(([, column]) => {
4✔
216
        // @ts-ignore - PrimaryKeyBuilder has internal columns property
217
        return primaryKeyBuilder.columns.includes(column);
8✔
218
      })) {
219
        primaryKeyColumns.add([name, column1]);
4✔
220
      }
221
    }
222

223
    return Array.from(primaryKeyColumns);
4✔
224
  }
225

226
  return [];
×
227
}
228

229
/**
230
 * Processes foreign keys from foreignKeysSymbol
231
 */
232
function processForeignKeysFromSymbol(
233
  table: AnyMySqlTable,
234
  foreignKeysSymbol: symbol,
235
): ForeignKeyBuilder[] {
236
  const foreignKeys: ForeignKeyBuilder[] = [];
46✔
237
  // @ts-ignore
238
  const fkArray: any[] = table[foreignKeysSymbol];
46✔
239

240
  if (!fkArray) {
46!
NEW
241
    return foreignKeys;
×
242
  }
243

244
  for (const fk of fkArray) {
46✔
NEW
245
    if (fk.reference) {
×
NEW
246
      const item = fk.reference(fk);
×
NEW
247
      foreignKeys.push(item);
×
248
    }
249
  }
250

251
  return foreignKeys;
46✔
252
}
253

254
/**
255
 * Extracts config builders from config builder data
256
 */
257
function extractConfigBuilders(configBuilderData: any): any[] {
258
  if (Array.isArray(configBuilderData)) {
21!
259
    return configBuilderData;
21✔
260
  }
261

NEW
262
  return Object.values(configBuilderData).map((item) => (item as ConfigBuilderData).value ?? item);
×
263
}
264

265
/**
266
 * Checks if a builder is a ForeignKeyBuilder
267
 */
268
function isForeignKeyBuilder(builder: any): boolean {
269
  if (!builder?.constructor) {
21!
NEW
270
    return false;
×
271
  }
272

273
  const builderName = builder.constructor.name.toLowerCase();
21✔
274
  return builderName.includes("foreignkeybuilder");
21✔
275
}
276

277
/**
278
 * Processes foreign keys from extraSymbol
279
 */
280
function processForeignKeysFromExtra(
281
  table: AnyMySqlTable,
282
  extraSymbol: symbol,
283
): ForeignKeyBuilder[] {
284
  const foreignKeys: ForeignKeyBuilder[] = [];
46✔
285
  // @ts-ignore
286
  const extraConfigBuilder = table[extraSymbol];
46✔
287

288
  if (!extraConfigBuilder || typeof extraConfigBuilder !== "function") {
46✔
289
    return foreignKeys;
25✔
290
  }
291

292
  const configBuilderData = extraConfigBuilder(table);
21✔
293
  if (!configBuilderData) {
21!
NEW
294
    return foreignKeys;
×
295
  }
296

297
  const configBuilders = extractConfigBuilders(configBuilderData);
21✔
298

299
  for (const builder of configBuilders) {
21✔
300
    if (isForeignKeyBuilder(builder)) {
21!
NEW
301
      foreignKeys.push(builder);
×
302
    }
303
  }
304

305
  return foreignKeys;
21✔
306
}
307

308
/**
309
 * Processes foreign keys from both foreignKeysSymbol and extraSymbol
310
 * @param table - The table schema
311
 * @param foreignKeysSymbol - Symbol for foreign keys
312
 * @param extraSymbol - Symbol for extra configuration
313
 * @returns Array of foreign key builders
314
 */
315
function processForeignKeys(
316
  table: AnyMySqlTable,
317
  foreignKeysSymbol: symbol | undefined,
318
  extraSymbol: symbol | undefined,
319
): ForeignKeyBuilder[] {
320
  const foreignKeys: ForeignKeyBuilder[] = [];
46✔
321

322
  // Process foreign keys from foreignKeysSymbol
323
  if (foreignKeysSymbol) {
46!
324
    foreignKeys.push(...processForeignKeysFromSymbol(table, foreignKeysSymbol));
46✔
325
  }
326

327
  // Process foreign keys from extraSymbol
328
  if (extraSymbol) {
46!
329
    foreignKeys.push(...processForeignKeysFromExtra(table, extraSymbol));
46✔
330
  }
331

332
  return foreignKeys;
46✔
333
}
334

335
/**
336
 * Extracts table metadata from the schema.
337
 * @param {AnyMySqlTable} table - The table schema
338
 * @returns {MetadataInfo} Object containing table metadata
339
 */
340
export function getTableMetadata(table: AnyMySqlTable): MetadataInfo {
341
  const symbols = Object.getOwnPropertySymbols(table);
46✔
342
  const nameSymbol = symbols.find((s) => s.toString().includes("Name"));
46✔
343
  const columnsSymbol = symbols.find((s) => s.toString().includes("Columns"));
184✔
344
  const foreignKeysSymbol = symbols.find((s) => s.toString().includes("ForeignKeys)"));
460✔
345
  const extraSymbol = symbols.find((s) => s.toString().includes("ExtraConfigBuilder"));
414✔
346

347
  // Initialize builders arrays
348
  const builders = {
46✔
349
    indexes: [] as AnyIndexBuilder[],
350
    checks: [] as CheckBuilder[],
351
    foreignKeys: [] as ForeignKeyBuilder[],
352
    primaryKeys: [] as PrimaryKeyBuilder[],
353
    uniqueConstraints: [] as UniqueConstraintBuilder[],
354
    extras: [] as any[],
355
  };
356

357
  // Process foreign keys
358
  builders.foreignKeys = processForeignKeys(table, foreignKeysSymbol, extraSymbol);
46✔
359

360
  // Process extra configuration if available
361
  if (extraSymbol) {
46!
362
    // @ts-ignore
363
    const extraConfigBuilder = table[extraSymbol];
46✔
364
    if (extraConfigBuilder && typeof extraConfigBuilder === "function") {
46✔
365
      const configBuilderData = extraConfigBuilder(table);
21✔
366
      if (configBuilderData) {
21!
367
        // Convert configBuilderData to array if it's an object
368
        const configBuilders = Array.isArray(configBuilderData)
21!
369
          ? configBuilderData
370
          : Object.values(configBuilderData).map(
371
              (item) => (item as ConfigBuilderData).value ?? item,
×
372
            );
373

374
        // Process each builder
375
        for (const builder of configBuilders) {
21✔
376
          if (!builder?.constructor) continue;
21!
377

378
          const builderName = builder.constructor.name.toLowerCase();
21✔
379

380
          // Map builder types to their corresponding arrays
381
          const builderMap = {
21✔
382
            indexbuilder: builders.indexes,
383
            checkbuilder: builders.checks,
384
            primarykeybuilder: builders.primaryKeys,
385
            uniqueconstraintbuilder: builders.uniqueConstraints,
386
          };
387

388
          // Add builder to appropriate array if it matches any type
389
          for (const [type, array] of Object.entries(builderMap)) {
21✔
390
            if (builderName.includes(type)) {
63✔
391
              array.push(builder);
21✔
392
              break;
21✔
393
            }
394
          }
395

396
          // Always add to extras array
397
          builders.extras.push(builder);
21✔
398
        }
399
      }
400
    }
401
  }
402

403
  return {
46✔
404
    tableName: nameSymbol ? (table as any)[nameSymbol] : "",
46!
405
    columns: columnsSymbol ? ((table as any)[columnsSymbol] as Record<string, AnyColumn>) : {},
46!
406
    ...builders,
407
  };
408
}
409

410
/**
411
 * Generates SQL statements for dropping tables and/or their sequences.
412
 *
413
 * @param tables - List of table names to generate DROP statements for.
414
 * @param options - Configuration object:
415
 *   - sequence: whether to drop associated sequences (default: true)
416
 *   - table: whether to drop tables themselves (default: true)
417
 * @returns Array of SQL statements for dropping the specified objects
418
 */
419
export function generateDropTableStatements(
420
  tables: string[],
421
  options?: { sequence: boolean; table: boolean },
422
): string[] {
423
  const dropStatements: string[] = [];
7✔
424
  const validOptions = options ?? { sequence: true, table: true };
7✔
425
  if (!validOptions.sequence && !validOptions.table) {
7✔
426
    // eslint-disable-next-line no-console
427
    console.warn('No drop operations requested: both "table" and "sequence" options are false');
1✔
428
    return [];
1✔
429
  }
430
  for (const tableName of tables) {
6✔
431
    if (validOptions.table) {
8✔
432
      dropStatements.push(`DROP TABLE IF EXISTS \`${tableName}\`;`);
7✔
433
    }
434
    if (validOptions.sequence) {
8✔
435
      dropStatements.push(`DROP SEQUENCE IF EXISTS \`${tableName}\`;`);
7✔
436
    }
437
  }
438

439
  return dropStatements;
6✔
440
}
441

442
type AliasColumnMap = Record<string, AnyColumn>;
443

444
function mapSelectTableToAlias(
445
  table: MySqlTable,
446
  uniqPrefix: string,
447
  aliasMap: AliasColumnMap,
448
): any {
449
  const { columns, tableName } = getTableMetadata(table);
10✔
450
  const selectionsTableFields: Record<string, unknown> = {};
10✔
451
  for (const name of Object.keys(columns)) {
10✔
452
    const column = columns[name] as AnyColumn;
39✔
453
    const uniqName = `a_${uniqPrefix}_${tableName}_${column.name}`.toLowerCase();
39✔
454
    const fieldAlias = sql.raw(uniqName);
39✔
455
    selectionsTableFields[name] = sql`${column} as \`${fieldAlias}\``;
39✔
456
    aliasMap[uniqName] = column;
39✔
457
  }
458
  return selectionsTableFields;
10✔
459
}
460

461
function isDrizzleColumn(column: any): boolean {
462
  return column && typeof column === "object" && "table" in column;
144✔
463
}
464

465
export function mapSelectAllFieldsToAlias(
466
  selections: any,
467
  name: string,
468
  uniqName: string,
469
  fields: any,
470
  aliasMap: AliasColumnMap,
471
): any {
472
  if (isTable(fields)) {
154✔
473
    selections[name] = mapSelectTableToAlias(fields as MySqlTable, uniqName, aliasMap);
10✔
474
  } else if (isDrizzleColumn(fields)) {
144✔
475
    const column = fields as Column;
122✔
476
    const uniqAliasName = `a_${uniqName}_${column.name}`.toLowerCase();
122✔
477
    let aliasName = sql.raw(uniqAliasName);
122✔
478
    selections[name] = sql`${column} as \`${aliasName}\``;
122✔
479
    aliasMap[uniqAliasName] = column;
122✔
480
  } else if (isSQLWrapper(fields)) {
22✔
481
    selections[name] = fields;
10✔
482
  } else {
483
    const innerSelections: any = {};
12✔
484
    for (const [iname, ifields] of Object.entries(fields)) {
12✔
485
      mapSelectAllFieldsToAlias(innerSelections, iname, `${uniqName}_${iname}`, ifields, aliasMap);
24✔
486
    }
487
    selections[name] = innerSelections;
12✔
488
  }
489
  return selections;
154✔
490
}
491
export function mapSelectFieldsWithAlias<TSelection extends SelectedFields>(
492
  fields: TSelection,
493
): { selections: TSelection; aliasMap: AliasColumnMap } {
494
  if (!fields) {
53!
495
    throw new Error("fields is empty");
×
496
  }
497
  const aliasMap: AliasColumnMap = {};
53✔
498
  const selections: any = {};
53✔
499
  for (let i = 0; i < Object.entries(fields).length; i++) {
53✔
500
    const [name, fields1] = Object.entries(fields)[i];
130✔
501
    mapSelectAllFieldsToAlias(selections, name, name, fields1, aliasMap);
130✔
502
  }
503
  return { selections, aliasMap };
53✔
504
}
505

506
function getAliasFromDrizzleAlias(value: unknown): string | undefined {
507
  const isSQL =
508
    value !== null && typeof value === "object" && isSQLWrapper(value) && "queryChunks" in value;
230✔
509
  if (isSQL) {
230✔
510
    const sql = value as SQL;
187✔
511
    const queryChunks = sql.queryChunks;
187✔
512
    if (queryChunks.length > 3) {
187✔
513
      const aliasNameChunk = queryChunks[queryChunks.length - 2];
169✔
514
      if (isSQLWrapper(aliasNameChunk) && "queryChunks" in aliasNameChunk) {
169!
515
        const aliasNameChunkSql = aliasNameChunk as SQL;
169✔
516
        if (aliasNameChunkSql.queryChunks?.length === 1 && aliasNameChunkSql.queryChunks[0]) {
169!
517
          const queryChunksStringChunc = aliasNameChunkSql.queryChunks[0];
169✔
518
          if ("value" in queryChunksStringChunc) {
169!
519
            const values = (queryChunksStringChunc as StringChunk).value;
169✔
520
            if (values && values.length === 1) {
169!
521
              return values[0];
169✔
522
            }
523
          }
524
        }
525
      }
526
    }
527
  }
528
  return undefined;
61✔
529
}
530

531
function transformValue(
532
  value: unknown,
533
  alias: string,
534
  aliasMap: Record<string, AnyColumn>,
535
): unknown {
536
  const column = aliasMap[alias];
169✔
537
  if (!column) return value;
169!
538

539
  let customColumn = column as MySqlCustomColumn<any>;
169✔
540
  // @ts-ignore
541
  const fromDriver = customColumn?.mapFrom;
169✔
542
  if (fromDriver && value !== null && value !== undefined) {
169✔
543
    return fromDriver(value);
26✔
544
  }
545
  return value;
143✔
546
}
547

548
function transformObject(
549
  obj: Record<string, unknown>,
550
  selections: Record<string, unknown>,
551
  aliasMap: Record<string, AnyColumn>,
552
): Record<string, unknown> {
553
  const result: Record<string, unknown> = {};
84✔
554

555
  for (const [key, value] of Object.entries(obj)) {
84✔
556
    const selection = selections[key];
230✔
557
    const alias = getAliasFromDrizzleAlias(selection);
230✔
558
    if (alias && aliasMap[alias]) {
230✔
559
      result[key] = transformValue(value, alias, aliasMap);
169✔
560
    } else if (selection && typeof selection === "object" && !isSQLWrapper(selection)) {
61✔
561
      result[key] = transformObject(
43✔
562
        value as Record<string, unknown>,
563
        selection as Record<string, unknown>,
564
        aliasMap,
565
      );
566
    } else {
567
      result[key] = value;
18✔
568
    }
569
  }
570

571
  return result;
84✔
572
}
573

574
export function applyFromDriverTransform<T, TSelection>(
575
  rows: T[],
576
  selections: TSelection,
577
  aliasMap: Record<string, AnyColumn>,
578
): T[] {
579
  return rows.map((row) => {
33✔
580
    const transformed = transformObject(
41✔
581
      row as Record<string, unknown>,
582
      selections as Record<string, unknown>,
583
      aliasMap,
584
    ) as Record<string, unknown>;
585

586
    return processNullBranches(transformed) as unknown as T;
41✔
587
  });
588
}
589

590
function processNullBranches(obj: Record<string, unknown>): Record<string, unknown> | null {
591
  if (obj === null || typeof obj !== "object") {
92!
592
    return obj;
×
593
  }
594

595
  // Skip built-in objects like Date, Array, etc.
596
  if (obj.constructor && obj.constructor.name !== "Object") {
92✔
597
    return obj;
8✔
598
  }
599

600
  const result: Record<string, unknown> = {};
84✔
601
  let allNull = true;
84✔
602

603
  for (const [key, value] of Object.entries(obj)) {
84✔
604
    if (value === null || value === undefined) {
230✔
605
      result[key] = null;
5✔
606
      continue;
5✔
607
    }
608

609
    if (typeof value === "object") {
225✔
610
      const processed = processNullBranches(value as Record<string, unknown>);
51✔
611
      result[key] = processed;
51✔
612
      if (processed !== null) {
51✔
613
        allNull = false;
50✔
614
      }
615
    } else {
616
      result[key] = value;
174✔
617
      allNull = false;
174✔
618
    }
619
  }
620

621
  return allNull ? null : result;
84✔
622
}
623

624
export function formatLimitOffset(limitOrOffset: number): number {
625
  if (typeof limitOrOffset !== "number" || Number.isNaN(limitOrOffset)) {
5✔
626
    throw new Error("limitOrOffset must be a valid number");
2✔
627
  }
628
  return sql.raw(`${limitOrOffset}`) as unknown as number;
3✔
629
}
630

631
export function nextVal(sequenceName: string): number {
632
  return sql.raw(`NEXTVAL(${sequenceName})`) as unknown as number;
3✔
633
}
634

635
/**
636
 * Analyzes and prints query performance data from CLUSTER_STATEMENTS_SUMMARY table.
637
 *
638
 * This function queries the CLUSTER_STATEMENTS_SUMMARY table to find queries that were executed
639
 * within the specified time window and prints detailed performance information including:
640
 * - SQL query text
641
 * - Memory usage (average and max in MB)
642
 * - Execution time (average in ms)
643
 * - Number of executions
644
 * - Execution plan
645
 *
646
 * @param forgeSQLORM - The ForgeSQL operation instance for database access
647
 * @param timeDiffMs - Time window in milliseconds to look back for queries (e.g., 1500 for last 1.5 seconds)
648
 * @param timeout - Optional timeout in milliseconds for the query execution (defaults to 1500ms)
649
 *
650
 * @example
651
 * ```typescript
652
 * // Analyze queries from the last 2 seconds
653
 * await printQueriesWithPlan(forgeSQLORM, 2000);
654
 *
655
 * // Analyze queries with custom timeout
656
 * await printQueriesWithPlan(forgeSQLORM, 1000, 3000);
657
 * ```
658
 *
659
 * @throws Does not throw - errors are logged to console.debug instead
660
 */
661
export async function printQueriesWithPlan(
662
  forgeSQLORM: ForgeSqlOperation,
663
  timeDiffMs: number,
664
  timeout?: number,
665
) {
666
  try {
×
667
    const statementsTable = clusterStatementsSummary;
×
668
    const timeoutMs = timeout ?? 3000;
×
669
    const results = await withTimeout(
×
670
      forgeSQLORM
671
        .getDrizzleQueryBuilder()
672
        .select({
673
          digestText: withTidbHint(statementsTable.digestText),
674
          avgLatency: statementsTable.avgLatency,
675
          avgMem: statementsTable.avgMem,
676
          execCount: statementsTable.execCount,
677
          plan: statementsTable.plan,
678
          stmtType: statementsTable.stmtType,
679
        })
680
        .from(statementsTable)
681
        .where(
682
          and(
683
            isNotNull(statementsTable.digest),
684
            not(ilike(statementsTable.digestText, "%information_schema%")),
685
            notInArray(statementsTable.stmtType, [
686
              "Use",
687
              "Set",
688
              "Show",
689
              "Commit",
690
              "Rollback",
691
              "Begin",
692
            ]),
693
            gte(
694
              statementsTable.lastSeen,
695
              sql`DATE_SUB
696
                            (NOW(), INTERVAL
697
                            ${timeDiffMs * 1000}
698
                            MICROSECOND
699
                            )`,
700
            ),
701
          ),
702
        ),
703
      `Timeout ${timeoutMs}ms in printQueriesWithPlan - transient timeouts are usually fine; repeated timeouts mean this diagnostic query is consistently slow and should be investigated`,
704
      timeoutMs + 200,
705
    );
706

707
    for (const result of results) {
×
708
      // Average execution time (convert from nanoseconds to milliseconds)
709
      const avgTimeMs = Number(result.avgLatency) / 1_000_000;
×
710
      const avgMemMB = Number(result.avgMem) / 1_000_000;
×
711

712
      // 1. Query info: SQL, memory, time, executions
713
      // eslint-disable-next-line no-console
714
      console.warn(
×
715
        `SQL: ${result.digestText} | Memory: ${avgMemMB.toFixed(2)} MB | Time: ${avgTimeMs.toFixed(2)} ms | stmtType: ${result.stmtType} | Executions: ${result.execCount}\n Plan:${result.plan}`,
716
      );
717
    }
718
  } catch (error) {
719
    // eslint-disable-next-line no-console
720
    console.debug(
×
721
      `Error occurred while retrieving query execution plan: ${error instanceof Error ? error.message : "Unknown error"}. Try again after some time`,
×
722
      error,
723
    );
724
  }
725
}
726

727
const SESSION_ALIAS_NAME_ORM = "orm";
7✔
728

729
/**
730
 * Analyzes and logs slow queries from the last specified number of hours.
731
 *
732
 * This function queries the slow query system table to find queries that were executed
733
 * within the specified time window and logs detailed performance information including:
734
 * - SQL query text
735
 * - Maximum memory usage (in MB)
736
 * - Query execution time (in ms)
737
 * - Execution count
738
 * - Execution plan
739
 *
740
 * @param forgeSQLORM - The ForgeSQL operation instance for database access
741
 * @param hours - Number of hours to look back for slow queries (e.g., 1 for last hour, 24 for last day)
742
 * @param timeout - Optional timeout in milliseconds for the query execution (defaults to 1500ms)
743
 *
744
 * @example
745
 * ```typescript
746
 * // Analyze slow queries from the last hour
747
 * await slowQueryPerHours(forgeSQLORM, 1);
748
 *
749
 * // Analyze slow queries from the last 24 hours with custom timeout
750
 * await slowQueryPerHours(forgeSQLORM, 24, 3000);
751
 *
752
 * // Analyze slow queries from the last 6 hours
753
 * await slowQueryPerHours(forgeSQLORM, 6);
754
 * ```
755
 *
756
 * @throws Does not throw - errors are logged to console.debug instead
757
 */
758
export async function slowQueryPerHours(
759
  forgeSQLORM: ForgeSqlOperation,
760
  hours: number,
761
  timeout?: number,
762
) {
763
  try {
×
764
    const timeoutMs = timeout ?? 1500;
×
765
    const results = await withTimeout(
×
766
      forgeSQLORM
767
        .getDrizzleQueryBuilder()
768
        .select({
769
          query: withTidbHint(slowQuery.query),
770
          queryTime: slowQuery.queryTime,
771
          memMax: slowQuery.memMax,
772
          plan: slowQuery.plan,
773
        })
774
        .from(slowQuery)
775
        .where(
776
          and(
777
            isNotNull(slowQuery.digest),
778
            ne(slowQuery.sessionAlias, SESSION_ALIAS_NAME_ORM),
779
            gte(
780
              slowQuery.time,
781
              sql`DATE_SUB
782
                            (NOW(), INTERVAL
783
                            ${hours}
784
                            HOUR
785
                            )`,
786
            ),
787
          ),
788
        ),
789
      `Timeout ${timeoutMs}ms in slowQueryPerHours - transient timeouts are usually fine; repeated timeouts mean this diagnostic query is consistently slow and should be investigated`,
790
      timeoutMs,
791
    );
792
    const response: string[] = [];
×
793
    for (const result of results) {
×
794
      // Convert memory from bytes to MB and handle null values
795
      const memMaxMB = result.memMax ? Number(result.memMax) / 1_000_000 : 0;
×
796

797
      const message = `Found SlowQuery SQL: ${result.query} | Memory: ${memMaxMB.toFixed(2)} MB | Time: ${result.queryTime} ms\n Plan:${result.plan}`;
×
798
      response.push(message);
×
799
      // 1. Query info: SQL, memory, time, executions
800
      // eslint-disable-next-line no-console
801
      console.warn(message);
×
802
    }
803
    return response;
×
804
  } catch (error) {
805
    // eslint-disable-next-line no-console
806
    console.debug(
×
807
      `Error occurred while retrieving query execution plan: ${error instanceof Error ? error.message : "Unknown error"}. Try again after some time`,
×
808
      error,
809
    );
810
    return [
×
811
      `Error occurred while retrieving query execution plan: ${error instanceof Error ? error.message : "Unknown error"}`,
×
812
    ];
813
  }
814
}
815

816
/**
817
 * Executes a promise with a timeout.
818
 *
819
 * @param promise - The promise to execute
820
 * @param timeoutMs - Timeout in milliseconds
821
 * @returns Promise that resolves with the result or rejects on timeout
822
 * @throws {Error} When the operation times out
823
 */
824
export async function withTimeout<T>(
825
  promise: Promise<T>,
826
  message: string,
827
  timeoutMs: number,
828
): Promise<T> {
829
  let timeoutId: ReturnType<typeof setTimeout> | undefined;
830

831
  const timeoutPromise = new Promise<never>((_, reject) => {
100✔
832
    timeoutId = setTimeout(() => {
100✔
833
      reject(new Error(message));
1✔
834
    }, timeoutMs);
835
  });
836

837
  try {
100✔
838
    return await Promise.race([promise, timeoutPromise]);
100✔
839
  } finally {
840
    if (timeoutId) {
100!
841
      clearTimeout(timeoutId);
100✔
842
    }
843
  }
844
}
845

846
export function withTidbHint<
847
  TDataType extends ColumnDataType,
848
  TPartial extends Partial<ColumnBaseConfig<TDataType, string>>,
849
>(column: AnyMySqlColumn<TPartial>): AnyMySqlColumn<TPartial> {
850
  // We lie a bit to TypeScript here: at runtime this is a new SQL fragment,
851
  // but returning TExpr keeps the column type info in downstream inference.
852
  return sql`/*+ SET_VAR(tidb_session_alias=${sql.raw(`${SESSION_ALIAS_NAME_ORM}`)}) */ ${column}` as unknown as AnyMySqlColumn<TPartial>;
2✔
853
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc