README.md

# PartitionedSchema

Adds partition maintenance helpers to an Ecto schema module.

## Usage

    defmodule MyApp.Voltage do
      use Ecto.Schema
      use PartitionedSchema,
        repo: MyApp.Repo,
        table_name: "voltages",
        partition_column: :ts,
        partition_column_type: :timestamptz, # :date | :timestamptz | :naive_datetime
        partition_type: :weekly,             # :daily | :weekly | :monthly
        retention: 30,                       # days, or {:days, n}, {:weeks, n}, {:months, n}, or nil
        timezone: "Etc/UTC"                  # used for DateTime boundaries

      schema "voltages" do
        field :tracker_id, :id
        field :ts, :utc_datetime_usec
        field :voltage, :float
      end
    end

Your parent table must already exist and be partitioned, e.g.:

    CREATE TABLE voltages (
      tracker_id bigint NOT NULL,
      ts timestamptz NOT NULL,
      voltage real NOT NULL
    ) PARTITION BY RANGE (ts);

This module creates partitions with:

    CREATE TABLE IF NOT EXISTS <child> PARTITION OF <parent>
    FOR VALUES FROM ('...') TO ('...');

Notes:
- Partition ranges are inclusive on start and exclusive on end (Postgres semantics).
- By default, weekly boundaries are ISO weeks (week starts Monday) when using dates.
- For timestamptz partitions, boundaries are computed in `timezone` and then stored as UTC instants.

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `partitioned_schema` to your list of dependencies in `mix.exs`:

```elixir
def deps do
  [
    {:partitioned_schema, "~> 0.1.0"}
  ]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/partitioned_schema>.