Documentation ¶
Index ¶
- type Annotation
- func Check(c string) *Annotation
- func Checks(c map[string]string) *Annotation
- func Default(literal string) *Annotation
- func DefaultExpr(expr string) *Annotation
- func DefaultExprs(exprs map[string]string) *Annotation
- func OnDelete(opt ReferenceOption) *Annotation
- func Schema(s string) *Annotation
- func SchemaTable(s, t string) *Annotation
- func Table(t string) *Annotation
- func WithComments(b bool) *Annotation
- type IndexAnnotation
- func Desc() *IndexAnnotation
- func DescColumns(names ...string) *IndexAnnotation
- func IncludeColumns(names ...string) *IndexAnnotation
- func IndexType(t string) *IndexAnnotation
- func IndexTypes(types map[string]string) *IndexAnnotation
- func IndexWhere(pred string) *IndexAnnotation
- func OpClass(op string) *IndexAnnotation
- func OpClassColumn(name, op string) *IndexAnnotation
- func Prefix(prefix uint) *IndexAnnotation
- func PrefixColumn(name string, prefix uint) *IndexAnnotation
- type ReferenceOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Annotation ¶
type Annotation struct { // The Schema option allows setting the schema which the table belongs to. // Note, this option is no-op for Ent default migration engine. However, schema // extensions (like Atlas) can accept this option and implement it accordingly. // // entsql.Annotation{ // Schema: "public", // } // Schema string `json:"schema,omitempty"` // The Table option allows overriding the default table // name that is generated by ent. For example: // // entsql.Annotation{ // Table: "Users", // } // Table string `json:"table,omitempty"` // Charset defines the character-set of the table. For example: // // entsql.Annotation{ // Charset: "utf8mb4", // } // Charset string `json:"charset,omitempty"` // Collation defines the collation of the table (a set of rules for comparing // characters in a character set). For example: // // entsql.Annotation{ // Collation: "utf8mb4_bin", // } // Collation string `json:"collation,omitempty"` // Default specifies a literal default value of a column. Note that using // this option overrides the default behavior of the code-generation. // // entsql.Annotation{ // Default: `{"key":"value"}`, // } // Default string `json:"default,omitempty"` // DefaultExpr specifies an expression default value of a column. Using this option, // users can define custom expressions to be set as database default values. Note that // using this option overrides the default behavior of the code-generation. // // entsql.Annotation{ // DefaultExpr: "CURRENT_TIMESTAMP", // } // // entsql.Annotation{ // DefaultExpr: "uuid_generate_v4()", // } // // entsql.Annotation{ // DefaultExpr: "(a + b)", // } // DefaultExpr string `json:"default_expr,omitempty"` // DefaultExpr specifies an expression default value of a column per dialect. // See, DefaultExpr for full doc. // // entsql.Annotation{ // DefaultExprs: map[string]string{ // dialect.MySQL: "uuid()", // dialect.Postgres: "uuid_generate_v4", // } // DefaultExprs map[string]string `json:"default_exprs,omitempty"` // Options defines the additional table options. For example: // // entsql.Annotation{ // Options: "ENGINE = INNODB", // } // Options string `json:"options,omitempty"` // Size defines the column size in the generated schema. For example: // // entsql.Annotation{ // Size: 128, // } // Size int64 `json:"size,omitempty"` // WithComments specifies whether fields' comments should // be stored in the database schema as column comments. // // withCommentsEnabled := true // entsql.WithComments{ // WithComments: &withCommentsEnabled, // } // WithComments *bool `json:"with_comments,omitempty"` // Incremental defines the auto-incremental behavior of a column. For example: // // incrementalEnabled := true // entsql.Annotation{ // Incremental: &incrementalEnabled, // } // // By default, this value is nil defaulting to whatever best fits each scenario. // Incremental *bool `json:"incremental,omitempty"` // OnDelete specifies a custom referential action for DELETE operations on parent // table that has matching rows in the child table. // // For example, in order to delete rows from the parent table and automatically delete // their matching rows in the child table, pass the following annotation: // // entsql.Annotation{ // OnDelete: entsql.Cascade, // } // OnDelete ReferenceOption `json:"on_delete,omitempty"` // Check allows injecting custom "DDL" for setting an unnamed "CHECK" clause in "CREATE TABLE". // // entsql.Annotation{ // Check: "age < 10", // } // Check string `json:"check,omitempty"` // Checks allows injecting custom "DDL" for setting named "CHECK" clauses in "CREATE TABLE". // // entsql.Annotation{ // Checks: map[string]string{ // "valid_discount": "price > discount_price", // }, // } // Checks map[string]string `json:"checks,omitempty"` }
Annotation is a builtin schema annotation for attaching SQL metadata to schema objects for both codegen and runtime.
func Check ¶ added in v1.2.0
func Check(c string) *Annotation
Check allows injecting custom "DDL" for setting an unnamed "CHECK" clause in "CREATE TABLE".
entsql.Annotation{ Check: "(`age` < 10)", }
func Checks ¶ added in v1.2.0
func Checks(c map[string]string) *Annotation
Checks allows injecting custom "DDL" for setting named "CHECK" clauses in "CREATE TABLE".
entsql.Annotation{ Checks: map[string]string{ "valid_discount": "price > discount_price", }, }
func Default ¶ added in v1.2.0
func Default(literal string) *Annotation
Default specifies a literal default value of a column. Note that using this option overrides the default behavior of the code-generation.
entsql.Annotation{ Default: `{"key":"value"}`, }
func DefaultExpr ¶ added in v1.2.0
func DefaultExpr(expr string) *Annotation
DefaultExpr specifies an expression default value for the annotated column. Using this option, users can define custom expressions to be set as database default values.Note that using this option overrides the default behavior of the code-generation.
field.UUID("id", uuid.Nil). Default(uuid.New). Annotations( entsql.DefaultExpr("uuid_generate_v4()"), )
func DefaultExprs ¶ added in v1.2.0
func DefaultExprs(exprs map[string]string) *Annotation
DefaultExprs specifies an expression default value for the annotated column per dialect. See, DefaultExpr for full doc.
field.UUID("id", uuid.Nil). Default(uuid.New). Annotations( entsql.DefaultExprs(map[string]string{ dialect.MySQL: "uuid()", dialect.Postgres: "uuid_generate_v4()", }), )
func OnDelete ¶ added in v1.2.0
func OnDelete(opt ReferenceOption) *Annotation
OnDelete specifies a custom referential action for DELETE operations on parent table that has matching rows in the child table.
For example, in order to delete rows from the parent table and automatically delete their matching rows in the child table, pass the following annotation:
func (T) Annotations() []schema.Annotation { return []schema.Annotation{ entsql.OnDelete(entsql.Cascade), } }
func Schema ¶ added in v1.2.0
func Schema(s string) *Annotation
The Schema option allows setting the schema which the table belongs to. Note, this option is no-op for Ent default migration engine. However, schema extensions (like Atlas) can accept this option and implement it accordingly.
func (T) Annotations() []schema.Annotation { return []schema.Annotation{ entsql.Schema("public"), } }
func SchemaTable ¶ added in v1.2.0
func SchemaTable(s, t string) *Annotation
SchemaTable allows setting both schema and table name in one annotation.
func Table ¶ added in v1.2.0
func Table(t string) *Annotation
The Table option allows overriding the default table name that is generated by ent. For example:
func (T) Annotations() []schema.Annotation { return []schema.Annotation{ entsql.Table("Users"), } }
func WithComments ¶ added in v1.2.0
func WithComments(b bool) *Annotation
WithComments specifies whether fields' comments should be stored in the database schema as column comments.
func (T) Annotations() []schema.Annotation { return []schema.Annotation{ entsql.WithComments(true), } }
func (Annotation) Merge ¶
func (a Annotation) Merge(other schema.Annotation) schema.Annotation
Merge implements the schema.Merger interface.
type IndexAnnotation ¶
type IndexAnnotation struct { // Prefix defines a column prefix for a single string column index. // In MySQL, the following annotation maps to: // // index.Fields("column"). // Annotation(entsql.Prefix(100)) // // CREATE INDEX `table_column` ON `table`(`column`(100)) // Prefix uint // PrefixColumns defines column prefixes for a multi-column index. // In MySQL, the following annotation maps to: // // index.Fields("c1", "c2", "c3"). // Annotation( // entsql.PrefixColumn("c1", 100), // entsql.PrefixColumn("c2", 200), // ) // // CREATE INDEX `table_c1_c2_c3` ON `table`(`c1`(100), `c2`(200), `c3`) // PrefixColumns map[string]uint // Desc defines the DESC clause for a single column index. // In MySQL, the following annotation maps to: // // index.Fields("column"). // Annotation(entsql.Desc()) // // CREATE INDEX `table_column` ON `table`(`column` DESC) // Desc bool // DescColumns defines the DESC clause for columns in multi-column index. // In MySQL, the following annotation maps to: // // index.Fields("c1", "c2", "c3"). // Annotation( // entsql.DescColumns("c1", "c2"), // ) // // CREATE INDEX `table_c1_c2_c3` ON `table`(`c1` DESC, `c2` DESC, `c3`) // DescColumns map[string]bool // IncludeColumns defines the INCLUDE clause for the index. // Works only in Postgres and its definition is as follows: // // index.Fields("c1"). // Annotation( // entsql.IncludeColumns("c2"), // ) // // CREATE INDEX "table_column" ON "table"("c1") INCLUDE ("c2") // IncludeColumns []string // Type defines the type of the index. // In MySQL, the following annotation maps to: // // index.Fields("c1"). // Annotation( // entsql.IndexType("FULLTEXT"), // ) // // CREATE FULLTEXT INDEX `table_c1` ON `table`(`c1`) // Type string // Types is like the Type option but allows mapping an index-type per dialect. // // index.Fields("c1"). // Annotation( // entsql.IndexTypes(map[string]string{ // dialect.MySQL: "FULLTEXT", // dialect.Postgres: "GIN", // }), // ) // Types map[string]string // OpClass defines the operator class for a single string column index. // In PostgreSQL, the following annotation maps to: // // index.Fields("column"). // Annotation( // entsql.IndexType("BRIN"), // entsql.OpClass("int8_bloom_ops"), // ) // // CREATE INDEX "table_column" ON "table" USING BRIN ("column" int8_bloom_ops) // OpClass string // OpClassColumns defines operator-classes for a multi-column index. // In PostgreSQL, the following annotation maps to: // // index.Fields("c1", "c2", "c3"). // Annotation( // entsql.IndexType("BRIN"), // entsql.OpClassColumn("c1", "int8_bloom_ops"), // entsql.OpClassColumn("c2", "int8_minmax_multi_ops(values_per_range=8)"), // ) // // CREATE INDEX "table_column" ON "table" USING BRIN ("c1" int8_bloom_ops, "c2" int8_minmax_multi_ops(values_per_range=8), "c3") // OpClassColumns map[string]string // IndexWhere allows configuring partial indexes in SQLite and PostgreSQL. // Read more: https://postgresql.org/docs/current/indexes-partial.html. // // Note that the `WHERE` clause should be defined exactly like it is // stored in the database (i.e. normal form). Read more about this on // the Atlas website: https://atlasgo.io/concepts/dev-database#diffing. // // index.Fields("a"). // Annotations( // entsql.IndexWhere("b AND c > 0"), // ) // CREATE INDEX "table_a" ON "table"("a") WHERE (b AND c > 0) Where string }
IndexAnnotation is a builtin schema annotation for attaching SQL metadata to schema indexes for both codegen and runtime.
func Desc ¶
func Desc() *IndexAnnotation
Desc returns a new index annotation with the DESC clause for a single column index. In MySQL, the following annotation maps to:
index.Fields("column"). Annotation(entsql.Desc()) CREATE INDEX `table_column` ON `table`(`column` DESC)
func DescColumns ¶
func DescColumns(names ...string) *IndexAnnotation
DescColumns returns a new index annotation with the DESC clause attached to the columns in the index. In MySQL, the following annotation maps to:
index.Fields("c1", "c2", "c3"). Annotation( entsql.DescColumns("c1", "c2"), ) CREATE INDEX `table_c1_c2_c3` ON `table`(`c1` DESC, `c2` DESC, `c3`)
func IncludeColumns ¶ added in v1.2.0
func IncludeColumns(names ...string) *IndexAnnotation
IncludeColumns defines the INCLUDE clause for the index. Works only in Postgres and its definition is as follows:
index.Fields("c1"). Annotation( entsql.IncludeColumns("c2"), ) CREATE INDEX "table_column" ON "table"("c1") INCLUDE ("c2")
func IndexType ¶
func IndexType(t string) *IndexAnnotation
IndexType defines the type of the index. In MySQL, the following annotation maps to:
index.Fields("c1"). Annotation( entsql.IndexType("FULLTEXT"), ) CREATE FULLTEXT INDEX `table_c1` ON `table`(`c1`)
func IndexTypes ¶
func IndexTypes(types map[string]string) *IndexAnnotation
IndexTypes is like the Type option but allows mapping an index-type per dialect.
index.Fields("c1"). Annotations( entsql.IndexTypes(map[string]string{ dialect.MySQL: "FULLTEXT", dialect.Postgres: "GIN", }), )
func IndexWhere ¶ added in v1.2.0
func IndexWhere(pred string) *IndexAnnotation
IndexWhere allows configuring partial indexes in SQLite and PostgreSQL. Read more: https://postgresql.org/docs/current/indexes-partial.html.
Note that the `WHERE` clause should be defined exactly like it is stored in the database (i.e. normal form). Read more about this on the Atlas website: https://atlasgo.io/concepts/dev-database#diffing.
index.Fields("a"). Annotations( entsql.IndexWhere("b AND c > 0"), ) CREATE INDEX "table_a" ON "table"("a") WHERE (b AND c > 0)
func OpClass ¶ added in v1.2.0
func OpClass(op string) *IndexAnnotation
OpClass defines the operator class for a single string column index. In PostgreSQL, the following annotation maps to:
index.Fields("column"). Annotation( entsql.IndexType("BRIN"), entsql.OpClass("int8_bloom_ops"), ) CREATE INDEX "table_column" ON "table" USING BRIN ("column" int8_bloom_ops)
func OpClassColumn ¶ added in v1.2.0
func OpClassColumn(name, op string) *IndexAnnotation
OpClassColumn returns a new index annotation with column operator class for multi-column indexes. In PostgreSQL, the following annotation maps to:
index.Fields("c1", "c2", "c3"). Annotation( entsql.IndexType("BRIN"), entsql.OpClassColumn("c1", "int8_bloom_ops"), entsql.OpClassColumn("c2", "int8_minmax_multi_ops(values_per_range=8)"), ) CREATE INDEX "table_column" ON "table" USING BRIN ("c1" int8_bloom_ops, "c2" int8_minmax_multi_ops(values_per_range=8), "c3")
func Prefix ¶
func Prefix(prefix uint) *IndexAnnotation
Prefix returns a new index annotation with a single string column index. In MySQL, the following annotation maps to:
index.Fields("column"). Annotation(entsql.Prefix(100)) CREATE INDEX `table_column` ON `table`(`column`(100))
func PrefixColumn ¶
func PrefixColumn(name string, prefix uint) *IndexAnnotation
PrefixColumn returns a new index annotation with column prefix for multi-column indexes. In MySQL, the following annotation maps to:
index.Fields("c1", "c2", "c3"). Annotation( entsql.PrefixColumn("c1", 100), entsql.PrefixColumn("c2", 200), ) CREATE INDEX `table_c1_c2_c3` ON `table`(`c1`(100), `c2`(200), `c3`)
func (IndexAnnotation) Merge ¶
func (a IndexAnnotation) Merge(other schema.Annotation) schema.Annotation
Merge implements the schema.Merger interface.
func (IndexAnnotation) Name ¶
func (IndexAnnotation) Name() string
Name describes the annotation name.
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 (actions) specified by ON UPDATE and ON DELETE subclauses of the FOREIGN KEY clause.