sqlschema

package
v1.2.7 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2025 License: BSD-2-Clause Imports: 8 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyInspectorOptions

func ApplyInspectorOptions(cfg *InspectorConfig, options ...InspectorOption)

Types

type BaseColumn

type BaseColumn struct {
	Name            string
	SQLType         string
	VarcharLen      int
	DefaultValue    string
	IsNullable      bool
	IsAutoIncrement bool
	IsIdentity      bool
}

BaseColumn is a base column definition that stores various attributes of a column.

Dialects and only dialects can use it to implement the Column interface. Other packages must use the Column interface.

func (*BaseColumn) AppendQuery

func (c *BaseColumn) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error)

AppendQuery appends full SQL data type.

func (BaseColumn) GetDefaultValue

func (cd BaseColumn) GetDefaultValue() string

func (BaseColumn) GetIsAutoIncrement

func (cd BaseColumn) GetIsAutoIncrement() bool

func (BaseColumn) GetIsIdentity

func (cd BaseColumn) GetIsIdentity() bool

func (BaseColumn) GetIsNullable

func (cd BaseColumn) GetIsNullable() bool

func (BaseColumn) GetName

func (cd BaseColumn) GetName() string

func (BaseColumn) GetSQLType

func (cd BaseColumn) GetSQLType() string

func (BaseColumn) GetVarcharLen

func (cd BaseColumn) GetVarcharLen() int

type BaseDatabase

type BaseDatabase struct {
	Tables      *ordered.Map[string, Table]
	ForeignKeys map[ForeignKey]string
}

BaseDatabase is a base database definition.

Dialects and only dialects can use it to implement the Database interface. Other packages must use the Database interface.

func (BaseDatabase) GetForeignKeys

func (ds BaseDatabase) GetForeignKeys() map[ForeignKey]string

func (BaseDatabase) GetTables

func (ds BaseDatabase) GetTables() *ordered.Map[string, Table]

type BaseMigrator

type BaseMigrator struct {
	// contains filtered or unexported fields
}

BaseMigrator can be embeded by dialect's Migrator implementations to re-use some of the existing bun queries.

func NewBaseMigrator

func NewBaseMigrator(db *bun.DB) *BaseMigrator

func (*BaseMigrator) AppendCreateTable

func (m *BaseMigrator) AppendCreateTable(b []byte, model interface{}) ([]byte, error)

func (*BaseMigrator) AppendDropTable

func (m *BaseMigrator) AppendDropTable(b []byte, schemaName, tableName string) ([]byte, error)

type BaseTable

type BaseTable struct {
	Schema string
	Name   string

	// ColumnDefinitions map each column name to the column definition.
	Columns *ordered.Map[string, Column]

	// PrimaryKey holds the primary key definition.
	// A nil value means that no primary key is defined for the table.
	PrimaryKey *PrimaryKey

	// UniqueConstraints defined on the table.
	UniqueConstraints []Unique
}

BaseTable is a base table definition.

Dialects and only dialects can use it to implement the Table interface. Other packages must use the Table interface.

func (*BaseTable) GetColumns

func (td *BaseTable) GetColumns() *ordered.Map[string, Column]

func (*BaseTable) GetName

func (td *BaseTable) GetName() string

func (*BaseTable) GetPrimaryKey

func (td *BaseTable) GetPrimaryKey() *PrimaryKey

func (*BaseTable) GetSchema

func (td *BaseTable) GetSchema() string

func (*BaseTable) GetUniqueConstraints

func (td *BaseTable) GetUniqueConstraints() []Unique

type BunModelInspector

type BunModelInspector struct {
	InspectorConfig
	// contains filtered or unexported fields
}

BunModelInspector creates the current project state from the passed bun.Models. Do not recycle BunModelInspector for different sets of models, as older models will not be de-registerred before the next run.

func NewBunModelInspector

func NewBunModelInspector(tables *schema.Tables, options ...InspectorOption) *BunModelInspector

func (*BunModelInspector) Inspect

func (bmi *BunModelInspector) Inspect(ctx context.Context) (Database, error)

type BunModelSchema

type BunModelSchema struct {
	BaseDatabase

	Tables *ordered.Map[string, Table]
}

BunModelSchema is the schema state derived from bun table models.

func (BunModelSchema) GetTables

func (ms BunModelSchema) GetTables() *ordered.Map[string, Table]

type BunTable

type BunTable struct {
	BaseTable

	// Model stores the zero interface to the underlying Go struct.
	Model interface{}
}

BunTable provides additional table metadata that is only accessible from scanning bun models.

type Column

