Skip to content

Commit

Permalink
Add partition to table type, update createdb template
Browse files Browse the repository at this point in the history
  • Loading branch information
dimonmontana committed Mar 6, 2024
1 parent ce2cf40 commit f71d33c
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 7 deletions.
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
7 changes: 7 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type Table struct {
ForeignKeys []ForeignKey `json:"foreign_keys,omitempty"`
Manual bool `json:"manual,omitempty"`
Definition string `json:"definition,omitempty"` // empty for tables
Partition Partition `json:"partition,omitempty"` // empty for views
}

// MarshalYAML satisfies the yaml.Marshaler interface.
Expand Down Expand Up @@ -150,6 +151,12 @@ type Type struct {
Enum *Enum `json:"-"`
}

// Partition holds information about table partition.
type Partition struct {
Reference string `json:"reference,omitempty"`
Definition string `json:"definition,omitempty"`
}

// ParseType parses "type[ (precision[,scale])][\[\]]" strings returning the
// parsed precision, scale, and if the type is an array or not.
//
Expand Down

0 comments on commit f71d33c

Please sign in to comment.