codegen

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DiffSchemas added in v0.3.0

func DiffSchemas(old, newSchema Schema, dialect string) (upSQL, downSQL string)

DiffSchemas compares an old (previous snapshot) schema against a new (desired) schema and produces incremental up/down migration SQL containing only the changes. It returns ("", "") when the schemas are equivalent.

Coverage (up order; down is the sensible inverse):

  • new enums (Postgres only): CREATE TYPE / down DROP TYPE
  • new tables: CREATE TABLE + indexes / down DROP TABLE
  • dropped tables: DROP TABLE / down recreate
  • per-table column add/drop, type changes, NOT NULL changes
  • per-table index add/drop

SQLite cannot ALTER COLUMN TYPE or SET/DROP NOT NULL; those changes are emitted as clearly-marked WARNING comments rather than silently skipped. Column renames are not detected and surface as a drop + add.

func EmitDBFile

func EmitDBFile(models []ModelInfo, dbPkgName string) string

EmitDBFile generates the aggregated DB struct file that wires up all repositories. dbPkgName is the Go package name for the output file (e.g. "db").

func EmitModelFile

func EmitModelFile(m ModelInfo) string

EmitModelFile generates the complete per-model _drel.go file content.

func EmitModelFileChecked

func EmitModelFileChecked(m ModelInfo) (string, error)

EmitModelFileChecked generates the per-model file, returning an error if the model uses unsupported features.

func Generate

func Generate(configPath string) error

Generate runs the full code generation pipeline: loads config, scans packages, emits per-model files, and emits the aggregated DB file.

func GenerateCreateTable

func GenerateCreateTable(m ModelInfo, fks map[string]string, dialect string) string

GenerateCreateTable emits a CREATE TABLE statement for a single model. fks maps column names to referenced table names for foreign key constraints. dialect controls SQL type mappings and syntax ("postgres" or "sqlite").

func GenerateDropSchema

func GenerateDropSchema(models []ModelInfo) string

GenerateDropSchema emits DROP TABLE statements in reverse order to respect foreign key dependencies.

func GenerateSchema

func GenerateSchema(models []ModelInfo, dialect string) string

GenerateSchema emits the full schema DDL for a slice of models, including enum type definitions, foreign key constraints, and many-to-many pivot tables. dialect controls SQL type mappings and syntax ("postgres" or "sqlite").

func GoTypeToSQL

func GoTypeToSQL(goType string, dialect string) string

GoTypeToSQL maps a Go type string to its corresponding SQL type for the given dialect. Pointer types are unwrapped to their base type. Unknown types default to "text"/"TEXT". Supported dialects: "postgres" (default), "sqlite".

func SaveSnapshot added in v0.3.0

func SaveSnapshot(path string, s Schema) error

SaveSnapshot writes the schema to path as pretty-printed JSON, creating the parent directory if needed. The file is written with 0644 permissions.

Types

type Column added in v0.3.0

type Column struct {
	Name    string `json:"name"`
	Type    string `json:"type"`
	NotNull bool   `json:"notNull"`
	Default string `json:"default,omitempty"`
	Check   string `json:"check,omitempty"`
	Ref     string `json:"ref,omitempty"`
	PK      bool   `json:"pk,omitempty"`
}

