drivers

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package drivers talks to various database backends and retrieves table, column, type, and foreign key information

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColumnDBTypes

func ColumnDBTypes(cols []Column) map[string]string

ColumnDBTypes of the columns.

func ColumnNames

func ColumnNames(cols []Column) []string

ColumnNames of the columns.

func ColumnsFromList

func ColumnsFromList(list []string, tablename string) []string

ColumnsFromList takes a whitelist or blacklist and returns the columns for a given table.

func Skip added in v0.15.0

func Skip(name string, include, exclude []string) bool

Types

type Capabilities added in v0.15.0

type Capabilities struct{}

type Column

type Column struct {
	Name      string `json:"name" yaml:"name" toml:"name"`
	DBType    string `json:"db_type" yaml:"db_type" toml:"db_type"`
	Default   string `json:"default" yaml:"default" toml:"default"`
	Comment   string `json:"comment" yaml:"comment" toml:"comment"`
	Nullable  bool   `json:"nullable" yaml:"nullable" toml:"nullable"`
	Generated bool   `json:"generated" yaml:"generated" toml:"generated"`
	AutoIncr  bool   `json:"autoincr" yaml:"autoincr" toml:"autoincr"`

	// DomainName is the domain type name associated to the column. See here:
	// https://www.postgresql.org/docs/16/extend-type-system.html
	DomainName string `json:"domain_name" yaml:"domain_name" toml:"domain_name"`

	Type string `json:"type" yaml:"type" toml:"type"`
}

Column holds information about a database column. Types are Go types, converted by TranslateColumnType.

type ColumnFilter

type ColumnFilter map[string]Filter

func ParseColumnFilter

func ParseColumnFilter(tables []string, only, except map[string][]string) ColumnFilter

type Constraint

type Constraint struct {
	Name    string   `yaml:"name" json:"name"`
	Columns []string `yaml:"columns" json:"columns"`
}

Constraint represents a primary key constraint in a database

type Constraints added in v0.23.0

type Constraints struct {
	Primary *PrimaryKey  `yaml:"primary" json:"primary"`
	Foreign []ForeignKey `yaml:"foreign" json:"foreign"`
	Uniques []Constraint `yaml:"uniques" json:"uniques"`
}

type Constructor

type Constructor interface {
	// Load all constraints in the database, keyed by TableInfo.Key
	Constraints(context.Context, ColumnFilter) (DBConstraints, error)

	// Load basic info about all tables
	TablesInfo(context.Context, Filter) (TablesInfo, error)
	// Load details about a single table
	TableDetails(ctx context.Context, info TableInfo, filter ColumnFilter) (schema, name string, _ []Column, _ error)
}

Constructor breaks down the functionality required to implement a driver such that the drivers.Tables method can be used to reduce duplication in driver implementations.

type DBConstraints

type DBConstraints struct {
	PKs     map[string]*PrimaryKey
	FKs     map[string][]ForeignKey
	Uniques map[string][]Constraint
}

type DBInfo

type DBInfo[T any] struct {
	Tables    []Table `json:"tables"`
	Enums     []Enum  `json:"enums"`
	ExtraInfo T       `json:"extra_info"`
}

DBInfo is the database's table data and dialect.

type Enum added in v0.15.0

type Enum struct {
	Type   string
	Values []string
}

type Filter

type Filter struct {
	Only   []string
	Except []string
}

func ParseTableFilter added in v0.15.0

func ParseTableFilter(only, except map[string][]string) Filter

type ForeignKey

type ForeignKey struct {
	Name           string   `yaml:"name" json:"name"`
	Columns        []string `yaml:"columns" json:"columns"`
	ForeignTable   string   `yaml:"foreign_table" json:"foreign_table"`
	ForeignColumns []string `yaml:"foreign_columns" json:"foreign_columns"`
}

ForeignKey represents a foreign key constraint in a database

type Interface

type Interface[T any] interface {
	// The dialect
	Dialect() string
	// What the driver is capable of
	Capabilities() Capabilities
	// Assemble the database information into a nice struct
	Assemble(ctx context.Context) (*DBInfo[T], error)
	// Custom types defined by the driver
	Types() Types
}

Interface abstracts either a side-effect imported driver or a binary that is called in order to produce the data required for generation.

type PrimaryKey

type PrimaryKey = Constraint

PrimaryKey represents a primary key constraint in a database

type Table

type Table struct {
	Key string `yaml:"key" json:"key"`
	// For dbs with real schemas, like Postgres.
	// Example value: "schema_name"."table_name"
	Schema  string   `yaml:"schema" json:"schema"`
	Name    string   `yaml:"name" json:"name"`
	Columns []Column `yaml:"columns" json:"columns"`

	Constraints Constraints `yaml:"constraints" json:"constraints"`
}

Table metadata from the database schema.

func BuildDBInfo added in v0.15.0

func BuildDBInfo(ctx context.Context, c Constructor, concurrency int, only, except map[string][]string) ([]Table, error)

TablesConcurrently is a concurrent version of BuildDBInfo. It returns the metadata for all tables, minus the tables specified in the excludes.

func GetTable

func GetTable(tables []Table, name string) Table

GetTable by name. Panics if not found (for use in templates mostly).

func (Table) CanSoftDelete

func (t Table) CanSoftDelete(deleteColumn string) bool

func (Table) GetColumn

func (t Table) GetColumn(name string) Column

GetColumn by name. Panics if not found (for use in templates mostly).

func (Table) NonGeneratedColumns added in v0.22.0

func (t Table) NonGeneratedColumns() []Column

type TableInfo added in v0.15.0

type TableInfo struct {
	Key    string
	Schema string
	Name   string
}

type TablesInfo added in v0.15.0

type TablesInfo []TableInfo

func (TablesInfo) Keys added in v0.15.0

func (t TablesInfo) Keys() []string

type Type added in v0.24.0

type Type struct {
	Imports importers.List `yaml:"imports"`
	// To be used in factory.random[T]
	// a variable `f` of type `faker.Faker` is available
	// since this is in a generic function, the final return should be like
	// return any(yourVariableOrExpressions).(T)
	RandomExpr string `yaml:"random_expr"`
	// Additional imports for the randomize expression
	RandomExprImports importers.List `yaml:"random_expr_imports"`
	// Set this to true if the randomization should not be tested
	// this is useful for low-cardinality types like bool
	NoRandomizationTest bool `yaml:"no_randomization_test"`
}

type Types added in v0.24.0

type Types map[string]Type

Jump to

Keyboard shortcuts

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