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

Add partitioned tables type in postgres schema generation #405

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions _examples/a_bit_of_everything/postgres/apartitiontable.xo.go

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

7 changes: 7 additions & 0 deletions _examples/a_bit_of_everything/postgres/xo.xo.dot

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

63 changes: 63 additions & 0 deletions _examples/a_bit_of_everything/postgres/xo.xo.json

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

8 changes: 8 additions & 0 deletions _examples/a_bit_of_everything/postgres/xo.xo.sql

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

36 changes: 36 additions & 0 deletions _examples/a_bit_of_everything/postgres/xo.xo.yaml

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

7 changes: 7 additions & 0 deletions _examples/a_bit_of_everything/sql/postgres_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ CREATE TABLE a_unique_index_composite (
UNIQUE (a_key1, a_key2)
);

-- table a_partition_table
CREATE TABLE a_partition_table (
a_key1 INTEGER,
a_key2 TIMESTAMP,
CONSTRAINT a_partition_table_pkey PRIMARY KEY (a_key1,a_key2)
) PARTITION BY RANGE (a_key2);

-- enum type
CREATE TYPE a_enum AS ENUM (
'ONE',
Expand Down
10 changes: 7 additions & 3 deletions cmd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,13 @@ func LoadTables(ctx context.Context, args *Args, typ string) ([]xo.Table, error)
}
// create table
t := &xo.Table{
Type: typ,
Name: table.TableName,
Manual: true,
Type: typ,
Name: table.TableName,
Manual: true,
Partition: xo.Partition{
Reference: table.PartitionOf,
Definition: table.PartitionDef,
},
Definition: strings.TrimSpace(table.ViewDef),
}
// process columns
Expand Down
8 changes: 7 additions & 1 deletion gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,27 @@ $XOBIN query $PGDB -M -B -2 -T Table -F PostgresTables --type-comment "$COMMENT"
SELECT
(CASE c.relkind
WHEN 'r' THEN 'table'
WHEN 'p' THEN 'table'
WHEN 'v' THEN 'view'
END)::varchar AS type,
c.relname::varchar AS table_name,
false::boolean AS manual_pk,
CASE c.relkind
WHEN 'r' THEN COALESCE(obj_description(c.relname::regclass), '')
WHEN 'p' THEN COALESCE(obj_description(c.relname::regclass), '')
WHEN 'v' THEN v.definition
END AS view_def
END AS view_def,
(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,
COALESCE(pg_get_expr(c.relpartbound, c.oid, true), pg_get_partkeydef(c.oid), '') as partition_def
FROM pg_class c
JOIN ONLY pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_views v ON n.nspname = v.schemaname
AND v.viewname = c.relname
LEFT JOIN pg_inherits i ON i.inhrelid = c.oid
WHERE n.nspname = %%schema string%%
AND (CASE c.relkind
WHEN 'r' THEN 'table'
WHEN 'p' THEN 'table'
WHEN 'v' THEN 'view'
END) = LOWER(%%typ string%%)
ENDSQL
Expand Down
12 changes: 10 additions & 2 deletions models/table.xo.go

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

16 changes: 15 additions & 1 deletion templates/createdb/xo.xo.sql.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CREATE TYPE {{ esc $e.Name }} AS ENUM (
{{- 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 }}
Expand All @@ -28,11 +29,24 @@ CREATE TABLE {{ esc $t.Name }} (
{{- 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 }}
){{ engine }};
)
{{- 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 -}}
Expand Down
2 changes: 1 addition & 1 deletion templates/go/schema.xo.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (err ErrInvalid{{ $e.GoName }}) Error() string {
{{ define "typedef" }}
{{- $t := .Data -}}
{{- if $t.Comment -}}
// {{ $t.Comment | eval $t.GoName }}
/* {{ $t.Comment | eval $t.GoName }} */
{{- else -}}
// {{ $t.GoName }} represents a row from '{{ schema $t.SQLName }}'.
{{- end }}
Expand Down
Loading