testing/schema_introspect.ts

PostgreSQL schema introspection — produces a normalized, JSON-serializable snapshot of a database's structure for cross-impl parity checks.

The snapshot covers:

  • Tables with columns (data type, nullability, default, identity)
  • Indexes with canonical Postgres-rendered definitions
  • Constraints (CHECK, FOREIGN KEY, PRIMARY KEY, UNIQUE, EXCLUSION)
  • Sequences with data type — distinguishes int4 (SERIAL) from int8 (BIGSERIAL)
  • Enum types (CREATE TYPE ... AS ENUM) with labels in declared order — so a label set / ordering drift (e.g. cell_visibility) is a gated fact, not invisible

Designed for pg_catalog introspection — it runs on PostgreSQL, PGlite and pglet. The snapshot is fully deterministic: every collection sorts by a stable key and excludes time-varying fields like applied_at.

Index and constraint definitions are compared as the engine renders them, so both sides of a parity comparison need engines that render alike. pglet leaves an index's modifiers out of pg_indexes.indexdef — per-key sort direction, NULLS placement, operator classes, NULLS NOT DISTINCT — and that misleads a comparison in both directions: a pglet snapshot diffed against a PostgreSQL one reports every index that uses a modifier, with identical DDL on both sides (a false difference), and two pglet snapshots agree when the two schemas differ only in a modifier (a false agreement). So query_schema_snapshot throws on pglet when the snapshot holds an index, unless the caller passes accept_pglet_index_definitions. Take both snapshots of a parity comparison on PostgreSQL.

Paired with testing/schema_parity.ts for comparison + assertion helpers.

view source

Declarations
#

10 declarations

ColumnSnapshot
#

testing/schema_introspect.ts view source

value + type

{ data_type: string; udt_name: string; is_nullable: boolean; column_default: string | null; is_identity: boolean; } import {ColumnSnapshot} from '@fuzdev/fuz_app/testing/schema_introspect.js';

Per-column structural metadata. The schema is the canonical source for the column shape — SchemaSnapshot reuses it as the cross-impl _testing_schema_snapshot RPC action's wire validator, so the introspection type and the wire contract can't drift apart.

data_type

SQL standard type name from information_schema.columns.data_type.

type string

udt_name

Postgres-native type name from information_schema.columns.udt_name.

type string

is_nullable

true when the column accepts NULL.

type boolean

column_default

Default-value expression as Postgres reports it, or null if none.

type string | null

is_identity

true when the column was declared GENERATED ... AS IDENTITY.

type boolean

EnumTypeSnapshot
#

testing/schema_introspect.ts view source

value + type

{ labels: string[]; } import {EnumTypeSnapshot} from '@fuzdev/fuz_app/testing/schema_introspect.js';

Enum-type metadata — the labels of a CREATE TYPE ... AS ENUM, captured in pg_enum.enumsortorder (declaration) order. Order is significant: a Postgres enum's labels are an ordered set, and reordering them is a schema change, so the parity diff compares the arrays positionally.

labels

Enum labels in declared order.

type string[]

MigrationTracker
#

testing/schema_introspect.ts view source

value + type

{ entries: { namespace: string; name: string; sequence: number; }[]; } import {MigrationTracker} from '@fuzdev/fuz_app/testing/schema_introspect.js';

The full schema_version tracker as a deterministically-ordered list, wrapped in an object so it round-trips cleanly as a JSON-RPC result. Sorted by (namespace, sequence) on capture.

entries

type MigrationTrackerEntry[]

MigrationTrackerEntry
#

testing/schema_introspect.ts view source

value + type

{ namespace: string; name: string; sequence: number; } import {MigrationTrackerEntry} from '@fuzdev/fuz_app/testing/schema_introspect.js';

A single schema_version tracker row — the migration-identity primitive.

Where SchemaSnapshot is provenance-agnostic (it captures the resulting tables and deliberately *excludes* the tracker), this is the tracker: the (namespace, name, sequence) the migration runner records per applied migration. sequence carries order; name carries identity (the PK is (namespace, name)). The cross-impl gate diffs these between the two bootstrapped spines so a migration-name or partitioning drift — invisible to the schema snapshot — is a gated fact, not a latent interop break.

namespace

Migration namespace (e.g. fuz_auth, fuz_cell).

type string

name

Migration name within the namespace (e.g. full_cell_schema).

type string

sequence

Per-namespace apply order, starting at 0.

type number

query_migration_tracker
#

testing/schema_introspect.ts view source

(db: Db): Promise<{ entries: { namespace: string; name: string; sequence: number; }[]; }> import {query_migration_tracker} from '@fuzdev/fuz_app/testing/schema_introspect.js';

Read every schema_version row into a deterministic MigrationTracker.

