Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

entc/gen: skip Table() predicate and warn about reserved schema name (Client) #2486

Merged
merged 4 commits into from Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/md/schema-def.md
Expand Up @@ -58,6 +58,11 @@ the root directory of your project, and can be generated by `entc` as follows:
go run entgo.io/ent/cmd/ent init User Group
```

:::note
Pleae note, that `Client` is a reserved Go struct and cannot be used as schema name. For database tables
named `client`, please use an annotation as mentioned [here](schema-annotations.md#custom-table-name).
:::
masseelch marked this conversation as resolved.
Show resolved Hide resolved

## It's Just Another ORM

If you are used to the definition of relations over edges, that's fine.
Expand Down
16 changes: 14 additions & 2 deletions entc/gen/graph.go
Expand Up @@ -142,8 +142,9 @@ func (f GenerateFunc) Generate(g *Graph) error {
func NewGraph(c *Config, schemas ...*load.Schema) (g *Graph, err error) {
defer catch(&err)
g = &Graph{c, make([]*Type, 0, len(schemas)), schemas}
for i := range schemas {
g.addNode(schemas[i])
for _, s := range schemas {
check(reservedName(s.Name), "add schema %q", s.Name)
g.addNode(s)
}
for i := range schemas {
g.addEdges(schemas[i])
Expand Down Expand Up @@ -438,6 +439,17 @@ func resolve(t *Type) error {
return nil
}

var reservedNames = []string{"Client"}

func reservedName(n string) error {
for _, rn := range reservedNames {
if rn == n {
return fmt.Errorf("%q is a reserved name and cannot be used as schema name", n)
}
}
return nil
}

// Tables returns the schema definitions of SQL tables for the graph.
func (g *Graph) Tables() (all []*schema.Table, err error) {
tables := make(map[string]*schema.Table)
Expand Down
3 changes: 3 additions & 0 deletions entc/gen/graph_test.go
Expand Up @@ -130,6 +130,9 @@ func TestNewGraph(t *testing.T) {
require.Equal(t2.Edges[5], t2.Edges[6].Ref)
require.Equal(map[string]string{"Name": "From"}, t2.Edges[5].Annotations["GQL"])
require.Equal(map[string]string{"Name": "To"}, t2.Edges[6].Annotations["GQL"])

_, err = NewGraph(&Config{Package: "entc/gen", Storage: drivers[0]}, &load.Schema{Name: "Client"})
require.EqualError(err, `entc/gen: add schema "Client": "Client" is a reserved name and cannot be used as schema name`)
}

func TestNewGraphRequiredLoop(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion entc/gen/template/where.tmpl
Expand Up @@ -41,7 +41,7 @@ func ID(id {{ $.ID.Type }}) predicate.{{ $.Name }} {
{{/* JSON cannot be compared using "=" and Enum has a type defined with the field name */}}
{{ $hasP := not (or $f.IsJSON $f.IsEnum) }}
{{ $comparable := or $f.ConvertedToBasic $f.Type.Valuer }}
{{ $undeclared := (and (ne $func "Label") (ne $func "Hooks") (ne $func "Policy")) }}
{{ $undeclared := (and (ne $func "Label") (ne $func "Hooks") (ne $func "Policy") (ne $func "Table")) }}
{{- if and $hasP $comparable $undeclared }}
{{ $arg := "v" }}
// {{ $func }} applies equality check predicate on the {{ quote $f.Name }} field. It's identical to {{ $func }}EQ.
Expand Down
12 changes: 12 additions & 0 deletions entc/integration/ent/comment.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions entc/integration/ent/comment/comment.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 125 additions & 0 deletions entc/integration/ent/comment/where.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions entc/integration/ent/comment_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.