Skip to content

Commit f71d33c

Browse files
committed
Add partition to table type, update createdb template
1 parent ce2cf40 commit f71d33c

File tree

11 files changed

+261
-7
lines changed

11 files changed

+261
-7
lines changed

_examples/a_bit_of_everything/postgres/apartitiontable.xo.go

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/a_bit_of_everything/postgres/xo.xo.dot

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/a_bit_of_everything/postgres/xo.xo.json

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/a_bit_of_everything/postgres/xo.xo.sql

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/a_bit_of_everything/postgres/xo.xo.yaml

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/a_bit_of_everything/sql/postgres_schema.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ CREATE TABLE a_unique_index_composite (
8383
UNIQUE (a_key1, a_key2)
8484
);
8585

86+
-- table a_partition_table
87+
CREATE TABLE a_partition_table (
88+
a_key1 INTEGER,
89+
a_key2 TIMESTAMP,
90+
CONSTRAINT a_partition_table_pkey PRIMARY KEY (a_key1,a_key2)
91+
) PARTITION BY RANGE (a_key2);
92+
8693
-- enum type
8794
CREATE TYPE a_enum AS ENUM (
8895
'ONE',

cmd/schema.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,13 @@ func LoadTables(ctx context.Context, args *Args, typ string) ([]xo.Table, error)
200200
}
201201
// create table
202202
t := &xo.Table{
203-
Type: typ,
204-
Name: table.TableName,
205-
Manual: true,
203+
Type: typ,
204+
Name: table.TableName,
205+
Manual: true,
206+
Partition: xo.Partition{
207+
Reference: table.PartitionOf,
208+
Definition: table.PartitionDef,
209+
},
206210
Definition: strings.TrimSpace(table.ViewDef),
207211
}
208212
// process columns

gen.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,27 @@ $XOBIN query $PGDB -M -B -2 -T Table -F PostgresTables --type-comment "$COMMENT"
152152
SELECT
153153
(CASE c.relkind
154154
WHEN 'r' THEN 'table'
155+
WHEN 'p' THEN 'table'
155156
WHEN 'v' THEN 'view'
156157
END)::varchar AS type,
157158
c.relname::varchar AS table_name,
158159
false::boolean AS manual_pk,
159160
CASE c.relkind
160161
WHEN 'r' THEN COALESCE(obj_description(c.relname::regclass), '')
162+
WHEN 'p' THEN COALESCE(obj_description(c.relname::regclass), '')
161163
WHEN 'v' THEN v.definition
162-
END AS view_def
164+
END AS view_def,
165+
(CASE WHEN i.inhparent IS NOT NULL THEN (SELECT pg_class.relname FROM pg_class WHERE pg_class.oid = i.inhparent) ELSE '' END) as partition_of,
166+
COALESCE(pg_get_expr(c.relpartbound, c.oid, true), pg_get_partkeydef(c.oid), '') as partition_def
163167
FROM pg_class c
164168
JOIN ONLY pg_namespace n ON n.oid = c.relnamespace
165169
LEFT JOIN pg_views v ON n.nspname = v.schemaname
166170
AND v.viewname = c.relname
171+
LEFT JOIN pg_inherits i ON i.inhrelid = c.oid
167172
WHERE n.nspname = %%schema string%%
168173
AND (CASE c.relkind
169174
WHEN 'r' THEN 'table'
175+
WHEN 'p' THEN 'table'
170176
WHEN 'v' THEN 'view'
171177
END) = LOWER(%%typ string%%)
172178
ENDSQL

models/table.xo.go

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/createdb/xo.xo.sql.tpl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CREATE TYPE {{ esc $e.Name }} AS ENUM (
1818
{{- if $s.Tables }}
1919
{{- range $t := $s.Tables }}
2020
-- table {{ $t.Name }}
21+
{{- if eq $t.Partition.Reference ""}}
2122
CREATE TABLE {{ esc $t.Name }} (
2223
{{- range $i, $c := $t.Columns }}
2324
{{ coldef $t $c }}{{ comma $i $t.Columns }}
@@ -28,11 +29,24 @@ CREATE TABLE {{ esc $t.Name }} (
2829
{{- range $fk := $t.ForeignKeys -}}{{- if gt (len $fk.Fields) 1 }},
2930
{{ constraint $fk.Name -}} FOREIGN KEY ({{ fields $fk.Fields }}) REFERENCES {{ esc $fk.RefTable }} ({{ fields $fk.RefFields }})
3031
{{- end -}}{{- end }}
31-
){{ engine }};
32+
)
33+
{{- if $t.Partition.Definition }}
34+
PARTITION BY {{$t.Partition.Definition}}
35+
{{- end -}}
36+
{{- else }}
37+
CREATE TABLE {{ esc $t.Name }} PARTITION OF {{ $t.Partition.Reference }}
38+
{{$t.Partition.Definition}}
39+
{{- end -}}
40+
{{ engine }};
41+
3242
{{- if $t.Indexes }}
3343
{{ range $idx := $t.Indexes }}{{ if not (or $idx.IsPrimary $idx.IsUnique) }}
3444
-- index {{ $idx.Name }}
45+
{{- if $t.Partition.Reference }}
46+
CREATE INDEX IF NOT EXISTS {{ esc $idx.Name }} ON {{ esc $t.Name }} ({{ fields $idx.Fields }});
47+
{{ else }}
3548
CREATE INDEX {{ esc $idx.Name }} ON {{ esc $t.Name }} ({{ fields $idx.Fields }});
49+
{{ end -}}
3650
{{ end -}}{{- end -}}{{- end }}
3751
{{ end -}}
3852
{{- end -}}

0 commit comments

Comments
 (0)