{"version":3,"file":"schema.mjs","names":[],"sources":["../src/schema.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { SCHEMA_DUMP_SQL } from \"./schema-dump.js\";\nimport type { Postgres } from \"./sql/database.js\";\nimport { PgIdentifier } from \"./sql/pg-identifier.js\";\n\nconst Identifier = z.codec(z.string(), z.custom<PgIdentifier>(), {\n  encode: (v) => v.toString(),\n  decode: (v) => PgIdentifier.fromString(v),\n});\n\nexport const FullSchemaKeyColumn = z.object({\n  type: z.literal(\"indexColumn\"),\n  name: Identifier,\n  order: z.enum([\"ASC\", \"DESC\"]).optional(),\n  nulls: z.enum([\"FIRST\", \"LAST\"]).optional(),\n  opclass: z.string().optional(),\n  collation: z.string().optional(),\n});\n\nexport type FullSchemaKeyColumn = z.infer<typeof FullSchemaKeyColumn>;\n\nexport const FullSchemaIncludedColumn = z.object({\n  name: Identifier,\n});\n\nexport type FullSchemaIncludedColumn = z.infer<typeof FullSchemaIncludedColumn>;\n\nexport const FullSchemaIndex = z.object({\n  type: z.literal(\"index\"),\n  oid: z.number(),\n  schemaName: Identifier,\n  tableName: Identifier,\n  indexName: Identifier,\n  indexType: z.string(),\n  isUnique: z.boolean(),\n  isPrimary: z.boolean(),\n  isClustered: z.boolean(),\n  wherePredicate: z.string().optional(),\n  tablespace: z.string().optional(),\n  keyColumns: z.array(FullSchemaKeyColumn),\n  includedColumns: z.array(FullSchemaIncludedColumn).optional(),\n});\n\nexport type FullSchemaIndex = z.infer<typeof FullSchemaIndex>;\n\nexport const FullSchemaColumn = z.object({\n  type: z.literal(\"column\"),\n  name: Identifier,\n  order: z.number(),\n  columnType: z.string(),\n  isNullable: z.boolean(),\n  defaultValue: z.string().optional(),\n  dropped: z.boolean(),\n  collation: z.string().optional(),\n  storage: z.enum([\"plain\", \"main\", \"external\", \"extended\"]).optional(),\n  isIdentity: z.enum([\"always\", \"by default\"]).optional(),\n});\n\nexport type FullSchemaColumn = z.infer<typeof FullSchemaColumn>;\n\nexport const FullSchemaTable = z.object({\n  type: z.literal(\"table\"),\n  oid: z.number(),\n  schemaName: Identifier,\n  tableName: Identifier,\n  tablespace: z.string().optional(),\n  partitionKeyDef: z.string().optional(),\n  columns: z.array(FullSchemaColumn).default([]),\n});\n\nexport type FullSchemaTable = z.infer<typeof FullSchemaTable>;\n\nexport const FullSchemaConstraint = z.object({\n  type: z.literal(\"constraint\"),\n  oid: z.number(),\n  schemaName: Identifier,\n  tableName: Identifier,\n  constraintName: Identifier,\n  constraintType: z\n    .enum([\n      \"check\",\n      \"foreign_key\",\n      \"not_null\",\n      \"primary_key\",\n      \"unique\",\n      \"trigger\",\n      \"exclusion\",\n    ])\n    .or(z.string()),\n  definition: z.string(),\n  isDeferrable: z.boolean().optional(),\n  isInitiallyDeferred: z.boolean().optional(),\n  isValidated: z.boolean().optional(),\n  backingIndexOid: z.number().optional(),\n});\n\nexport type FullSchemaConstraint = z.infer<typeof FullSchemaConstraint>;\n\nexport const FullSchemaFunction = z.object({\n  type: z.literal(\"function\"),\n  schemaName: Identifier,\n  objectName: Identifier,\n  objectType: z.enum([\"function\", \"procedure\", \"aggregate\", \"window function\"]),\n  identityArguments: z.string().optional(),\n  definition: z.string(),\n});\n\nexport type FullSchemaFunction = z.infer<typeof FullSchemaFunction>;\n\nexport const FullSchemaExtension = z.object({\n  type: z.literal(\"extension\"),\n  extensionName: z.string(),\n  version: z.string(),\n  schemaName: Identifier,\n});\n\nexport type FullSchemaExtension = z.infer<typeof FullSchemaExtension>;\n\nexport const FullSchemaView = z.object({\n  type: z.literal(\"view\"),\n  schemaName: Identifier,\n  viewName: Identifier,\n  objectType: z.enum([\"view\", \"materialized_view\"]),\n  definition: z.string(),\n  tablespace: z.string().optional(),\n});\n\nexport type FullSchemaView = z.infer<typeof FullSchemaView>;\n\nexport const FullSchemaTypeConstraint = z.object({\n  name: Identifier,\n  definition: z.string(),\n});\n\nexport type FullSchemaTypeConstraint = z.infer<typeof FullSchemaTypeConstraint>;\n\nexport const FullSchemaCompositeAttribute = z.object({\n  type: z.literal(\"compositeAttribute\"),\n  name: Identifier,\n  attributeType: z.string(),\n  collation: Identifier.optional(),\n});\n\nexport type FullSchemaCompositeAttribute = z.infer<\n  typeof FullSchemaCompositeAttribute\n>;\n\nexport const FullSchemaType = z.object({\n  type: z.literal(\"type\"),\n  schemaName: Identifier,\n  typeName: Identifier,\n  typeCategory: z.enum([\"enum\", \"domain\", \"composite\"]),\n  enumLabels: z.array(z.string()).optional(),\n  domainBaseType: z.string().optional(),\n  domainIsNotNull: z.boolean().optional(),\n  domainDefault: z.string().optional(),\n  domainConstraints: z.array(FullSchemaTypeConstraint).optional(),\n  compositeAttributes: z.array(FullSchemaCompositeAttribute).optional(),\n});\n\nexport type FullSchemaType = z.infer<typeof FullSchemaType>;\n\nexport const FullSchemaTrigger = z.object({\n  type: z.literal(\"trigger\"),\n  schemaName: Identifier,\n  tableName: Identifier,\n  triggerName: Identifier,\n  definition: z.string(),\n  enabledMode: z.string(),\n});\n\nexport type FullSchemaTrigger = z.infer<typeof FullSchemaTrigger>;\n\nexport const FullSchema = z.object({\n  indexes: z.array(FullSchemaIndex).default([]),\n  tables: z.array(FullSchemaTable).default([]),\n  constraints: z.array(FullSchemaConstraint).default([]),\n  functions: z.array(FullSchemaFunction).default([]),\n  extensions: z.array(FullSchemaExtension).default([]),\n  views: z.array(FullSchemaView).default([]),\n  types: z.array(FullSchemaType).default([]),\n  triggers: z.array(FullSchemaTrigger).default([]),\n});\n\nexport type FullSchema = z.infer<typeof FullSchema>;\n\nexport async function dumpSchema(db: Postgres): Promise<FullSchema> {\n  const rows = await db.exec<{ result: FullSchema }>(SCHEMA_DUMP_SQL);\n  return FullSchema.parse(rows[0]?.result ?? {});\n}\n"],"mappings":";;;;;AAKA,MAAM,aAAa,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,QAAsB,EAAE;CAC/D,SAAS,MAAM,EAAE,UAAU;CAC3B,SAAS,MAAM,aAAa,WAAW,EAAE;CAC1C,CAAC;AAEF,MA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