types

package
v1.11.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package types defines the shared schema data structures used throughout makemigrations.

Index

Constants

This section is empty.

Variables

View Source
var ValidFieldTypes = map[string]bool{
	"varchar":      true,
	"text":         true,
	"integer":      true,
	"bigint":       true,
	"float":        true,
	"decimal":      true,
	"boolean":      true,
	"date":         true,
	"timestamp":    true,
	"time":         true,
	"uuid":         true,
	"json":         true,
	"jsonb":        true,
	"serial":       true,
	"bytes":        true,
	"foreign_key":  true,
	"many_to_many": true,
	"text[]":       true,
}

ValidFieldTypes represents valid YAML field types

Functions

func IsValidDatabase

func IsValidDatabase(db string) bool

IsValidDatabase checks if a database type is valid

func IsValidFieldType

func IsValidFieldType(fieldType string) bool

IsValidFieldType checks if a field type is valid

Types

type Database

type Database struct {
	Name             string `yaml:"name"`
	Version          string `yaml:"version"`
	MigrationVersion string `yaml:"migration_version,omitempty"`
}

Database represents the database metadata

type DatabaseType

type DatabaseType string

DatabaseType represents supported database types

const (
	DatabasePostgreSQL DatabaseType = "postgresql"
	DatabaseMySQL      DatabaseType = "mysql"
	DatabaseSQLServer  DatabaseType = "sqlserver"
	DatabaseSQLite     DatabaseType = "sqlite"
	DatabaseRedshift   DatabaseType = "redshift"
	DatabaseClickHouse DatabaseType = "clickhouse"
	DatabaseTiDB       DatabaseType = "tidb"
	DatabaseVertica    DatabaseType = "vertica"
	DatabaseYDB        DatabaseType = "ydb"
	DatabaseTurso      DatabaseType = "turso"
	DatabaseStarRocks  DatabaseType = "starrocks"
	DatabaseAuroraDSQL DatabaseType = "auroradsql"
)

DatabaseType constants enumerate the supported database backends.

func ParseDatabaseType

func ParseDatabaseType(db string) (DatabaseType, error)

ParseDatabaseType parses a string into a DatabaseType

type Defaults

type Defaults map[DatabaseType]map[string]string

Defaults represents default value mappings per database type. It is a map from DatabaseType to a map of symbolic default keys to SQL expressions.

func (Defaults) ForProvider

func (d Defaults) ForProvider(dbType DatabaseType) map[string]string

ForProvider returns the defaults map for the given database type, or nil.

func (Defaults) SetForProvider

func (d Defaults) SetForProvider(dbType DatabaseType, defaults map[string]string)

SetForProvider sets the defaults map for the given database type. The receiver must be a non-nil map (use make(Defaults) or Defaults{} first).

func (*Defaults) UnmarshalYAML

func (d *Defaults) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements custom YAML unmarshaling so that YAML keys (lowercase strings like "postgresql") are converted to DatabaseType keys.

type Field

type Field struct {
	Name       string      `yaml:"name"`
	Type       string      `yaml:"type"`
	PrimaryKey bool        `yaml:"primary_key,omitempty"`
	Nullable   *bool       `yaml:"nullable,omitempty"`
	Default    string      `yaml:"default,omitempty"`
	Length     int         `yaml:"length,omitempty"`
	Precision  int         `yaml:"precision,omitempty"`
	Scale      int         `yaml:"scale,omitempty"`
	AutoCreate bool        `yaml:"auto_create,omitempty"`
	AutoUpdate bool        `yaml:"auto_update,omitempty"`
	ForeignKey *ForeignKey `yaml:"foreign_key,omitempty"`
	ManyToMany *ManyToMany `yaml:"many_to_many,omitempty"`
}

Field represents a database field/column definition

func (*Field) IsNullable

func (f *Field) IsNullable() bool

IsNullable returns the nullable value, defaulting to true if not set

func (*Field) SetNullable

func (f *Field) SetNullable(nullable bool)

SetNullable sets the nullable field

func (*Field) Validate

func (f *Field) Validate() error

Validate validates the field structure

type ForeignKey

