Skip to content

Commit

Permalink
dialect/entsql: add schema to package annotation (ent#3817)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored Nov 10, 2023
1 parent 616ccf7 commit c2ff3ff
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions dialect/entsql/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import "entgo.io/ent/schema"
// Annotation is a builtin schema annotation for attaching
// SQL metadata to schema objects for both codegen and runtime.
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:
//
Expand Down Expand Up @@ -146,6 +156,21 @@ func (Annotation) Name() string {
return "EntSQL"
}

// 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 Schema(s string) *Annotation {
return &Annotation{
Schema: s,
}
}

// The Table option allows overriding the default table
// name that is generated by ent. For example:
//
Expand All @@ -160,6 +185,14 @@ func Table(t string) *Annotation {
}
}

// SchemaTable allows setting both schema and table name in one annotation.
func SchemaTable(s, t string) *Annotation {
return &Annotation{
Schema: s,
Table: t,
}
}

// Check allows injecting custom "DDL" for setting an unnamed "CHECK" clause in "CREATE TABLE".
//
// entsql.Annotation{
Expand Down Expand Up @@ -273,6 +306,9 @@ func (a Annotation) Merge(other schema.Annotation) schema.Annotation {
default:
return a
}
if s := ant.Schema; s != "" {
a.Schema = s
}
if t := ant.Table; t != "" {
a.Table = t
}
Expand Down

0 comments on commit c2ff3ff

Please sign in to comment.