Documentation ¶
Overview ¶
Package schema contains all schema migration logic for SQL dialects.
Index ¶
- Constants
- type Applier
- type ApplyFunc
- type ApplyHook
- type ChangeKind
- type Column
- type CreateFunc
- type Creator
- type DiffFunc
- type DiffHook
- type Differ
- type ForeignKey
- type Hook
- type Index
- type Indexes
- type InspectOption
- type Inspector
- type Migrate
- type MigrateOption
- func WithApplyHook(hooks ...ApplyHook) MigrateOption
- func WithAtlas(b bool) MigrateOption
- func WithDiffHook(hooks ...DiffHook) MigrateOption
- func WithDir(dir migrate.Dir) MigrateOption
- func WithDropColumn(b bool) MigrateOption
- func WithDropIndex(b bool) MigrateOption
- func WithFixture(b bool) MigrateOption
- func WithForeignKeys(b bool) MigrateOption
- func WithFormatter(fmt migrate.Formatter) MigrateOption
- func WithGlobalUniqueID(b bool) MigrateOption
- func WithHooks(hooks ...Hook) MigrateOption
- func WithSkipChanges(skip ChangeKind) MigrateOption
- type MySQL
- type Postgres
- type ReferenceOption
- type SQLite
- type Table
- func (t *Table) AddColumn(c *Column) *Table
- func (t *Table) AddForeignKey(fk *ForeignKey) *Table
- func (t *Table) AddIndex(name string, unique bool, columns []string) *Table
- func (t *Table) AddPrimary(c *Column) *Table
- func (t *Table) HasColumn(name string) bool
- func (t *Table) Index(name string) (*Index, bool)
- func (t *Table) SetAnnotation(ant *entsql.Annotation) *Table
- type WriteDriver
Constants ¶
const ( // TypeTable defines the table name holding the type information. TypeTable = "ent_types" // MaxTypes defines the max number of types can be created when // defining universal ids. The left 16-bits are reserved. MaxTypes = math.MaxUint16 )
const ( // DefaultStringLen describes the default length for string/varchar types. DefaultStringLen int64 = 255 // Null is the string representation of NULL in SQL. Null = "NULL" // PrimaryKey is the string representation of PKs in SQL. PrimaryKey = "PRI" // UniqueKey is the string representation of PKs in SQL. UniqueKey = "UNI" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Applier ¶
type Applier interface { // Apply applies the given migrate.Plan on the database. Apply(context.Context, dialect.ExecQuerier, *migrate.Plan) error }
Applier is the interface that wraps the Apply method.
type ApplyFunc ¶
The ApplyFunc type is an adapter to allow the use of ordinary function as Applier. If f is a function with the appropriate signature, ApplyFunc(f) is an Applier that calls f.
type ApplyHook ¶
ApplyHook defines the "migration applying middleware". A function that gets an Applier and returns an Applier.
type ChangeKind ¶
type ChangeKind uint
A ChangeKind denotes the kind of schema change.
const ( NoChange ChangeKind = 0 AddSchema ChangeKind = 1 << (iota - 1) ModifySchema DropSchema AddTable ModifyTable DropTable AddColumn ModifyColumn DropColumn AddIndex ModifyIndex DropIndex AddForeignKey ModifyForeignKey DropForeignKey AddCheck ModifyCheck DropCheck )
List of change types.
func (ChangeKind) Is ¶
func (k ChangeKind) Is(c ChangeKind) bool
Is reports whether c is match the given change kind.
type Column ¶
type Column struct { Name string // column name. Type field.Type // column type. SchemaType map[string]string // optional schema type per dialect. Attr string // extra attributes. Size int64 // max size parameter for string, blob, etc. Key string // key definition (PRI, UNI or MUL). Unique bool // column with unique constraint. Increment bool // auto increment attribute. Nullable bool // null or not null attribute. Default interface{} // default value. Enums []string // enum values. Collation string // collation type (utf8mb4_unicode_ci, utf8mb4_general_ci) // contains filtered or unexported fields }
Column schema definition for SQL dialects.
func (*Column) ConvertibleTo ¶
ConvertibleTo reports whether a column can be converted to the new column without altering its data.
func (*Column) PrimaryKey ¶
PrimaryKey returns boolean indicates if this column is on of the primary key columns. Used by the migration tool when parsing the `DESCRIBE TABLE` output Go objects.
func (*Column) ScanDefault ¶
ScanDefault scans the default value string to its interface type.
type CreateFunc ¶
The CreateFunc type is an adapter to allow the use of ordinary function as Creator. If f is a function with the appropriate signature, CreateFunc(f) is a Creator that calls f.
type Creator ¶
type Creator interface { // Create creates the given tables in the database. See Migrate.Create for more details. Create(context.Context, ...*Table) error }
Creator is the interface that wraps the Create method.
type DiffFunc ¶
The DiffFunc type is an adapter to allow the use of ordinary function as Differ. If f is a function with the appropriate signature, DiffFunc(f) is a Differ that calls f.
type DiffHook ¶
DiffHook defines the "diff middleware". A function that gets a Differ and returns a Differ.
type Differ ¶
type Differ interface { // Diff returns a list of changes that construct a migration plan. Diff(current, desired *schema.Schema) ([]schema.Change, error) }
Differ is the interface that wraps the Diff method.
type ForeignKey ¶
type ForeignKey struct { Symbol string // foreign-key name. Generated if empty. Columns []*Column // table column RefTable *Table // referenced table. RefColumns []*Column // referenced columns. OnUpdate ReferenceOption // action on update. OnDelete ReferenceOption // action on delete. }
ForeignKey definition for creation.
func (ForeignKey) DSL ¶
func (fk ForeignKey) DSL() *sql.ForeignKeyBuilder
DSL returns a default DSL query for a foreign-key.
type Hook ¶
Hook defines the "create middleware". A function that gets a Creator and returns a Creator. For example:
hook := func(next schema.Creator) schema.Creator { return schema.CreateFunc(func(ctx context.Context, tables ...*schema.Table) error { fmt.Println("Tables:", tables) return next.Create(ctx, tables...) }) }
type Index ¶
type Index struct { Name string // index name. Unique bool // uniqueness. Columns []*Column // actual table columns. Annotation *entsql.IndexAnnotation // index annotation. // contains filtered or unexported fields }
Index definition for table index.
func (*Index) Builder ¶
func (i *Index) Builder(table string) *sql.IndexBuilder
Builder returns the query builder for index creation. The DSL is identical in all dialects.
func (*Index) DropBuilder ¶
func (i *Index) DropBuilder(table string) *sql.DropIndexBuilder
DropBuilder returns the query builder for the drop index.
type Indexes ¶
type Indexes []*Index
Indexes used for scanning all sql.Rows into a list of indexes, because multiple sql rows can represent the same index (multi-columns indexes).
type InspectOption ¶
type InspectOption func(inspect *Inspector)
InspectOption allows for managing schema configuration using functional options.
func WithSchema ¶
func WithSchema(schema string) InspectOption
WithSchema provides a schema (named-database) for reading the tables from.
type Inspector ¶
type Inspector struct {
// contains filtered or unexported fields
}
An Inspector provides methods for inspecting database tables.
func NewInspect ¶
func NewInspect(d dialect.Driver, opts ...InspectOption) (*Inspector, error)
NewInspect returns an inspector for the given SQL driver.
type Migrate ¶
type Migrate struct {
// contains filtered or unexported fields
}
Migrate runs the migrations logic for the SQL dialects.
func NewMigrate ¶
func NewMigrate(d dialect.Driver, opts ...MigrateOption) (*Migrate, error)
NewMigrate create a migration structure for the given SQL driver.
func (*Migrate) Create ¶
Create creates all schema resources in the database. It works in an "append-only" mode, which means, it only creates tables, appends columns to tables or modifies column types.
Column can be modified by turning into a NULL from NOT NULL, or having a type conversion not resulting data altering. From example, changing varchar(255) to varchar(120) is invalid, but changing varchar(120) to varchar(255) is valid. For more info, see the convert function below.
Note that SQLite dialect does not support (this moment) the "append-only" mode describe above, since it's used only for testing.
type MigrateOption ¶
type MigrateOption func(*Migrate)
MigrateOption allows for managing schema configuration using functional options.
func WithApplyHook ¶
func WithApplyHook(hooks ...ApplyHook) MigrateOption
WithApplyHook adds a list of ApplyHook to the schema migration.
schema.WithApplyHook(func(next schema.Applier) schema.Applier { return schema.ApplyFunc(func(ctx context.Context, conn dialect.ExecQuerier, plan *migrate.Plan) error { // Example to hook into the apply process, or implement // a custom applier. // // for _, c := range plan.Changes { // fmt.Printf("%s: %s", c.Comment, c.Cmd) // } // return next.Apply(ctx, conn, plan) }) })
func WithAtlas ¶
func WithAtlas(b bool) MigrateOption
WithAtlas is an opt-in option for v0.10 indicates the migration should be executed using Atlas engine (i.e. https://atlasgo.io). Note, in future versions, this option is going to be replaced from opt-in to opt-out and the deprecation of this package.
func WithDiffHook ¶
func WithDiffHook(hooks ...DiffHook) MigrateOption
WithDiffHook adds a list of DiffHook to the schema migration.
schema.WithDiffHook(func(next schema.Differ) schema.Differ { return schema.DiffFunc(func(current, desired *atlas.Schema) ([]atlas.Change, error) { // Code before standard diff. changes, err := next.Diff(current, desired) if err != nil { return nil, err } // After diff, you can filter // changes or return new ones. return changes, nil }) })
func WithDir ¶
func WithDir(dir migrate.Dir) MigrateOption
WithDir sets the atlas migration directory to use to store migration files.
func WithDropColumn ¶
func WithDropColumn(b bool) MigrateOption
WithDropColumn sets the columns dropping option to the migration. Defaults to false.
func WithDropIndex ¶
func WithDropIndex(b bool) MigrateOption
WithDropIndex sets the indexes dropping option to the migration. Defaults to false.
func WithFixture ¶
func WithFixture(b bool) MigrateOption
WithFixture sets the foreign-key renaming option to the migration when upgrading ent from v0.1.0 (issue-#285). Defaults to false.
func WithForeignKeys ¶
func WithForeignKeys(b bool) MigrateOption
WithForeignKeys enables creating foreign-key in ddl. Defaults to true.
func WithFormatter ¶
func WithFormatter(fmt migrate.Formatter) MigrateOption
WithFormatter sets atlas formatter to use to write changes to migration files.
func WithGlobalUniqueID ¶
func WithGlobalUniqueID(b bool) MigrateOption
WithGlobalUniqueID sets the universal ids options to the migration. Defaults to false.
func WithHooks ¶
func WithHooks(hooks ...Hook) MigrateOption
WithHooks adds a list of hooks to the schema migration.
func WithSkipChanges ¶
func WithSkipChanges(skip ChangeKind) MigrateOption
WithSkipChanges allows skipping/filtering list of changes returned by the Differ before executing migration planning.
SkipChanges(schema.DropTable|schema.DropColumn)
type ReferenceOption ¶
type ReferenceOption string
ReferenceOption for constraint actions.
const ( NoAction ReferenceOption = "NO ACTION" Restrict ReferenceOption = "RESTRICT" Cascade ReferenceOption = "CASCADE" SetNull ReferenceOption = "SET NULL" SetDefault ReferenceOption = "SET DEFAULT" )
Reference options.
func (ReferenceOption) ConstName ¶
func (r ReferenceOption) ConstName() string
ConstName returns the constant name of a reference option. It's used by entc for printing the constant name in templates.
type Table ¶
type Table struct { Name string Columns []*Column Indexes []*Index PrimaryKey []*Column ForeignKeys []*ForeignKey Annotation *entsql.Annotation // contains filtered or unexported fields }
Table schema definition for SQL dialects.
func (*Table) AddForeignKey ¶
func (t *Table) AddForeignKey(fk *ForeignKey) *Table
AddForeignKey adds a foreign key to the table.
func (*Table) AddPrimary ¶
AddPrimary adds a new primary key to the table.
func (*Table) SetAnnotation ¶
func (t *Table) SetAnnotation(ant *entsql.Annotation) *Table
SetAnnotation the entsql.Annotation on the table.
type WriteDriver ¶
type WriteDriver struct { dialect.Driver // underlying driver. io.Writer // target for exec statements. }
WriteDriver is a driver that writes all driver exec operations to its writer.
func (*WriteDriver) Commit ¶
func (w *WriteDriver) Commit() error
Commit writes the transaction commit.
func (*WriteDriver) Exec ¶
func (w *WriteDriver) Exec(_ context.Context, query string, _, _ interface{}) error
Exec writes its query and calls the underlying driver Exec method.
func (*WriteDriver) Rollback ¶
func (w *WriteDriver) Rollback() error
Rollback writes the transaction rollback.