The migration-identity twin of query_schema_snapshot: that captures the resulting schema (and excludes this tracker); this captures the tracker rows themselves, so the cross-impl gate can assert the two spines record byte-identical migration identity.

db

type Db

returns

Promise<MigrationTracker>

query_schema_snapshot
#

testing/schema_introspect.ts view source

(db: Db, options?: QuerySchemaSnapshotOptions): Promise<{ tables: Record<string, { columns: Record<string, { data_type: string; udt_name: string; is_nullable: boolean; column_default: string | null; is_identity: boolean; }>; indexes: { ...; }[]; constraints: { ...; }[]; }>; sequences: Record<...>; enums: Record<...>; }> import {query_schema_snapshot} from '@fuzdev/fuz_app/testing/schema_introspect.js';

Introspect a live database into a deterministic SchemaSnapshot.

Reads information_schema and pg_catalog to capture tables, columns, indexes, constraints, and sequences.

The schema_version migration tracker never appears in the tables field — it's framework bookkeeping created by the migration runner, identical across consumers, and would only add noise.

db

the database to introspect

type Db

options?

schema, table excludes, and the pglet opt-in

optional default {}

returns

Promise<SchemaSnapshot>

the snapshot, every collection sorted

throws

  • Error - on pglet when the snapshot holds an index and `accept_pglet_index_definitions` is not set — its index definitions omit modifiers, so a comparison built on them is wrong in both directions

QuerySchemaSnapshotOptions
#

testing/schema_introspect.ts view source

QuerySchemaSnapshotOptions import type {QuerySchemaSnapshotOptions} from '@fuzdev/fuz_app/testing/schema_introspect.js';

Filter options for query_schema_snapshot.

schema?

Schema name to introspect — defaults to 'public'. Single-schema only; cross-schema introspection isn't a current need.

type string

readonly

exclude_tables?

Tables to exclude from the snapshot. The schema_version migration tracker is always excluded — it's framework bookkeeping created by the migration runner, identical across impls, and not part of any consumer's domain schema.

type readonly string[]

readonly

accept_pglet_index_definitions?

Take the snapshot on pglet even though it holds indexes. Default false: pglet's pg_indexes.indexdef omits index modifiers (sort direction, NULLS placement, operator classes, NULLS NOT DISTINCT), so the snapshot's index definitions cannot be compared with another engine's, and do not tell two pglet schemas apart when they differ only in a modifier. Pass true only where no index definition is compared — reading tables, columns, constraints, sequences or enums off the snapshot. No effect on PostgreSQL or PGlite.

type boolean

readonly

SchemaSnapshot
#

testing/schema_introspect.ts view source

value + type

{ tables: Record<string, { columns: Record<string, { data_type: string; udt_name: string; is_nullable: boolean; column_default: string | null; is_identity: boolean; }>; indexes: { name: string; definition: string; }[]; constraints: { ...; }[]; }>; sequences: Record<...>; enums: Record<...>; } import {SchemaSnapshot} from '@fuzdev/fuz_app/testing/schema_introspect.js';

Normalized database schema snapshot for parity comparison — the single source of truth for the snapshot shape across the introspection query (query_schema_snapshot), the diff comparator (testing/schema_parity.ts), and the cross-impl RPC action's wire validator (testing/cross_backend/testing_reset_actions.ts).

All fields are deterministically ordered on capture so structural equality via JSON.stringify or per-key comparison yields stable results.

tables

Tables keyed by name.

type Record<string, TableSnapshot>

sequences

Sequences keyed by name.

type Record<string, SequenceSnapshot>

enums

Enum types (CREATE TYPE ... AS ENUM) keyed by name; labels in declared order.

type Record<string, EnumTypeSnapshot>

SequenceSnapshot
#

testing/schema_introspect.ts view source

value + type

{ data_type: string; } import {SequenceSnapshot} from '@fuzdev/fuz_app/testing/schema_introspect.js';

Sequence metadata — data_type is bigint (BIGSERIAL) or integer (SERIAL).

data_type

type string

TableSnapshot
#

testing/schema_introspect.ts view source

value + type

{ columns: Record<string, { data_type: string; udt_name: string; is_nullable: boolean; column_default: string | null; is_identity: boolean; }>; indexes: { name: string; definition: string; }[]; constraints: { ...; }[]; } import {TableSnapshot} from '@fuzdev/fuz_app/testing/schema_introspect.js';

Per-table structural metadata.

columns

Column metadata keyed by column name (sorted on serialization).

type Record<string, ColumnSnapshot>

indexes

Index definitions as Postgres renders them via pg_indexes.indexdef.

type { name: string; definition: string; }[]

constraints

Constraint definitions as Postgres renders them via pg_get_constraintdef.

type { name: string; type: string; definition: string; }[]

Depends on
#

Imported by
#