perseas / Pyrseas

Provides utilities for Postgres database schema versioning.
https://perseas.github.io/
BSD 3-Clause "New" or "Revised" License
395 stars 67 forks source link

Add support for table partitioning syntax #163

Closed jmafc closed 6 years ago

jmafc commented 7 years ago

Coming in PG 10. See https://www.postgresql.org/docs/devel/static/sql-createtable.html#sql-createtable-partition

jmafc commented 7 years ago

@dvarrazzo @rhunwicks @darthunix @tbussmann I have started working on this and would like some feedback. When pg_dump outputs a partitioned table and its partitions, it looks as in the following example:

CREATE TABLE t1 (
    c1 integer NOT NULL,
    c2 text
)
PARTITION BY RANGE (c1);

CREATE TABLE t1a PARTITION OF t1
FOR VALUES FROM (0) TO (1000);

I'm proposing to follow a similar pattern, i.e., the partitions will not show the column details. Currently the proposed dbtoyaml output looks as follows:

  table t1:
    columns:
    - c1:
        not_null: true
        type: integer
    - c2:
        type: text
    partition_by: range
  table t1a:
    inherits:
    - t1
    partition_bound_spec: FROM (0) TO (1000)

The inherits comes automatically from existing code, but perhaps we could change it to partition_of (leaving the list spec for t1 although partitions inherit from only a single table).

Still missing are the columns and/or expressions used for partitioning. I'm proposing to add them under partition_by as keys (the PG documentation states "The parenthesized list of columns or expressions forms the partition key for the table") followed by a YAML list, e.g.,

  partition_by: range
    keys:
    - c1

Comments?

jmafc commented 7 years ago

I've worked on this (and after noticing my YAML error in the last part above), I've decided to implement something that looks as follows:

  table t1:
    columns:
    - c1:
        not_null: true
        type: integer
    - c2:
        type: text
    partition_by:
      range:
      - c1
  table t1a:
    partition_of:  t1
    partition_bound_spec: FROM (0) TO (1000)

The partition_of part is already done, so now I have to add the list of columns and expressions.

jmafc commented 6 years ago

Closing this issue. Missing support for expressions will be addressed in #178.