data.schema.parse

Page actions AI-ready formats and sharing
Open LLM text
Share with AI
Ask Claude Ask ChatGPT Ask Gemini Ask Copilot

Schema parser — parse desired schema SQL into a SchemaSnapshot.

Parses CREATE TABLE and CREATE INDEX statements from SQL text.

Schema parser — parse desired schema SQL into a SchemaSnapshot.

Parses CREATE TABLE and CREATE INDEX statements from SQL text.

data.schema.parse

Name Type Default Description
type
qualified_name
element_type
description
source_file
line_number
is_autodoc
autodoc_element
_autodoc_template
_autodoc_url_path
_autodoc_page_type
title
doc_content_hash

Symbols on this page

_parse_column_def
function
def _parse_column_def(col_def: str, table_name: str) -> tuple[ColumnSchema | None, ForeignKey | None]

Parse a single column definition.

Parameters

Name Type Default Description
col_def str
table_name str
_split_column_defs
function
def _split_column_defs(body: str) -> list[str]

Split column definitions handling nested parentheses.

Parameters

Name Type Default Description
body str
parse_schema
function
def parse_schema(sql: str) -> SchemaSnapshot

Parse SQL schema text into a SchemaSnapshot.

Supports CREATE TABLE and CREATE INDEX statements.

Parameters

Name Type Default Description
sql str
_apply_add_columns
function
def _apply_add_columns(sql: str, tables: dict[str, TableSchema]) -> None

ApplyALTER TABLE ... ADD COLUMNstatements to parsed tables.

parse_schema only models CREATE TABLE / CREATE INDEX. Migrations routinely add columns later, so the declared-schema union used by contract checks must fold those in or a perfectly valid SELECT would look like drift. Only the column name matters for the shape contract; type is recorded as a best-effort string. Unknown tables are ignored (no CREATE seen yet).

Parameters

Name Type Default Description
sql str
tables dict[str, TableSchema]
schema_from_migrations
function
def schema_from_migrations(migrations_dir: str | Path) -> SchemaSnapshot | None

Build a declaredSchemaSnapshotfrom a migrations directory.

Reads every*.sqlfile in migrations_dir in sorted (version) order and folds theirCREATE TABLE / CREATE INDEX / ALTER TABLE ADD COLUMN statements into a single snapshot. This is the declared schema used by the datashape contract -- no live database connection is required.

ReturnsNone when the directory is missing or contains no .sqlfiles, so the contract stays a silent no-op for HTML-only / db-less apps. This is a tolerant, static read: malformed individual statements are simply not parsed rather than raising (unlike the migration runner, which must be strict).

Fidelity note: onlyCREATE/ADD COLUMN are folded; ALTER TABLE RENAME COLUMN and DROP COLUMNare not applied, so a heavily-altered table's snapshot may list columns that no longer exist. Because thedata shape contract uses this snapshot only to suppress drift warnings (never to raise on its own), an over-broad column set is the safe direction -- it can miss a real drift but will not invent a false positive.

Parameters

Name Type Default Description
migrations_dir str | Path

View source · /home/runner/work/chirp/chirp/site/../src/chirp/data/schema/parse.py:1