schema

package
v0.0.0-...-8cc97d5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package schema defines the intermediate representation (IR) for database schemas. All parsers (Postgres DDL, MySQL DDL, live DB introspection) produce this IR, and all code generators consume it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResolvePolymorphic

func ResolvePolymorphic(s *Schema, defs []PolymorphicDef)

ResolvePolymorphic adds polymorphic relationships to the schema. For each polymorphic definition, it creates: - RelPolymorphicOne on the source table (one per target) - RelPolymorphicMany on each target table

func ResolveRelationships

func ResolveRelationships(s *Schema)

ResolveRelationships walks all foreign keys in the schema and infers relationships between tables. It populates the Relationships field on each table.

It detects:

  • BelongsTo: table has a FK to another table (many-to-one)
  • HasOne: another table has a FK to this table, and the FK columns are unique
  • HasMany: another table has a FK to this table (one-to-many)
  • ManyToMany: a join table connects two tables via two FKs

Types

type Action

type Action string

Action represents a foreign key referential action.

const (
	ActionNone       Action = ""
	ActionCascade    Action = "CASCADE"
	ActionSetNull    Action = "SET NULL"
	ActionSetDefault Action = "SET DEFAULT"
	ActionRestrict   Action = "RESTRICT"
	ActionNoAction   Action = "NO ACTION"
)

type Column

type Column struct {
	Name       string
	DBType     string // Raw DB type string (e.g., "uuid", "text", "integer")
	IsNullable bool
	HasDefault bool
	DefaultVal string // Raw default expression as string
	IsArray    bool
	ArrayDims  int // Number of array dimensions (1 for int[], 2 for int[][], etc.)

	// IsAutoIncrement is true for SERIAL/BIGSERIAL or GENERATED ALWAYS AS IDENTITY.
	IsAutoIncrement bool

	// EnumName is set when the column type is a user-defined enum.
	EnumName string
}

Column represents a single column in a table.

type Enum

type Enum struct {
	Schema string
	Name   string
	Values []string
}

Enum represents a database enum type.

type ForeignKey

type ForeignKey struct {
	Name       string   // Constraint name
	Columns    []string // Local column names
	RefTable   string   // Referenced table name
	RefSchema  string   // Referenced table schema
	RefColumns []string // Referenced column names
	OnDelete   Action
	OnUpdate   Action
}

ForeignKey represents a foreign key constraint.

type Index

type Index struct {
	Name    string
	Columns []string
	Unique  bool
}

Index represents a database index.

type Parser

type Parser interface {
	// ParseFile parses a single SQL file and returns the schema objects found.
	ParseFile(path string) (*Schema, error)

	// ParseDir parses all SQL files in a directory and merges them into one Schema.
	ParseDir(dir string) (*Schema, error)

	// ParseString parses SQL from a string.
	ParseString(sql string) (*Schema, error)
}

Parser is the interface that all DDL parsers implement.

type PolymorphicDef

type PolymorphicDef struct {
	Table      string            // Table with type+id columns
	TypeColumn string            // Column holding type discriminator
	IDColumn   string            // Column holding FK value
	Targets    map[string]string // TypeValue -> target table name
}

PolymorphicDef describes a polymorphic relationship from config.

type PrimaryKey

type PrimaryKey struct {
	Name    string   // Constraint name (may be empty)
	Columns []string // Column names in PK order
}

PrimaryKey represents a table's primary key constraint.

type RelationType

type RelationType int

RelationType classifies the kind of relationship between two tables.

const (
	RelBelongsTo       RelationType = iota // This table has the FK (many-to-one)
	RelHasOne                              // Other table has FK pointing here, unique constraint
	RelHasMany                             // Other table has FK pointing here
	RelManyToMany                          // Join table connects two tables
	RelPolymorphicOne                      // Polymorphic to-one (this table has type+id columns)
	RelPolymorphicMany                     // Polymorphic to-many (other table has type+id columns pointing here)
)

type Relationship

type Relationship struct {
	Type RelationType

	// Local side
	Table   string   // Table this relationship is defined on
	Columns []string // Local columns involved

	// Foreign side
	ForeignTable   string   // The other table
	ForeignColumns []string // Columns on the other table

	// For many-to-many: the join table details
	JoinTable          string
	JoinLocalColumns   []string // Join table columns pointing to local table
	JoinForeignColumns []string // Join table columns pointing to foreign table

	// ForeignKey is the FK that produced this relationship.
	ForeignKey *ForeignKey

	// For polymorphic relationships
	TypeColumn string // Column that holds the type discriminator (e.g., "commentable_type")
	IDColumn   string // Column that holds the FK value (e.g., "commentable_id")
	TypeValue  string // Value of the type column for this specific relationship (e.g., "User")
}

Relationship represents an inferred relationship between tables.

type Schema

type Schema struct {
	// Name is the schema name (e.g., "public" for Postgres default).
	Name string

	Tables []*Table
	Enums  []*Enum
	Views  []*View
}

Schema is the top-level container for a parsed database schema.

func (*Schema) FindTable

func (s *Schema) FindTable(name string) *Table

FindTable returns the table with the given name, or nil.

type Table

type Table struct {
	Schema      string // Schema name (e.g., "public")
	Name        string // Table name as it appears in the DB
	Columns     []*Column
	PrimaryKey  *PrimaryKey
	ForeignKeys []*ForeignKey
	Indexes     []*Index
	Unique      []*UniqueConstraint

	// Relationships are inferred from foreign keys after parsing.
	Relationships []*Relationship
}

Table represents a database table.

func (*Table) FindColumn

func (t *Table) FindColumn(name string) *Column

FindColumn returns the column with the given name, or nil.

func (*Table) FullName

func (t *Table) FullName() string

FullName returns the schema-qualified table name.

type UniqueConstraint

type UniqueConstraint struct {
	Name    string
	Columns []string
}

UniqueConstraint represents a UNIQUE constraint on one or more columns.

type View

type View struct {
	Schema         string
	Name           string
	Columns        []*Column
	IsMaterialized bool
	// Query is the raw SQL of the view definition, if available.
	Query string
}

View represents a database view or materialized view.

Directories

Path Synopsis
Package mysql implements a hand-written DDL parser for MySQL.
Package mysql implements a hand-written DDL parser for MySQL.
Package postgres implements a DDL parser for PostgreSQL using pg_query_go.
Package postgres implements a DDL parser for PostgreSQL using pg_query_go.
Package sqlite implements a hand-written DDL parser for SQLite.
Package sqlite implements a hand-written DDL parser for SQLite.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL