Files
ent/entc/gen/template/migrate/schema.tmpl
Alex Snast 4c1f28d58f ent/schema: allow setting collation for string fields
Reviewed By: a8m

Differential Revision: D17090481

fbshipit-source-id: a08768c9bac4318a91bc6a067c85d6eb022c3024
2019-08-28 04:52:32 -07:00

112 lines
3.5 KiB
Cheetah

{{ define "schema" }}
{{- with extend $ "Package" "migrate" -}}
{{ template "header" . }}
{{ end }}
import (
{{ range $_, $n := $.Nodes }}
"{{ $.Config.Package }}/{{ $.Package }}"
{{ end }}
"github.com/facebookincubator/ent/schema/field"
"github.com/facebookincubator/ent/dialect/sql/schema"
)
var (
{{- range $_, $t := $.Tables }}
{{- /* find type node in the graph of this table, if exists */}}
{{- $node := index $.Nodes 0 }}
{{- range $_, $n := $.Nodes }}{{ if eq $t.Name $n.Table }}{{ $node = $n }}{{ end }}{{ end }}
{{- $columns := pascal $t.Name | printf "%sColumns" }}
// {{ $columns }} holds the columns for the "{{ $t.Name }}" table.
{{ $columns }} = []*schema.Column{
{{- range $_, $c := $t.Columns }}
{ Name: "{{ $c.Name }}", Type: field.{{ $c.Type.ConstName }},
{{- if $c.Unique }} Unique: true,{{ end }}
{{- if $c.Increment }} Increment: true,{{ end }}
{{- if $c.Nullable }} Nullable: {{ $c.Nullable }},{{ end }}
{{- with $c.Size }} Size: {{ . }},{{ end }}
{{- with $c.Attr }} Attr: "{{ . }}",{{ end }}
{{- if $c.HasDefault }} Default: {{ $node.Package }}.Default{{ pascal $c.Name }},{{ end }}
{{- with $c.Charset }} Charset: "{{ . }}",{{ end }}
{{- with $c.Collation }} Collation: "{{ . }}",{{ end }}},
{{- end }}
}
{{- $table := pascal $t.Name | printf "%sTable" }}
// {{ $table }} holds the schema information for the "{{ $t.Name }}" table.
{{ $table }} = &schema.Table{
Name: "{{ $t.Name }}",
Columns: {{ $columns }},
PrimaryKey: []*schema.Column{
{{- range $_, $pk := $t.PrimaryKey }}
{{- range $i, $c := $t.Columns }}
{{- if eq $pk.Name $c.Name }}{{ $columns }}[{{ $i }}],{{ end }}
{{- end }}
{{- end }}
},
ForeignKeys: []*schema.ForeignKey{
{{- range $_, $fk := $t.ForeignKeys }}
{
Symbol: "{{ $fk.Symbol }}",
Columns: []*schema.Column{
{{- range $_, $c1 := $fk.Columns }}
{{- range $i, $c2 := $t.Columns }}
{{- if eq $c1.Name $c2.Name }}{{ $columns }}[{{ $i }}],{{ end }}
{{- end }}
{{- end }}
},
{{/* postpone refrencing to avoid typechecking loops */}}
RefColumns: []*schema.Column{
{{- range $_, $c1 := $fk.RefColumns }}
{{- range $i, $c2 := $fk.RefTable.Columns }}
{{- if eq $c1.Name $c2.Name }}{{ pascal $fk.RefTable.Name | printf "%sColumns" }}[{{ $i }}],{{ end }}
{{- end }}
{{- end }}
},
{{- with $fk.OnUpdate.ConstName }}
OnUpdate: schema.{{ . }},
{{- end }}
{{- with $fk.OnDelete.ConstName }}
OnDelete: schema.{{ . }},
{{- end }}
},
{{- end }}
},
{{- if $t.Indexes }}
Indexes: []*schema.Index{
{{- range $_, $idx := $t.Indexes }}
{
Name: "{{ $idx.Name }}",
Unique: {{ $idx.Unique }},
Columns: []*schema.Column{
{{- range $_, $c1 := $idx.Columns }}
{{- range $i, $c2 := $t.Columns }}
{{- if eq $c1.Name $c2.Name }}{{ $columns }}[{{ $i }}],{{ end }}
{{- end }}
{{- end }}
},
},
{{- end }}
},
{{- end }}
}
{{- end }}
// Tables holds all the tables in the schema.
Tables = []*schema.Table{
{{- range $_, $t := $.Tables }}
{{ pascal $t.Name | printf "%sTable" }},
{{- end }}
}
)
func init() {
{{- range $_, $t := $.Tables }}
{{- $table := pascal $t.Name | printf "%sTable" }}
{{- range $i, $fk := $t.ForeignKeys }}
{{ $table }}.ForeignKeys[{{ $i }}].RefTable = {{ pascal $fk.RefTable.Name | printf "%sTable" }}
{{- end }}
{{- end }}
}
{{ end }}