{"version":3,"file":"database.cjs","names":["z"],"sources":["../../src/sql/database.ts"],"sourcesContent":["import { z } from \"zod\";\nimport type { PgIdentifier } from \"./pg-identifier\";\n\nexport const PostgresVersion = z.string().brand(\"PostgresVersion\");\nexport type PostgresVersion = z.infer<typeof PostgresVersion>;\n\nexport interface PostgresTransaction {\n  /**\n   * Exec a query and return the result as an array of objects.\n   */\n  exec<T>(query: string, params?: unknown[]): Promise<T[]>;\n}\n\n/**\n * A shared interface for all postgres connections.\n * This is required to allow interop between pglite and regular postgres drivers\n */\nexport interface Postgres extends PostgresTransaction {\n  transaction<T>(callback: (tx: PostgresTransaction) => Promise<T>): Promise<T>;\n  cursor?<T>(\n    query: string,\n    params?: unknown[],\n    options?: { size?: number },\n  ): AsyncGenerator<T, void, unknown>;\n  // postgres returns versions as a string\n  serverNum(): Promise<PostgresVersion>;\n}\n\nexport type PostgresConnectionInput = {\n  url: string;\n};\n\nexport type PostgresFactory = (input: PostgresConnectionInput) => Postgres;\n\n// PostgreSQL EXPLAIN plan types\ndeclare const StageId: unique symbol;\nexport type PostgresStageId = number & { [StageId]: \"StageId\" };\n\nexport type PostgresStage =\n  | \"Seq Scan\"\n  | \"Limit\"\n  | \"Bitmap Heap Scan\"\n  | \"Bitmap Index Scan\"\n  | \"Index Only Scan\"\n  | \"Index Scan\"\n  | \"BitmapOr\";\n\nexport type PostgresExplainStageCommon = {\n  \"Node Type\": PostgresStage;\n  // available only when doing a trace explain using the\n  // query doctor postgres binary\n  \"Node Id\": PostgresStageId;\n  Plans?: PostgresExplainStage[];\n  \"Plan Width\": number;\n  \"Total Cost\": number;\n};\n\nexport type PostgresExplainStage =\n  | (PostgresExplainStageCommon & {\n      \"Node Type\": \"Index Scan\";\n      \"Index Name\": string;\n      \"Relation Name\": string;\n      Alias: string;\n      \"Rows Removed by Filter\": number;\n    })\n  | (PostgresExplainStageCommon & {\n      \"Node Type\": \"Seq Scan\";\n      \"Relation Name\": string;\n      Filter?: string;\n      \"Actual Rows\": number;\n      \"Actual Loops\": number;\n      \"Rows Removed by Filter\": number;\n    })\n  | (PostgresExplainStageCommon & {\n      \"Node Type\": \"Bitmap Heap Scan\";\n      \"Relation Name\": string;\n      Filter?: string;\n      \"Actual Rows\": number;\n      \"Actual Loops\": number;\n      \"Rows Removed by Filter\": number;\n    })\n  | (PostgresExplainStageCommon & {\n      \"Node Type\": \"Bitmap Index Scan\";\n      \"Index Name\": string;\n      Filter?: string;\n      \"Actual Rows\": number;\n      \"Actual Loops\": number;\n      \"Rows Removed by Filter\": number;\n    })\n  | (PostgresExplainStageCommon & {\n      \"Node Type\": \"Index Only Scan\";\n      \"Index Name\": string;\n      Filter?: string;\n      \"Actual Rows\": number;\n      \"Actual Loops\": number;\n      \"Rows Removed by Filter\": number;\n    })\n  | PostgresExplainStageCommon;\n\n/** Zod schema for PostgresExplainStage — validates common fields, passes through variant-specific ones. */\nexport const PostgresExplainStageSchema: z.ZodType<PostgresExplainStage> =\n  z.lazy(() =>\n    z\n      .object({\n        \"Node Type\": z.string(),\n        \"Total Cost\": z.number(),\n        \"Plan Width\": z.number(),\n        \"Node Id\": z.number().optional(),\n        Plans: z.array(PostgresExplainStageSchema).optional(),\n      })\n      .passthrough(),\n  ) as z.ZodType<PostgresExplainStage>;\n\nexport type PostgresExplainResult = {\n  \"QUERY PLAN\": {\n    Plan: PostgresExplainStage;\n  }[];\n};\n\n/**\n * Drops a disabled index. Rollsback if it fails for any reason\n * @returns Did dropping the index succeed?\n */\nexport async function dropIndex(\n  tx: PostgresTransaction,\n  index: PgIdentifier,\n): Promise<boolean> {\n  try {\n    await tx.exec(`\n      savepoint idx_drop;\n      drop index if exists ${index} cascade;\n    `);\n    return true;\n  } catch {\n    // no problem if droping the index fails. It should throw an error\n    await tx.exec(`rollback to idx_drop`);\n    return false;\n  }\n}\n"],"mappings":";;;;AAGA,MAAa,kBAAkBA,IAAAA,EAAE,QAAQ,CAAC,MAAM,kBAAkB;;AAiGlE,MAAa,6BACXA,IAAAA,EAAE,WACAA,IAAAA,EACG,OAAO;CACN,aAAaA,IAAAA,EAAE,QAAQ;CACvB,cAAcA,IAAAA,EAAE,QAAQ;CACxB,cAAcA,IAAAA,EAAE,QAAQ;CACxB,WAAWA,IAAAA,EAAE,QAAQ,CAAC,UAAU;CAChC,OAAOA,IAAAA,EAAE,MAAM,2BAA2B,CAAC,UAAU;CACtD,CAAC,CACD,aAAa,CACjB;;;;;AAYH,eAAsB,UACpB,IACA,OACkB;AAClB,KAAI;AACF,QAAM,GAAG,KAAK;;6BAEW,MAAM;MAC7B;AACF,SAAO;SACD;AAEN,QAAM,GAAG,KAAK,uBAAuB;AACrC,SAAO"}