Skip to main content

Module schema_from_db

Module schema_from_db 

Source
Expand description

Map a prax-query introspection result to a prax_schema::Schema.

prax migrate dev/diff need the current database structure expressed as a prax_schema::Schema so it can be fed to prax_migrate::SchemaDiffer as the diff source. The database driver (see crate::commands::introspect) produces a prax_query::introspection::DatabaseSchema; the migration engine’s prax_migrate::SchemaBuilder already knows how to turn its own raw introspection structs (TableInfo/ColumnInfo/ConstraintInfo/ IndexInfo/EnumInfo) into a Schema, complete with foreign-key relation synthesis, @map, @@index/@@unique, and primary-key detection — and that output is proven to round-trip cleanly with the differ.

This module bridges the two: it translates the query-layer DatabaseSchema into the migration engine’s introspection structs and runs them through SchemaBuilder. Keeping the translation here (rather than in prax-migrate) preserves the crate layering — prax-migrate depends only on prax-schema, while the CLI already depends on both prax-migrate and prax-query.

§Round-trip fidelity

The differ compares fields by name (SQL type, nullability, default), foreign keys by constraint name, and indexes by name. For a database that already matches its .prax schema to diff to empty (the “no spurious churn” property), the mapped source must reproduce the same constructs the target schema produces. Two mismatches are inherent to reverse-engineering and documented as limitations rather than papered over:

  • Field vs column names. A .prax field authorId mapped to column author_id reverse-engineers to a field named author_id. Schemas whose field names differ from their column names will show spurious add/drop churn; schemas whose field names match their columns (snake_case throughout) round-trip cleanly.
  • Foreign-key constraint names. The target auto-derives fk_<table>_<cols> unless the relation carries @relation(map: "..."). Introspection reports the real database constraint name. When they differ the differ proposes dropping/adding the FK; pin the name with @relation(map: ...) to avoid it.

Functions§

schema_from_database
Translate a query-layer DatabaseSchema into a prax_schema::Schema suitable as a diff source, using the migration engine’s SchemaBuilder.