cquill/schema

Types

A schema describes the structure of a database table. It contains metadata about fields, primary keys, and table-level constraints.

pub type Schema {
  Schema(
    source: String,
    table_schema: option.Option(String),
    fields: List(field.Field),
    primary_key: List(String),
    table_constraints: List(TableConstraint),
    comment: option.Option(String),
  )
}

Constructors

  • Schema(
      source: String,
      table_schema: option.Option(String),
      fields: List(field.Field),
      primary_key: List(String),
      table_constraints: List(TableConstraint),
      comment: option.Option(String),
    )

    Arguments

    source

    The source table name in the database

    table_schema

    Schema/namespace (e.g., “public” in Postgres)

    fields

    Ordered list of fields

    primary_key

    Primary key column names (supports composite keys)

    table_constraints

    Table-level constraints (multi-column unique, etc.)

    comment

    Table comment/documentation

Compare two schemas and find differences Useful for schema migration planning

pub type SchemaDiff {
  FieldAdded(field.Field)
  FieldRemoved(String)
  FieldTypeChanged(
    name: String,
    old_type: field.FieldType,
    new_type: field.FieldType,
  )
  PrimaryKeyChanged(old_pk: List(String), new_pk: List(String))
}

Constructors

Validation errors for schemas

pub type SchemaError {
  InvalidPrimaryKey(column: String)
  DuplicateField(name: String)
  EmptySchema
  InvalidTableName(name: String)
  InvalidConstraintColumn(
    constraint_name: String,
    column: String,
  )
}

Constructors

  • InvalidPrimaryKey(column: String)

    Primary key references non-existent field

  • DuplicateField(name: String)

    Duplicate field name

  • EmptySchema

    Empty schema (no fields)

  • InvalidTableName(name: String)

    Invalid table name

  • InvalidConstraintColumn(constraint_name: String, column: String)

    Table constraint references invalid column

Table-level constraints (apply to multiple columns)

pub type TableConstraint {
  UniqueConstraint(name: String, columns: List(String))
  Index(name: String, columns: List(String), unique: Bool)
  TableCheck(name: String, expression: String)
  CompositeForeignKey(
    name: String,
    columns: List(String),
    references_table: String,
    references_columns: List(String),
  )
}

Constructors

  • UniqueConstraint(name: String, columns: List(String))

    Multi-column unique constraint

  • Index(name: String, columns: List(String), unique: Bool)

    Multi-column index (not enforced, but for documentation)

  • TableCheck(name: String, expression: String)

    Table-level check constraint

  • CompositeForeignKey(
      name: String,
      columns: List(String),
      references_table: String,
      references_columns: List(String),
    )

    Composite foreign key

Values

pub fn add_constraint(
  schema: Schema,
  constraint: TableConstraint,
) -> Schema

Add a table-level constraint

pub fn add_field(
  schema: Schema,
  new_field: field.Field,
) -> Schema

Add a field to the schema Fields are added in order and the order is preserved

pub fn auto_increment_fields(schema: Schema) -> List(field.Field)

Get all auto-increment fields

pub fn diff(
  old_schema: Schema,
  new_schema: Schema,
) -> List(SchemaDiff)

Find differences between two schemas

pub fn exclude_fields(
  schema: Schema,
  names: List(String),
) -> Schema

Create a new schema excluding the specified fields Useful for creating public schemas without sensitive fields

pub fn field(schema: Schema, new_field: field.Field) -> Schema

Alias for add_field - more concise in pipelines

pub fn field_count(schema: Schema) -> Int

Get field count

pub fn field_names(schema: Schema) -> List(String)

Get all field names in order

pub fn foreign_key_fields(schema: Schema) -> List(field.Field)

Get fields with foreign key constraints

pub fn get_field(
  schema: Schema,
  name: String,
) -> option.Option(field.Field)

Get a field by name

pub fn get_fields(schema: Schema) -> List(field.Field)

Get all fields in order

pub fn get_primary_key(schema: Schema) -> List(String)

Get the primary key columns

pub fn get_qualified_name(schema: Schema) -> String

Get the fully qualified table name (schema.table or just table)

pub fn get_source(schema: Schema) -> String

Get the source table name

pub fn has_field(schema: Schema, name: String) -> Bool

Check if schema has a field with the given name

pub fn has_primary_key(schema: Schema) -> Bool

Check if schema has a primary key defined

pub fn in_schema(schema: Schema, table_schema: String) -> Schema

Set the table schema/namespace

pub fn index(
  schema: Schema,
  name: String,
  columns: List(String),
  unique: Bool,
) -> Schema

Add an index

pub fn insertable_fields(schema: Schema) -> Schema

Create a new schema with only non-auto-generated fields Useful for insert schemas

pub fn is_valid(schema: Schema) -> Bool

Check if schema is valid

pub fn new(source: String) -> Schema

Create a new empty schema for a table

pub fn new_with_schema(
  source: String,
  table_schema: String,
) -> Schema

Create a new schema with a specific database schema/namespace

pub fn non_primary_key_fields(
  schema: Schema,
) -> List(field.Field)

Get fields that are NOT part of the primary key

pub fn nullable_fields(schema: Schema) -> List(field.Field)

Get all nullable fields

pub fn primary_key(
  schema: Schema,
  columns: List(String),
) -> Schema

Set the primary key columns Supports composite primary keys

pub fn primary_key_fields(schema: Schema) -> List(field.Field)

Get fields that are part of the primary key

pub fn rename_field(
  schema: Schema,
  old_name: String,
  new_name: String,
) -> Schema

Rename a field in the schema

pub fn required_fields(schema: Schema) -> List(field.Field)

Get all required (non-nullable, no default) fields

pub fn select_fields(
  schema: Schema,
  names: List(String),
) -> Schema

Create a new schema with only the specified fields Useful for creating insert/update schemas without auto-generated fields

pub fn single_primary_key(
  schema: Schema,
  column: String,
) -> Schema

Set a single-column primary key

pub fn table_check(
  schema: Schema,
  name: String,
  expression: String,
) -> Schema

Add a table-level check constraint

pub fn unique_constraint(
  schema: Schema,
  name: String,
  columns: List(String),
) -> Schema

Add a multi-column unique constraint

pub fn validate(schema: Schema) -> List(SchemaError)

Validate a schema and return any errors

pub fn with_comment(schema: Schema, text: String) -> Schema

Add a comment to the schema

Search Document