forge-sql-orm 2.1.3 → 2.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { isTable, sql, eq, and, getTableColumns } from "drizzle-orm";
1
+ import { isTable, sql, eq, and, getTableColumns, gte, desc } from "drizzle-orm";
2
2
  import { DateTime } from "luxon";
3
3
  import { isSQLWrapper } from "drizzle-orm/sql/sql";
4
4
  import { AsyncLocalStorage } from "node:async_hooks";
@@ -7,7 +7,7 @@ import * as crypto from "crypto";
7
7
  import { kvs, WhereConditions, Filter, FilterConditions } from "@forge/kvs";
8
8
  import { sql as sql$1, migrationRunner } from "@forge/sql";
9
9
  import { drizzle } from "drizzle-orm/mysql-proxy";
10
- import { customType, mysqlTable, timestamp, varchar, bigint } from "drizzle-orm/mysql-core";
10
+ import { customType, mysqlTable, timestamp, varchar, bigint, mysqlSchema, longtext, double, boolean, text, int, unionAll } from "drizzle-orm/mysql-core";
11
11
  const parseDateTime = (value, format) => {
12
12
  let result;
13
13
  if (value instanceof Date) {
@@ -2623,6 +2623,435 @@ const migrations = mysqlTable("__migrations", {
2623
2623
  name: varchar("name", { length: 255 }).notNull(),
2624
2624
  migratedAt: timestamp("migratedAt").defaultNow().notNull()
2625
2625
  });
2626
+ const informationSchema = mysqlSchema("information_schema");
2627
+ const slowQuery = informationSchema.table("SLOW_QUERY", {
2628
+ time: timestamp("Time", { fsp: 6, mode: "string" }).notNull(),
2629
+ // Timestamp when the slow query was recorded
2630
+ txnStartTs: bigint("Txn_start_ts", { mode: "bigint", unsigned: true }),
2631
+ // Transaction start timestamp (TSO)
2632
+ user: varchar("User", { length: 64 }),
2633
+ // User executing the query
2634
+ host: varchar("Host", { length: 64 }),
2635
+ // Host from which the query originated
2636
+ connId: bigint("Conn_ID", { mode: "bigint", unsigned: true }),
2637
+ // Connection ID
2638
+ sessionAlias: varchar("Session_alias", { length: 64 }),
2639
+ // Session alias
2640
+ execRetryCount: bigint("Exec_retry_count", { mode: "bigint", unsigned: true }),
2641
+ // Number of retries during execution
2642
+ execRetryTime: double("Exec_retry_time"),
2643
+ // Time spent in retries
2644
+ queryTime: double("Query_time"),
2645
+ // Total execution time
2646
+ parseTime: double("Parse_time"),
2647
+ // Time spent parsing SQL
2648
+ compileTime: double("Compile_time"),
2649
+ // Time spent compiling query plan
2650
+ rewriteTime: double("Rewrite_time"),
2651
+ // Time spent rewriting query
2652
+ preprocSubqueries: bigint("Preproc_subqueries", { mode: "bigint", unsigned: true }),
2653
+ // Number of subqueries preprocessed
2654
+ preprocSubqueriesTime: double("Preproc_subqueries_time"),
2655
+ // Time spent preprocessing subqueries
2656
+ optimizeTime: double("Optimize_time"),
2657
+ // Time spent in optimizer
2658
+ waitTs: double("Wait_TS"),
2659
+ // Wait time for getting TSO
2660
+ prewriteTime: double("Prewrite_time"),
2661
+ // Time spent in prewrite phase
2662
+ waitPrewriteBinlogTime: double("Wait_prewrite_binlog_time"),
2663
+ // Time waiting for binlog prewrite
2664
+ commitTime: double("Commit_time"),
2665
+ // Commit duration
2666
+ getCommitTsTime: double("Get_commit_ts_time"),
2667
+ // Time waiting for commit TSO
2668
+ commitBackoffTime: double("Commit_backoff_time"),
2669
+ // Backoff time during commit
2670
+ backoffTypes: varchar("Backoff_types", { length: 64 }),
2671
+ // Types of backoff occurred
2672
+ resolveLockTime: double("Resolve_lock_time"),
2673
+ // Time resolving locks
2674
+ localLatchWaitTime: double("Local_latch_wait_time"),
2675
+ // Time waiting on local latch
2676
+ writeKeys: bigint("Write_keys", { mode: "bigint" }),
2677
+ // Number of keys written
2678
+ writeSize: bigint("Write_size", { mode: "bigint" }),
2679
+ // Amount of data written
2680
+ prewriteRegion: bigint("Prewrite_region", { mode: "bigint" }),
2681
+ // Regions involved in prewrite
2682
+ txnRetry: bigint("Txn_retry", { mode: "bigint" }),
2683
+ // Transaction retry count
2684
+ copTime: double("Cop_time"),
2685
+ // Time spent in coprocessor
2686
+ processTime: double("Process_time"),
2687
+ // Processing time
2688
+ waitTime: double("Wait_time"),
2689
+ // Wait time in TiKV
2690
+ backoffTime: double("Backoff_time"),
2691
+ // Backoff wait time
2692
+ lockKeysTime: double("LockKeys_time"),
2693
+ // Time spent waiting for locks
2694
+ requestCount: bigint("Request_count", { mode: "bigint", unsigned: true }),
2695
+ // Total number of requests
2696
+ totalKeys: bigint("Total_keys", { mode: "bigint", unsigned: true }),
2697
+ // Total keys scanned
2698
+ processKeys: bigint("Process_keys", { mode: "bigint", unsigned: true }),
2699
+ // Keys processed
2700
+ rocksdbDeleteSkippedCount: bigint("Rocksdb_delete_skipped_count", {
2701
+ mode: "bigint",
2702
+ unsigned: true
2703
+ }),
2704
+ // RocksDB delete skips
2705
+ rocksdbKeySkippedCount: bigint("Rocksdb_key_skipped_count", { mode: "bigint", unsigned: true }),
2706
+ // RocksDB key skips
2707
+ rocksdbBlockCacheHitCount: bigint("Rocksdb_block_cache_hit_count", {
2708
+ mode: "bigint",
2709
+ unsigned: true
2710
+ }),
2711
+ // RocksDB block cache hits
2712
+ rocksdbBlockReadCount: bigint("Rocksdb_block_read_count", { mode: "bigint", unsigned: true }),
2713
+ // RocksDB block reads
2714
+ rocksdbBlockReadByte: bigint("Rocksdb_block_read_byte", { mode: "bigint", unsigned: true }),
2715
+ // RocksDB block read bytes
2716
+ db: varchar("DB", { length: 64 }),
2717
+ // Database name
2718
+ indexNames: varchar("Index_names", { length: 100 }),
2719
+ // Indexes used
2720
+ isInternal: boolean("Is_internal"),
2721
+ // Whether the query is internal
2722
+ digest: varchar("Digest", { length: 64 }),
2723
+ // SQL digest hash
2724
+ stats: varchar("Stats", { length: 512 }),
2725
+ // Stats used during planning
2726
+ copProcAvg: double("Cop_proc_avg"),
2727
+ // Coprocessor average processing time
2728
+ copProcP90: double("Cop_proc_p90"),
2729
+ // Coprocessor 90th percentile processing time
2730
+ copProcMax: double("Cop_proc_max"),
2731
+ // Coprocessor max processing time
2732
+ copProcAddr: varchar("Cop_proc_addr", { length: 64 }),
2733
+ // Coprocessor address for processing
2734
+ copWaitAvg: double("Cop_wait_avg"),
2735
+ // Coprocessor average wait time
2736
+ copWaitP90: double("Cop_wait_p90"),
2737
+ // Coprocessor 90th percentile wait time
2738
+ copWaitMax: double("Cop_wait_max"),
2739
+ // Coprocessor max wait time
2740
+ copWaitAddr: varchar("Cop_wait_addr", { length: 64 }),
2741
+ // Coprocessor address for wait
2742
+ memMax: bigint("Mem_max", { mode: "bigint" }),
2743
+ // Max memory usage
2744
+ diskMax: bigint("Disk_max", { mode: "bigint" }),
2745
+ // Max disk usage
2746
+ kvTotal: double("KV_total"),
2747
+ // Total KV request time
2748
+ pdTotal: double("PD_total"),
2749
+ // Total PD request time
2750
+ backoffTotal: double("Backoff_total"),
2751
+ // Total backoff time
2752
+ writeSqlResponseTotal: double("Write_sql_response_total"),
2753
+ // SQL response write time
2754
+ resultRows: bigint("Result_rows", { mode: "bigint" }),
2755
+ // Rows returned
2756
+ warnings: longtext("Warnings"),
2757
+ // Warnings during execution
2758
+ backoffDetail: varchar("Backoff_Detail", { length: 4096 }),
2759
+ // Detailed backoff info
2760
+ prepared: boolean("Prepared"),
2761
+ // Whether query was prepared
2762
+ succ: boolean("Succ"),
2763
+ // Success flag
2764
+ isExplicitTxn: boolean("IsExplicitTxn"),
2765
+ // Whether explicit transaction
2766
+ isWriteCacheTable: boolean("IsWriteCacheTable"),
2767
+ // Whether wrote to cache table
2768
+ planFromCache: boolean("Plan_from_cache"),
2769
+ // Plan was from cache
2770
+ planFromBinding: boolean("Plan_from_binding"),
2771
+ // Plan was from binding
2772
+ hasMoreResults: boolean("Has_more_results"),
2773
+ // Query returned multiple results
2774
+ resourceGroup: varchar("Resource_group", { length: 64 }),
2775
+ // Resource group name
2776
+ requestUnitRead: double("Request_unit_read"),
2777
+ // RU consumed for read
2778
+ requestUnitWrite: double("Request_unit_write"),
2779
+ // RU consumed for write
2780
+ timeQueuedByRc: double("Time_queued_by_rc"),
2781
+ // Time queued by resource control
2782
+ tidbCpuTime: double("Tidb_cpu_time"),
2783
+ // TiDB CPU time
2784
+ tikvCpuTime: double("Tikv_cpu_time"),
2785
+ // TiKV CPU time
2786
+ plan: longtext("Plan"),
2787
+ // Query execution plan
2788
+ planDigest: varchar("Plan_digest", { length: 128 }),
2789
+ // Plan digest hash
2790
+ binaryPlan: longtext("Binary_plan"),
2791
+ // Binary execution plan
2792
+ prevStmt: longtext("Prev_stmt"),
2793
+ // Previous statement in session
2794
+ query: longtext("Query")
2795
+ // Original SQL query
2796
+ });
2797
+ const createClusterStatementsSummarySchema = () => ({
2798
+ instance: varchar("INSTANCE", { length: 64 }),
2799
+ // TiDB/TiKV instance address
2800
+ summaryBeginTime: timestamp("SUMMARY_BEGIN_TIME", { mode: "string" }).notNull(),
2801
+ // Begin time of this summary window
2802
+ summaryEndTime: timestamp("SUMMARY_END_TIME", { mode: "string" }).notNull(),
2803
+ // End time of this summary window
2804
+ stmtType: varchar("STMT_TYPE", { length: 64 }).notNull(),
2805
+ // Statement type (e.g., Select/Insert/Update)
2806
+ schemaName: varchar("SCHEMA_NAME", { length: 64 }),
2807
+ // Current schema name
2808
+ digest: varchar("DIGEST", { length: 64 }),
2809
+ // SQL digest (normalized hash)
2810
+ digestText: text("DIGEST_TEXT").notNull(),
2811
+ // Normalized SQL text
2812
+ tableNames: text("TABLE_NAMES"),
2813
+ // Involved table names
2814
+ indexNames: text("INDEX_NAMES"),
2815
+ // Used index names
2816
+ sampleUser: varchar("SAMPLE_USER", { length: 64 }),
2817
+ // Sampled user who executed the statements
2818
+ execCount: bigint("EXEC_COUNT", { mode: "bigint", unsigned: true }).notNull(),
2819
+ // Total executions
2820
+ sumErrors: int("SUM_ERRORS", { unsigned: true }).notNull(),
2821
+ // Sum of errors
2822
+ sumWarnings: int("SUM_WARNINGS", { unsigned: true }).notNull(),
2823
+ // Sum of warnings
2824
+ sumLatency: bigint("SUM_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
2825
+ // Sum of latency (ns)
2826
+ maxLatency: bigint("MAX_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
2827
+ // Max latency (ns)
2828
+ minLatency: bigint("MIN_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
2829
+ // Min latency (ns)
2830
+ avgLatency: bigint("AVG_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
2831
+ // Avg latency (ns)
2832
+ avgParseLatency: bigint("AVG_PARSE_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
2833
+ // Avg parse time (ns)
2834
+ maxParseLatency: bigint("MAX_PARSE_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
2835
+ // Max parse time (ns)
2836
+ avgCompileLatency: bigint("AVG_COMPILE_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
2837
+ // Avg compile time (ns)
2838
+ maxCompileLatency: bigint("MAX_COMPILE_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
2839
+ // Max compile time (ns)
2840
+ sumCopTaskNum: bigint("SUM_COP_TASK_NUM", { mode: "bigint", unsigned: true }).notNull(),
2841
+ // Total number of cop tasks
2842
+ maxCopProcessTime: bigint("MAX_COP_PROCESS_TIME", { mode: "bigint", unsigned: true }).notNull(),
2843
+ // Max TiKV coprocessor processing time (ns)
2844
+ maxCopProcessAddress: varchar("MAX_COP_PROCESS_ADDRESS", { length: 256 }),
2845
+ // Address of cop task with max processing time
2846
+ maxCopWaitTime: bigint("MAX_COP_WAIT_TIME", { mode: "bigint", unsigned: true }).notNull(),
2847
+ // Max TiKV coprocessor wait time (ns)
2848
+ maxCopWaitAddress: varchar("MAX_COP_WAIT_ADDRESS", { length: 256 }),
2849
+ // Address of cop task with max wait time
2850
+ avgProcessTime: bigint("AVG_PROCESS_TIME", { mode: "bigint", unsigned: true }).notNull(),
2851
+ // Avg TiKV processing time (ns)
2852
+ maxProcessTime: bigint("MAX_PROCESS_TIME", { mode: "bigint", unsigned: true }).notNull(),
2853
+ // Max TiKV processing time (ns)
2854
+ avgWaitTime: bigint("AVG_WAIT_TIME", { mode: "bigint", unsigned: true }).notNull(),
2855
+ // Avg TiKV wait time (ns)
2856
+ maxWaitTime: bigint("MAX_WAIT_TIME", { mode: "bigint", unsigned: true }).notNull(),
2857
+ // Max TiKV wait time (ns)
2858
+ avgBackoffTime: bigint("AVG_BACKOFF_TIME", { mode: "bigint", unsigned: true }).notNull(),
2859
+ // Avg backoff time before retry (ns)
2860
+ maxBackoffTime: bigint("MAX_BACKOFF_TIME", { mode: "bigint", unsigned: true }).notNull(),
2861
+ // Max backoff time before retry (ns)
2862
+ avgTotalKeys: bigint("AVG_TOTAL_KEYS", { mode: "bigint", unsigned: true }).notNull(),
2863
+ // Avg scanned keys
2864
+ maxTotalKeys: bigint("MAX_TOTAL_KEYS", { mode: "bigint", unsigned: true }).notNull(),
2865
+ // Max scanned keys
2866
+ avgProcessedKeys: bigint("AVG_PROCESSED_KEYS", { mode: "bigint", unsigned: true }).notNull(),
2867
+ // Avg processed keys
2868
+ maxProcessedKeys: bigint("MAX_PROCESSED_KEYS", { mode: "bigint", unsigned: true }).notNull(),
2869
+ // Max processed keys
2870
+ avgRocksdbDeleteSkippedCount: double("AVG_ROCKSDB_DELETE_SKIPPED_COUNT").notNull(),
2871
+ // Avg RocksDB deletes skipped
2872
+ maxRocksdbDeleteSkippedCount: int("MAX_ROCKSDB_DELETE_SKIPPED_COUNT", {
2873
+ unsigned: true
2874
+ }).notNull(),
2875
+ // Max RocksDB deletes skipped
2876
+ avgRocksdbKeySkippedCount: double("AVG_ROCKSDB_KEY_SKIPPED_COUNT").notNull(),
2877
+ // Avg RocksDB keys skipped
2878
+ maxRocksdbKeySkippedCount: int("MAX_ROCKSDB_KEY_SKIPPED_COUNT", { unsigned: true }).notNull(),
2879
+ // Max RocksDB keys skipped
2880
+ avgRocksdbBlockCacheHitCount: double("AVG_ROCKSDB_BLOCK_CACHE_HIT_COUNT").notNull(),
2881
+ // Avg RocksDB block cache hits
2882
+ maxRocksdbBlockCacheHitCount: int("MAX_ROCKSDB_BLOCK_CACHE_HIT_COUNT", {
2883
+ unsigned: true
2884
+ }).notNull(),
2885
+ // Max RocksDB block cache hits
2886
+ avgRocksdbBlockReadCount: double("AVG_ROCKSDB_BLOCK_READ_COUNT").notNull(),
2887
+ // Avg RocksDB block reads
2888
+ maxRocksdbBlockReadCount: int("MAX_ROCKSDB_BLOCK_READ_COUNT", { unsigned: true }).notNull(),
2889
+ // Max RocksDB block reads
2890
+ avgRocksdbBlockReadByte: double("AVG_ROCKSDB_BLOCK_READ_BYTE").notNull(),
2891
+ // Avg RocksDB block read bytes
2892
+ maxRocksdbBlockReadByte: int("MAX_ROCKSDB_BLOCK_READ_BYTE", { unsigned: true }).notNull(),
2893
+ // Max RocksDB block read bytes
2894
+ avgPrewriteTime: bigint("AVG_PREWRITE_TIME", { mode: "bigint", unsigned: true }).notNull(),
2895
+ // Avg prewrite phase time (ns)
2896
+ maxPrewriteTime: bigint("MAX_PREWRITE_TIME", { mode: "bigint", unsigned: true }).notNull(),
2897
+ // Max prewrite phase time (ns)
2898
+ avgCommitTime: bigint("AVG_COMMIT_TIME", { mode: "bigint", unsigned: true }).notNull(),
2899
+ // Avg commit phase time (ns)
2900
+ maxCommitTime: bigint("MAX_COMMIT_TIME", { mode: "bigint", unsigned: true }).notNull(),
2901
+ // Max commit phase time (ns)
2902
+ avgGetCommitTsTime: bigint("AVG_GET_COMMIT_TS_TIME", {
2903
+ mode: "bigint",
2904
+ unsigned: true
2905
+ }).notNull(),
2906
+ // Avg get commit_ts time (ns)
2907
+ maxGetCommitTsTime: bigint("MAX_GET_COMMIT_TS_TIME", {
2908
+ mode: "bigint",
2909
+ unsigned: true
2910
+ }).notNull(),
2911
+ // Max get commit_ts time (ns)
2912
+ avgCommitBackoffTime: bigint("AVG_COMMIT_BACKOFF_TIME", {
2913
+ mode: "bigint",
2914
+ unsigned: true
2915
+ }).notNull(),
2916
+ // Avg backoff during commit (ns)
2917
+ maxCommitBackoffTime: bigint("MAX_COMMIT_BACKOFF_TIME", {
2918
+ mode: "bigint",
2919
+ unsigned: true
2920
+ }).notNull(),
2921
+ // Max backoff during commit (ns)
2922
+ avgResolveLockTime: bigint("AVG_RESOLVE_LOCK_TIME", {
2923
+ mode: "bigint",
2924
+ unsigned: true
2925
+ }).notNull(),
2926
+ // Avg resolve lock time (ns)
2927
+ maxResolveLockTime: bigint("MAX_RESOLVE_LOCK_TIME", {
2928
+ mode: "bigint",
2929
+ unsigned: true
2930
+ }).notNull(),
2931
+ // Max resolve lock time (ns)
2932
+ avgLocalLatchWaitTime: bigint("AVG_LOCAL_LATCH_WAIT_TIME", {
2933
+ mode: "bigint",
2934
+ unsigned: true
2935
+ }).notNull(),
2936
+ // Avg local latch wait (ns)
2937
+ maxLocalLatchWaitTime: bigint("MAX_LOCAL_LATCH_WAIT_TIME", {
2938
+ mode: "bigint",
2939
+ unsigned: true
2940
+ }).notNull(),
2941
+ // Max local latch wait (ns)
2942
+ avgWriteKeys: double("AVG_WRITE_KEYS").notNull(),
2943
+ // Avg number of written keys
2944
+ maxWriteKeys: bigint("MAX_WRITE_KEYS", { mode: "bigint", unsigned: true }).notNull(),
2945
+ // Max written keys
2946
+ avgWriteSize: double("AVG_WRITE_SIZE").notNull(),
2947
+ // Avg written bytes
2948
+ maxWriteSize: bigint("MAX_WRITE_SIZE", { mode: "bigint", unsigned: true }).notNull(),
2949
+ // Max written bytes
2950
+ avgPrewriteRegions: double("AVG_PREWRITE_REGIONS").notNull(),
2951
+ // Avg regions in prewrite
2952
+ maxPrewriteRegions: int("MAX_PREWRITE_REGIONS", { unsigned: true }).notNull(),
2953
+ // Max regions in prewrite
2954
+ avgTxnRetry: double("AVG_TXN_RETRY").notNull(),
2955
+ // Avg transaction retry count
2956
+ maxTxnRetry: int("MAX_TXN_RETRY", { unsigned: true }).notNull(),
2957
+ // Max transaction retry count
2958
+ sumExecRetry: bigint("SUM_EXEC_RETRY", { mode: "bigint", unsigned: true }).notNull(),
2959
+ // Sum of execution retries (pessimistic)
2960
+ sumExecRetryTime: bigint("SUM_EXEC_RETRY_TIME", { mode: "bigint", unsigned: true }).notNull(),
2961
+ // Sum time of execution retries (ns)
2962
+ sumBackoffTimes: bigint("SUM_BACKOFF_TIMES", { mode: "bigint", unsigned: true }).notNull(),
2963
+ // Sum of backoff retries
2964
+ backoffTypes: varchar("BACKOFF_TYPES", { length: 1024 }),
2965
+ // Backoff types with counts
2966
+ avgMem: bigint("AVG_MEM", { mode: "bigint", unsigned: true }).notNull(),
2967
+ // Avg memory used (bytes)
2968
+ maxMem: bigint("MAX_MEM", { mode: "bigint", unsigned: true }).notNull(),
2969
+ // Max memory used (bytes)
2970
+ avgDisk: bigint("AVG_DISK", { mode: "bigint", unsigned: true }).notNull(),
2971
+ // Avg disk used (bytes)
2972
+ maxDisk: bigint("MAX_DISK", { mode: "bigint", unsigned: true }).notNull(),
2973
+ // Max disk used (bytes)
2974
+ avgKvTime: bigint("AVG_KV_TIME", { mode: "bigint", unsigned: true }).notNull(),
2975
+ // Avg time spent in TiKV (ns)
2976
+ avgPdTime: bigint("AVG_PD_TIME", { mode: "bigint", unsigned: true }).notNull(),
2977
+ // Avg time spent in PD (ns)
2978
+ avgBackoffTotalTime: bigint("AVG_BACKOFF_TOTAL_TIME", {
2979
+ mode: "bigint",
2980
+ unsigned: true
2981
+ }).notNull(),
2982
+ // Avg total backoff time (ns)
2983
+ avgWriteSqlRespTime: bigint("AVG_WRITE_SQL_RESP_TIME", {
2984
+ mode: "bigint",
2985
+ unsigned: true
2986
+ }).notNull(),
2987
+ // Avg write SQL response time (ns)
2988
+ avgTidbCpuTime: bigint("AVG_TIDB_CPU_TIME", { mode: "bigint", unsigned: true }).notNull(),
2989
+ // Avg TiDB CPU time (ns)
2990
+ avgTikvCpuTime: bigint("AVG_TIKV_CPU_TIME", { mode: "bigint", unsigned: true }).notNull(),
2991
+ // Avg TiKV CPU time (ns)
2992
+ maxResultRows: bigint("MAX_RESULT_ROWS", { mode: "bigint" }).notNull(),
2993
+ // Max number of result rows
2994
+ minResultRows: bigint("MIN_RESULT_ROWS", { mode: "bigint" }).notNull(),
2995
+ // Min number of result rows
2996
+ avgResultRows: bigint("AVG_RESULT_ROWS", { mode: "bigint" }).notNull(),
2997
+ // Avg number of result rows
2998
+ prepared: boolean("PREPARED").notNull(),
2999
+ // Whether statements are prepared
3000
+ avgAffectedRows: double("AVG_AFFECTED_ROWS").notNull(),
3001
+ // Avg affected rows
3002
+ firstSeen: timestamp("FIRST_SEEN", { mode: "string" }).notNull(),
3003
+ // First time statements observed
3004
+ lastSeen: timestamp("LAST_SEEN", { mode: "string" }).notNull(),
3005
+ // Last time statements observed
3006
+ planInCache: boolean("PLAN_IN_CACHE").notNull(),
3007
+ // Whether last stmt hit plan cache
3008
+ planCacheHits: bigint("PLAN_CACHE_HITS", { mode: "bigint" }).notNull(),
3009
+ // Number of plan cache hits
3010
+ planInBinding: boolean("PLAN_IN_BINDING").notNull(),
3011
+ // Whether matched bindings
3012
+ querySampleText: text("QUERY_SAMPLE_TEXT"),
3013
+ // Sampled original SQL
3014
+ prevSampleText: text("PREV_SAMPLE_TEXT"),
3015
+ // Sampled previous SQL before commit
3016
+ planDigest: varchar("PLAN_DIGEST", { length: 64 }),
3017
+ // Plan digest hash
3018
+ plan: text("PLAN"),
3019
+ // Sampled textual plan
3020
+ binaryPlan: text("BINARY_PLAN"),
3021
+ // Sampled binary plan
3022
+ charset: varchar("CHARSET", { length: 64 }),
3023
+ // Sampled charset
3024
+ collation: varchar("COLLATION", { length: 64 }),
3025
+ // Sampled collation
3026
+ planHint: varchar("PLAN_HINT", { length: 64 }),
3027
+ // Sampled plan hint
3028
+ maxRequestUnitRead: double("MAX_REQUEST_UNIT_READ").notNull(),
3029
+ // Max RU cost (read)
3030
+ avgRequestUnitRead: double("AVG_REQUEST_UNIT_READ").notNull(),
3031
+ // Avg RU cost (read)
3032
+ maxRequestUnitWrite: double("MAX_REQUEST_UNIT_WRITE").notNull(),
3033
+ // Max RU cost (write)
3034
+ avgRequestUnitWrite: double("AVG_REQUEST_UNIT_WRITE").notNull(),
3035
+ // Avg RU cost (write)
3036
+ maxQueuedRcTime: bigint("MAX_QUEUED_RC_TIME", { mode: "bigint", unsigned: true }).notNull(),
3037
+ // Max queued time waiting for RU (ns)
3038
+ avgQueuedRcTime: bigint("AVG_QUEUED_RC_TIME", { mode: "bigint", unsigned: true }).notNull(),
3039
+ // Avg queued time waiting for RU (ns)
3040
+ resourceGroup: varchar("RESOURCE_GROUP", { length: 64 }),
3041
+ // Bound resource group name
3042
+ planCacheUnqualified: bigint("PLAN_CACHE_UNQUALIFIED", { mode: "bigint" }).notNull(),
3043
+ // Times not eligible for plan cache
3044
+ planCacheUnqualifiedLastReason: text("PLAN_CACHE_UNQUALIFIED_LAST_REASON")
3045
+ // Last reason of plan cache ineligibility
3046
+ });
3047
+ const clusterStatementsSummaryHistory = informationSchema.table(
3048
+ "CLUSTER_STATEMENTS_SUMMARY_HISTORY",
3049
+ createClusterStatementsSummarySchema()
3050
+ );
3051
+ const clusterStatementsSummary = informationSchema.table(
3052
+ "CLUSTER_STATEMENTS_SUMMARY",
3053
+ createClusterStatementsSummarySchema()
3054
+ );
2626
3055
  async function getTables() {
2627
3056
  const tables = await sql$1.executeDDL("SHOW TABLES");
2628
3057
  return tables.rows.flatMap((tableInfo) => Object.values(tableInfo));
@@ -2673,13 +3102,13 @@ const applySchemaMigrations = async (migration) => {
2673
3102
  body: "Migrations successfully executed"
2674
3103
  };
2675
3104
  } catch (error) {
2676
- const errorMessage = error?.debug?.sqlMessage ?? error?.debug?.message ?? error.message ?? "Unknown error occurred";
3105
+ const errorMessage = error?.cause?.context?.debug?.sqlMessage ?? error?.cause?.context?.debug?.message ?? error?.debug?.context?.sqlMessage ?? error?.debug?.context?.message ?? error.message ?? "Unknown error occurred";
2677
3106
  console.error("Error during migration:", errorMessage);
2678
3107
  return {
2679
3108
  headers: { "Content-Type": ["application/json"] },
2680
3109
  statusCode: 500,
2681
3110
  statusText: "Internal Server Error",
2682
- body: error instanceof Error ? error.message : "Unknown error during migration"
3111
+ body: error instanceof Error ? errorMessage : "Unknown error during migration"
2683
3112
  };
2684
3113
  }
2685
3114
  };
@@ -2770,6 +3199,169 @@ const clearCacheSchedulerTrigger = async (options) => {
2770
3199
  };
2771
3200
  }
2772
3201
  };
3202
+ const topSlowestStatementLastHourTrigger = async (orm, warnThresholdMs = 300, memoryThresholdBytes = 8 * 1024 * 1024) => {
3203
+ const nsToMs = (v) => {
3204
+ const n = Number(v);
3205
+ return Number.isFinite(n) ? n / 1e6 : NaN;
3206
+ };
3207
+ const bytesToMB = (v) => {
3208
+ const n = Number(v);
3209
+ return Number.isFinite(n) ? n / (1024 * 1024) : NaN;
3210
+ };
3211
+ const jsonSafeStringify = (value) => JSON.stringify(value, (_k, v) => typeof v === "bigint" ? v.toString() : v);
3212
+ const TOP_N = 1;
3213
+ try {
3214
+ const summaryHistory = clusterStatementsSummaryHistory;
3215
+ const summary = clusterStatementsSummary;
3216
+ const selectShape = (t) => ({
3217
+ digest: t.digest,
3218
+ stmtType: t.stmtType,
3219
+ schemaName: t.schemaName,
3220
+ execCount: t.execCount,
3221
+ avgLatencyNs: t.avgLatency,
3222
+ maxLatencyNs: t.maxLatency,
3223
+ minLatencyNs: t.minLatency,
3224
+ avgProcessTimeNs: t.avgProcessTime,
3225
+ avgWaitTimeNs: t.avgWaitTime,
3226
+ avgBackoffTimeNs: t.avgBackoffTime,
3227
+ avgTotalKeys: t.avgTotalKeys,
3228
+ firstSeen: t.firstSeen,
3229
+ lastSeen: t.lastSeen,
3230
+ planInCache: t.planInCache,
3231
+ planCacheHits: t.planCacheHits,
3232
+ digestText: t.digestText,
3233
+ plan: t.plan,
3234
+ avgMemBytes: t.avgMem,
3235
+ maxMemBytes: t.maxMem
3236
+ });
3237
+ const lastHourFilterHistory = gte(
3238
+ summaryHistory.summaryEndTime,
3239
+ sql`DATE_SUB(NOW(), INTERVAL 1 HOUR)`
3240
+ );
3241
+ const lastHourFilterSummary = gte(
3242
+ summary.summaryEndTime,
3243
+ sql`DATE_SUB(NOW(), INTERVAL 1 HOUR)`
3244
+ );
3245
+ const qHistory = orm.getDrizzleQueryBuilder().select(selectShape(summaryHistory)).from(summaryHistory).where(lastHourFilterHistory);
3246
+ const qSummary = orm.getDrizzleQueryBuilder().select(selectShape(summary)).from(summary).where(lastHourFilterSummary);
3247
+ const combined = unionAll(qHistory, qSummary).as("combined");
3248
+ const thresholdNs = Math.floor(warnThresholdMs * 1e6);
3249
+ const grouped = orm.getDrizzleQueryBuilder().select({
3250
+ digest: combined.digest,
3251
+ stmtType: combined.stmtType,
3252
+ schemaName: combined.schemaName,
3253
+ execCount: sql`SUM(${combined.execCount})`.as("execCount"),
3254
+ avgLatencyNs: sql`MAX(${combined.avgLatencyNs})`.as("avgLatencyNs"),
3255
+ maxLatencyNs: sql`MAX(${combined.maxLatencyNs})`.as("maxLatencyNs"),
3256
+ minLatencyNs: sql`MIN(${combined.minLatencyNs})`.as("minLatencyNs"),
3257
+ avgProcessTimeNs: sql`MAX(${combined.avgProcessTimeNs})`.as("avgProcessTimeNs"),
3258
+ avgWaitTimeNs: sql`MAX(${combined.avgWaitTimeNs})`.as("avgWaitTimeNs"),
3259
+ avgBackoffTimeNs: sql`MAX(${combined.avgBackoffTimeNs})`.as("avgBackoffTimeNs"),
3260
+ avgMemBytes: sql`MAX(${combined.avgMemBytes})`.as("avgMemBytes"),
3261
+ maxMemBytes: sql`MAX(${combined.maxMemBytes})`.as("maxMemBytes"),
3262
+ avgTotalKeys: sql`MAX(${combined.avgTotalKeys})`.as("avgTotalKeys"),
3263
+ firstSeen: sql`MIN(${combined.firstSeen})`.as("firstSeen"),
3264
+ lastSeen: sql`MAX(${combined.lastSeen})`.as("lastSeen"),
3265
+ planInCache: sql`MAX(${combined.planInCache})`.as("planInCache"),
3266
+ planCacheHits: sql`SUM(${combined.planCacheHits})`.as("planCacheHits"),
3267
+ // Prefer a non-empty sample text/plan via MAX; acceptable for de-dup
3268
+ digestText: sql`MAX(${combined.digestText})`.as("digestText"),
3269
+ plan: sql`MAX(${combined.plan})`.as("plan")
3270
+ }).from(combined).where(
3271
+ sql`COALESCE(${combined.digest}, '') <> '' AND COALESCE(${combined.digestText}, '') <> '' AND COALESCE(${combined.stmtType}, '') NOT IN ('Use','Set','Show')`
3272
+ ).groupBy(combined.digest, combined.stmtType, combined.schemaName).as("grouped");
3273
+ const rows = await orm.getDrizzleQueryBuilder().select({
3274
+ digest: grouped.digest,
3275
+ stmtType: grouped.stmtType,
3276
+ schemaName: grouped.schemaName,
3277
+ execCount: grouped.execCount,
3278
+ avgLatencyNs: grouped.avgLatencyNs,
3279
+ maxLatencyNs: grouped.maxLatencyNs,
3280
+ minLatencyNs: grouped.minLatencyNs,
3281
+ avgProcessTimeNs: grouped.avgProcessTimeNs,
3282
+ avgWaitTimeNs: grouped.avgWaitTimeNs,
3283
+ avgBackoffTimeNs: grouped.avgBackoffTimeNs,
3284
+ avgMemBytes: grouped.avgMemBytes,
3285
+ maxMemBytes: grouped.maxMemBytes,
3286
+ avgTotalKeys: grouped.avgTotalKeys,
3287
+ firstSeen: grouped.firstSeen,
3288
+ lastSeen: grouped.lastSeen,
3289
+ planInCache: grouped.planInCache,
3290
+ planCacheHits: grouped.planCacheHits,
3291
+ digestText: grouped.digestText,
3292
+ plan: grouped.plan
3293
+ }).from(grouped).where(
3294
+ sql`${grouped.avgLatencyNs} > ${thresholdNs} OR ${grouped.avgMemBytes} > ${memoryThresholdBytes}`
3295
+ ).orderBy(desc(grouped.avgLatencyNs)).limit(formatLimitOffset(TOP_N));
3296
+ const formatted = rows.map((r, i) => ({
3297
+ rank: i + 1,
3298
+ // 1-based rank in the top N
3299
+ digest: r.digest,
3300
+ stmtType: r.stmtType,
3301
+ schemaName: r.schemaName,
3302
+ execCount: r.execCount,
3303
+ avgLatencyMs: nsToMs(r.avgLatencyNs),
3304
+ // Convert ns to ms for readability
3305
+ maxLatencyMs: nsToMs(r.maxLatencyNs),
3306
+ minLatencyMs: nsToMs(r.minLatencyNs),
3307
+ avgProcessTimeMs: nsToMs(r.avgProcessTimeNs),
3308
+ avgWaitTimeMs: nsToMs(r.avgWaitTimeNs),
3309
+ avgBackoffTimeMs: nsToMs(r.avgBackoffTimeNs),
3310
+ avgMemMB: bytesToMB(r.avgMemBytes),
3311
+ maxMemMB: bytesToMB(r.maxMemBytes),
3312
+ avgMemBytes: r.avgMemBytes,
3313
+ maxMemBytes: r.maxMemBytes,
3314
+ avgTotalKeys: r.avgTotalKeys,
3315
+ firstSeen: r.firstSeen,
3316
+ lastSeen: r.lastSeen,
3317
+ planInCache: r.planInCache,
3318
+ planCacheHits: r.planCacheHits,
3319
+ digestText: r.digestText,
3320
+ plan: r.plan
3321
+ }));
3322
+ for (const f of formatted) {
3323
+ console.warn(
3324
+ `${f.rank}. ${f.stmtType} avg=${f.avgLatencyMs?.toFixed?.(2)}ms max=${f.maxLatencyMs?.toFixed?.(2)}ms mem≈${f.avgMemMB?.toFixed?.(2)}MB(max ${f.maxMemMB?.toFixed?.(2)}MB) exec=${f.execCount}
3325
+ digest=${f.digest}
3326
+ sql=${(f.digestText || "").slice(0, 300)}${f.digestText && f.digestText.length > 300 ? "…" : ""}`
3327
+ );
3328
+ if (f.plan) {
3329
+ console.warn(` full plan:
3330
+ ${f.plan}`);
3331
+ }
3332
+ }
3333
+ return {
3334
+ headers: { "Content-Type": ["application/json"] },
3335
+ statusCode: 200,
3336
+ statusText: "OK",
3337
+ body: jsonSafeStringify({
3338
+ success: true,
3339
+ window: "last_1h",
3340
+ top: TOP_N,
3341
+ warnThresholdMs,
3342
+ memoryThresholdBytes,
3343
+ rows: formatted,
3344
+ generatedAt: (/* @__PURE__ */ new Date()).toISOString()
3345
+ })
3346
+ };
3347
+ } catch (error) {
3348
+ console.error(
3349
+ "Error in topSlowestStatementLastHourTrigger:",
3350
+ error?.cause?.context?.debug?.sqlMessage ?? error?.cause ?? error
3351
+ );
3352
+ return {
3353
+ headers: { "Content-Type": ["application/json"] },
3354
+ statusCode: 500,
3355
+ statusText: "Internal Server Error",
3356
+ body: jsonSafeStringify({
3357
+ success: false,
3358
+ message: "Failed to fetch or log slow queries",
3359
+ error: error?.cause?.context?.debug?.sqlMessage ?? error?.cause?.message,
3360
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
3361
+ })
3362
+ };
3363
+ }
3364
+ };
2773
3365
  const getHttpResponse = (statusCode, body) => {
2774
3366
  let statusText = "";
2775
3367
  if (statusCode === 200) {
@@ -2790,6 +3382,8 @@ export {
2790
3382
  applyFromDriverTransform,
2791
3383
  applySchemaMigrations,
2792
3384
  clearCacheSchedulerTrigger,
3385
+ clusterStatementsSummary,
3386
+ clusterStatementsSummaryHistory,
2793
3387
  ForgeSQLORM as default,
2794
3388
  dropSchemaMigrations,
2795
3389
  dropTableSchemaMigrations,
@@ -2812,6 +3406,8 @@ export {
2812
3406
  migrations,
2813
3407
  nextVal,
2814
3408
  parseDateTime,
2815
- patchDbWithSelectAliased
3409
+ patchDbWithSelectAliased,
3410
+ slowQuery,
3411
+ topSlowestStatementLastHourTrigger
2816
3412
  };
2817
3413
  //# sourceMappingURL=ForgeSQLORM.mjs.map