Column describes a single column. Type is the dialect-resolved SQL type (already including any quoted enum type name). Ref, when non-empty, is the referenced table name for a foreign key on this column (always referencing that table's "id").

type Config

type Config struct {
	Packages []string     `yaml:"packages"`
	Output   OutputConfig `yaml:"output"`
	Dialect  string       `yaml:"dialect"`
	// Seed is an optional path to a Go main package that seeds the database.
	// `drel seed` runs it with `go run`, passing through DATABASE_URL.
	Seed string `yaml:"seed"`
}

func LoadConfig

func LoadConfig(path string) (*Config, error)

type EnumDef added in v0.3.0

type EnumDef struct {
	Name   string   `json:"name"`
	Values []string `json:"values"`
}

EnumDef describes a Postgres enum type. Name is the lower-cased local Go type name; it is emitted as a CREATE TYPE statement (Postgres only).

type FieldInfo

type FieldInfo struct {
	Name           string
	GoType         string
	ColumnName     string
	IsExported     bool
	RelTag         string
	Relation       *RelationFieldInfo
	IsVO           bool   // implements sql.Scanner + driver.Valuer (single-column VO)
	IsMultiColVO   bool   // implements drel.MultiColumnMapper (multi-column VO)
	MultiColPrefix string // db tag used as column prefix for multi-column VOs
	LocalGoType    string // type name without package qualifier, for same-package generated code
	TypePkgPath    string // import path of the field type's package (empty for primitives/same-package)
	IsPointer      bool   // whether the field is a pointer type
	IsEnum         bool
	EnumValues     []string
	Unique         bool   // db tag option: unique index on this column
	Indexed        bool   // db tag option: index on this column
	IndexName      string // explicit index name (db:"...,index=name"); fields sharing a name form a composite index
	CheckExpr      string // db tag option: column CHECK constraint expression (db:"...,check=expr")
}

type Index added in v0.3.0

type Index struct {
	Name    string   `json:"name"`
	Columns []string `json:"columns"`
	Unique  bool     `json:"unique,omitempty"`
}

Index describes a single (possibly composite, possibly unique) index.

type ModelInfo

type ModelInfo struct {
	Name          string
	PkgPath       string
	PkgName       string
	PKType        string // display type for generated code (e.g., "int", "uuid.UUID")
	PKTypeFull    string // fully qualified (e.g., "github.com/google/uuid.UUID")
	PKTypePkg     string // import path for external PK types (empty for primitives)
	TableName     string
	Fields        []FieldInfo
	HasSoftDelete bool
	HasVersioned  bool
	HasAudit      bool
	Dir           string // filesystem directory of the package
}

func ScanPackages

func ScanPackages(patterns []string, dir ...string) ([]ModelInfo, error)

type OutputConfig

type OutputConfig struct {
	DB         string `yaml:"db"`
	Migrations string `yaml:"migrations"`
}

type RelationFieldInfo

type RelationFieldInfo struct {
	Type        string // "has_many", "has_one", "belongs_to", "many_to_many"
	FK          string // foreign key column
	JoinTable   string // for many_to_many
	RefColumn   string // target FK column in pivot table (many-to-many)
	TargetModel string // target model name for foreign key resolution
}

RelationFieldInfo holds parsed relationship metadata from a `rel:"..."` struct tag.

type Schema added in v0.3.0

type Schema struct {
	Tables []Table   `json:"tables"`
	Enums  []EnumDef `json:"enums,omitempty"`
}

Schema is a JSON-serializable, dialect-resolved logical representation of the full database schema. It is the canonical input to the structured migration diff engine and the persisted migration snapshot.

func BuildSchema added in v0.3.0

func BuildSchema(models []ModelInfo, dialect string) Schema

BuildSchema produces the full logical schema for the given models and dialect. It includes all tables (models + many-to-many pivots), all columns (user columns + trait columns), all indexes (explicit/unique/composite from db tag options), and enum type definitions (Postgres only).

Ordering is deterministic: tables preserve model declaration order (pivots appended after, in discovery order), columns preserve PK-first then field order then trait order, and indexes within a table are sorted by name.

func LoadSnapshot added in v0.3.0

func LoadSnapshot(path string) (Schema, bool, error)

LoadSnapshot reads a schema snapshot from path. The returned bool is false when the file does not exist (which is not treated as an error).

type Table added in v0.3.0

type Table struct {
	Name       string   `json:"name"`
	Columns    []Column `json:"columns"`
	Indexes    []Index  `json:"indexes,omitempty"`
	PrimaryKey []string `json:"primaryKey,omitempty"`
}

Table describes a single relation (table or pivot) and its columns/indexes. PrimaryKey, when non-empty, declares a composite (table-level) primary key used by many-to-many pivot tables; for regular tables the primary key is the single column whose PK field is true.

Jump to

Keyboard shortcuts

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