type Column interface {
	GetName() string
	GetSQLType() string
	GetVarcharLen() int
	GetDefaultValue() string
	GetIsNullable() bool
	GetIsAutoIncrement() bool
	GetIsIdentity() bool
	AppendQuery(schema.Formatter, []byte) ([]byte, error)
}

type ColumnReference

type ColumnReference struct {
	TableName string
	Column    Columns
}

func NewColumnReference

func NewColumnReference(tableName string, columns ...string) ColumnReference

type Columns

type Columns string

Columns is a hashable representation of []string used to define schema constraints that depend on multiple columns. Although having duplicated column references in these constraints is illegal, Columns neither validates nor enforces this constraint on the caller.

func NewColumns

func NewColumns(columns ...string) Columns

NewColumns creates a composite column from a slice of column names.

func (*Columns) AppendQuery

func (c *Columns) AppendQuery(fmter schema.Formatter, b []byte) ([]byte, error)

func (*Columns) Contains

func (c *Columns) Contains(other string) bool

Contains checks that a composite column contains the current column.

func (*Columns) ContainsColumns

func (c *Columns) ContainsColumns(other Columns) bool

ContainsColumns checks that columns in "other" are a subset of current colums.

func (*Columns) Replace

func (c *Columns) Replace(oldColumn, newColumn string) bool

Replace renames a column if it is part of the composite. If a composite consists of multiple columns, only one column will be renamed.

func (*Columns) Split

func (c *Columns) Split() []string

Split returns a slice of column names that make up the composite.

func (*Columns) String

func (c *Columns) String() string

type Database

type Database interface {
	GetTables() *ordered.Map[string, Table]
	GetForeignKeys() map[ForeignKey]string
}

type ForeignKey

type ForeignKey struct {
	From ColumnReference
	To   ColumnReference
}

func (ForeignKey) DependsOnColumn

func (fk ForeignKey) DependsOnColumn(tableName string, column string) bool

func (ForeignKey) DependsOnTable

func (fk ForeignKey) DependsOnTable(tableName string) bool

type Inspector

type Inspector interface {
	Inspect(ctx context.Context) (Database, error)
}

Inspector reads schema state.

func NewInspector

func NewInspector(db *bun.DB, options ...InspectorOption) (Inspector, error)

NewInspector creates a new database inspector, if the dialect supports it.

type InspectorConfig

type InspectorConfig struct {
	// SchemaName limits inspection to tables in a particular schema.
	SchemaName string

	// ExcludeTables from inspection.
	ExcludeTables []string
}

InspectorConfig controls the scope of migration by limiting the objects Inspector should return. Inspectors SHOULD use the configuration directly instead of copying it, or MAY choose to embed it, to make sure options are always applied correctly.

type InspectorDialect

type InspectorDialect interface {
	schema.Dialect

	// Inspector returns a new instance of Inspector for the dialect.
	// Dialects MAY set their default InspectorConfig values in constructor
	// but MUST apply InspectorOptions to ensure they can be overriden.
	//
	// Use ApplyInspectorOptions to reduce boilerplate.
	NewInspector(db *bun.DB, options ...InspectorOption) Inspector

	// CompareType returns true if col1 and co2 SQL types are equivalent,
	// i.e. they might use dialect-specifc type aliases (SERIAL ~ SMALLINT)
	// or specify the same VARCHAR length differently (VARCHAR(255) ~ VARCHAR).
	CompareType(Column, Column) bool
}

type InspectorOption

type InspectorOption func(*InspectorConfig)

func WithExcludeTables

func WithExcludeTables(tables ...string) InspectorOption

WithExcludeTables works in append-only mode, i.e. tables cannot be re-included.

func WithSchemaName

func WithSchemaName(schemaName string) InspectorOption

type Migrator

type Migrator interface {
	AppendSQL(b []byte, operation interface{}) ([]byte, error)
}

func NewMigrator

func NewMigrator(db *bun.DB, schemaName string) (Migrator, error)

type MigratorDialect

type MigratorDialect interface {
	schema.Dialect
	NewMigrator(db *bun.DB, schemaName string) Migrator
}

type PrimaryKey

type PrimaryKey struct {
	Name    string
	Columns Columns
}

PrimaryKey represents a primary key constraint defined on 1 or more columns.

type Table

type Table interface {
	GetSchema() string
	GetName() string
	GetColumns() *ordered.Map[string, Column]
	GetPrimaryKey() *PrimaryKey
	GetUniqueConstraints() []Unique
}

type Unique

type Unique struct {
	Name    string
	Columns Columns
}

Unique represents a unique constraint defined on 1 or more columns.

func (Unique) Equals

func (u Unique) Equals(other Unique) bool

Equals checks that two unique constraint are the same, assuming both are defined for the same table.

Jump to

Keyboard shortcuts

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