type ForeignKey struct {
	Table    string `yaml:"table"`
	OnDelete string `yaml:"on_delete"`
	OnUpdate string `yaml:"on_update,omitempty"`
}

ForeignKey represents a foreign key relationship

type Include

type Include struct {
	Module string `yaml:"module"`
	Path   string `yaml:"path"`
}

Include represents an external schema to include

func (*Include) Validate

func (i *Include) Validate() error

Validate validates the include structure

type Index

type Index struct {
	Name   string   `yaml:"name"`
	Fields []string `yaml:"fields"`
	Unique bool     `yaml:"unique,omitempty"`
	// Method specifies the index access method (e.g. BTREE, HASH, GIN, GIST, BRIN for PostgreSQL).
	// Leave empty to use the database default (usually BTREE).
	// Not supported by SQLite or SQL Server — silently ignored on those providers.
	Method string `yaml:"method,omitempty"`
	// Where is a partial index predicate expression. Only rows satisfying this
	// condition are included in the index. Leave empty for a full index.
	// Not supported by MySQL — silently ignored on that provider.
	Where string `yaml:"where,omitempty"`
	// ForeignKey is an informational annotation (e.g. "auth_user.id") indicating
	// which FK relationship this index supports. Generated by Builder tooling.
	// It does not affect SQL generation.
	ForeignKey string `yaml:"foreign_key,omitempty"`
	// FromFK marks indexes auto-generated for foreign key columns. These are
	// managed automatically by AddForeignKey/DropForeignKey and excluded from
	// schema diffs and generated migration code.
	FromFK bool `yaml:"from_fk,omitempty"`
}

Index represents a database index definition

func (*Index) Validate

func (i *Index) Validate(table Table) error

Validate validates the index structure

type ManyToMany

type ManyToMany struct {
	Table string `yaml:"table"`
}

ManyToMany represents a many-to-many relationship

type Schema

type Schema struct {
	Database     Database     `yaml:"database"`
	Include      []Include    `yaml:"include,omitempty"`
	Defaults     Defaults     `yaml:"defaults"`
	TypeMappings TypeMappings `yaml:"type_mappings"`
	Tables       []Table      `yaml:"tables"`
}

Schema represents a YAML schema file structure

func (*Schema) GetTableByName

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

GetTableByName finds a table by name in the schema

func (*Schema) Validate

func (s *Schema) Validate() error

Validate validates the schema structure

type Table

type Table struct {
	Name    string  `yaml:"name"`
	Fields  []Field `yaml:"fields"`
	Indexes []Index `yaml:"indexes,omitempty"`
}

Table represents a database table definition

func (*Table) GetFieldByName

func (t *Table) GetFieldByName(name string) *Field

GetFieldByName finds a field by name in the table

func (*Table) GetPrimaryKeyField

func (t *Table) GetPrimaryKeyField() *Field

GetPrimaryKeyField returns the primary key field if it exists

func (*Table) HasPrimaryKey

func (t *Table) HasPrimaryKey() bool

HasPrimaryKey checks if the table has a primary key field

type TypeMappings

type TypeMappings map[DatabaseType]map[string]string

TypeMappings represents custom SQL type overrides per database. Values are SQL type strings. For parameterised types use Go template syntax: "DECIMAL({{.Precision}},{{.Scale}})" or "VARCHAR({{.Length}})". It is a map from DatabaseType to a map of abstract type names to SQL type strings.

func (TypeMappings) ForProvider

func (tm TypeMappings) ForProvider(dbType DatabaseType) map[string]string

ForProvider returns the type mapping overrides for the given database type, or nil.

func (TypeMappings) SetForProvider

func (tm TypeMappings) SetForProvider(dbType DatabaseType, mappings map[string]string)

SetForProvider sets the type mappings for the given database type. The receiver must be a non-nil map (use make(TypeMappings) or TypeMappings{} first).

func (*TypeMappings) UnmarshalYAML

func (tm *TypeMappings) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements custom YAML unmarshaling so that YAML keys (lowercase strings like "postgresql") are converted to DatabaseType keys.

Jump to

Keyboard shortcuts

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