Skip to content

Commit c2ff3ff

Browse files
authored
dialect/entsql: add schema to package annotation (ent#3817)
1 parent 616ccf7 commit c2ff3ff

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

dialect/entsql/annotation.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ import "entgo.io/ent/schema"
99
// Annotation is a builtin schema annotation for attaching
1010
// SQL metadata to schema objects for both codegen and runtime.
1111
type Annotation struct {
12+
// The Schema option allows setting the schema which the table belongs to.
13+
// Note, this option is no-op for Ent default migration engine. However, schema
14+
// extensions (like Atlas) can accept this option and implement it accordingly.
15+
//
16+
// entsql.Annotation{
17+
// Schema: "public",
18+
// }
19+
//
20+
Schema string `json:"schema,omitempty"`
21+
1222
// The Table option allows overriding the default table
1323
// name that is generated by ent. For example:
1424
//
@@ -146,6 +156,21 @@ func (Annotation) Name() string {
146156
return "EntSQL"
147157
}
148158

159+
// The Schema option allows setting the schema which the table belongs to.
160+
// Note, this option is no-op for Ent default migration engine. However, schema
161+
// extensions (like Atlas) can accept this option and implement it accordingly.
162+
//
163+
// func (T) Annotations() []schema.Annotation {
164+
// return []schema.Annotation{
165+
// entsql.Schema("public"),
166+
// }
167+
// }
168+
func Schema(s string) *Annotation {
169+
return &Annotation{
170+
Schema: s,
171+
}
172+
}
173+
149174
// The Table option allows overriding the default table
150175
// name that is generated by ent. For example:
151176
//
@@ -160,6 +185,14 @@ func Table(t string) *Annotation {
160185
}
161186
}
162187

188+
// SchemaTable allows setting both schema and table name in one annotation.
189+
func SchemaTable(s, t string) *Annotation {
190+
return &Annotation{
191+
Schema: s,
192+
Table: t,
193+
}
194+
}
195+
163196
// Check allows injecting custom "DDL" for setting an unnamed "CHECK" clause in "CREATE TABLE".
164197
//
165198
// entsql.Annotation{
@@ -273,6 +306,9 @@ func (a Annotation) Merge(other schema.Annotation) schema.Annotation {
273306
default:
274307
return a
275308
}
309+
if s := ant.Schema; s != "" {
310+
a.Schema = s
311+
}
276312
if t := ant.Table; t != "" {
277313
a.Table = t
278314
}

0 commit comments

Comments
 (0)