Repository navigation
Description
Summary
graphify extract --postgres <DSN> is the documented way to turn a Postgres schema into a graph, but it hard-requires a live psycopg connection. This proposes feeding the same code path a pre-generated DDL file instead (e.g. --postgres-ddl schema.sql, or letting --postgres accept a file), so environments that can't hand graphify a live DSN can still get the DB-schema graph.
Motivation — when a live DSN isn't available
- MCP / tool-mediated DB access. Increasingly common with Supabase and similar: the only DB access is through an authenticated MCP server that runs SQL but never exposes a raw
postgresql://user:pass@host/dbstring. You can runinformation_schema/pg_catalogqueries, but you can't givepsycopga DSN. - Air-gapped / CI without DB reachability. The graph is built where the DB isn't routable; a
pg_dump --schema-onlyartifact is available, the live server isn't. - Security policy. Some orgs bar DB credentials from leaving a boundary; a schema dump can cross it, a live connection can't.
Why graphify extract schema.sql isn't quite the same
A lone .sql file already gets parsed by tree-sitter-sql (nice!), but it isn't the --postgres path:
- no
postgresql://…virtual source framing — provenance reads as a local file, not "DB schema"; - a raw
pg_dump --schema-onlycarriesSET/COPY/ALTER/ complex PL/pgSQL bodies that the grammar parses unevenly (function definitions in particular), whereasintrospect_postgres()synthesizes normalizedCREATE …statements that parse cleanly.
The hard part is already connection-independent
introspect_postgres() ends with:
# graphify/pg_introspect.py (~L150)
result = extract_sql(virtual_path, content=ddl_string)Only the top half (the psycopg.connect + the four catalog queries, ~L11–100) needs a live DB. Everything from the DDL string onward — the value — is pure. A --postgres-ddl <file> would call extract_sql(virtual_path, content=Path(file).read_text()) and skip the connection: a small, additive surface that reuses the exact framing --postgres already produces.
Proposed interface (bikeshed welcome)
graphify extract --postgres-ddl schema.sql # DDL from a file
graphify extract --postgres - # DDL from stdin
Generate schema.sql however you can — pg_dump --schema-only, a query tool, or the same catalog queries introspect_postgres runs, emitted as CREATE TABLE/VIEW/FUNCTION + ALTER TABLE … ADD FOREIGN KEY.
Generic example
pg_dump --schema-only --no-owner "$DSN" > schema.sql # or produced via any SQL runner
graphify extract --postgres-ddl schema.sqlHappy to send a PR wiring --postgres-ddl through to extract_sql (plus a tests/ case with a tiny inline DDL) if the interface looks right.
Context: hit this while graphing a Supabase schema reachable only through an MCP server — the information_schema / pg_catalog queries run fine, but there's no raw DSN to hand psycopg. Building the normalized DDL and feeding extract_sql directly worked; a first-class flag would make it a supported path.
graphify 0.9.15 · installed via uv tool
Activity