-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathxo.xo.sql.tpl
More file actions
65 lines (63 loc) · 1.89 KB
/
xo.xo.sql.tpl
File metadata and controls
65 lines (63 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{{ define "header" -}}
{{- $s := .Data -}}
-- Generated by xo for the {{ .Data.Name }} schema.
{{ end }}
{{ define "createdb" -}}
{{- $s := .Data -}}
{{ if and $s.Enums (driver "postgres") }}
{{ range $e := $s.Enums }}
-- enum {{ $e.Name }}
CREATE TYPE {{ esc $e.Name }} AS ENUM (
{{- range $i, $v := $e.Values }}
{{ literal $v.Name }}{{ comma $i $e.Values }}
{{- end }}
);
{{ end -}}
{{- end -}}
{{- if $s.Tables }}
{{- range $t := $s.Tables }}
-- table {{ $t.Name }}
{{- if eq $t.Partition.Reference ""}}
CREATE TABLE {{ esc $t.Name }} (
{{- range $i, $c := $t.Columns }}
{{ coldef $t $c }}{{ comma $i $t.Columns }}
{{- end -}}
{{- range $idx := $t.Indexes -}}{{- if isEndConstraint $idx }},
{{ constraint $idx.Name -}} {{ if $idx.IsPrimary }}PRIMARY KEY{{ else }}UNIQUE{{ end }} ({{ fields $idx.Fields }})
{{- end -}}{{- end -}}
{{- range $fk := $t.ForeignKeys -}}{{- if gt (len $fk.Fields) 1 }},
{{ constraint $fk.Name -}} FOREIGN KEY ({{ fields $fk.Fields }}) REFERENCES {{ esc $fk.RefTable }} ({{ fields $fk.RefFields }})
{{- end -}}{{- end }}
)
{{- if $t.Partition.Definition }}
PARTITION BY {{$t.Partition.Definition}}
{{- end -}}
{{- else }}
CREATE TABLE {{ esc $t.Name }} PARTITION OF {{ $t.Partition.Reference }}
{{$t.Partition.Definition}}
{{- end -}}
{{ engine }};
{{- if $t.Indexes }}
{{ range $idx := $t.Indexes }}{{ if not (or $idx.IsPrimary $idx.IsUnique) }}
-- index {{ $idx.Name }}
{{- if $t.Partition.Reference }}
CREATE INDEX IF NOT EXISTS {{ esc $idx.Name }} ON {{ esc $t.Name }} ({{ fields $idx.Fields }});
{{ else }}
CREATE INDEX {{ esc $idx.Name }} ON {{ esc $t.Name }} ({{ fields $idx.Fields }});
{{ end -}}
{{ end -}}{{- end -}}{{- end }}
{{ end -}}
{{- end -}}
{{- if $s.Views }}
{{- range $v := $s.Views }}
-- view {{ $v.Name }}
{{ viewdef $v }};
{{ end }}
{{ end -}}
{{- if $s.Procs }}
{{- range $p := $s.Procs }}
-- {{ $p.Type }} {{ $p.Name }}
{{ procdef $p }};
{{ end -}}
{{ end -}}
{{ end -}}