AshPostgresPartition

Copy Markdown View Source

Ash Postgres Partition is an extension for Ash Resources It makes it easier to add partitions and to manage them It also supports having tenants

partition

Example of how to use this to add a :list type partition to a resource

  defmodule MyResource do
    use Ash.Resource,
      domain: MyDomain,
      data_layer: AshPostgres.DataLayer,
      extensions: [AshPostgresPartition]

    attributes do
      uuid_v7_primary_key(:id)

      attribute(:key, :string, primary_key?: true, allow_nil?: false)
      attribute(:data, :string)

      update_timestamp(:updated_at, writable?: true)
      create_timestamp(:inserted_at, writable?: true)
    end

    postgres do
      table("mytable")
      repo(MyRepo)
    end

    actions do
      defaults([:create, :read])
      default_accept([:key, :data])
    end

    partition do
      type(:list)
      attribute(:key)
      name fn table, key -> {:ok, table <> "_" <> key} end
    end

    multitenancy do
      strategy(:context)
    end
  end

Options

NameTypeDefaultDocs
type:range | :list | :hashType of partitioning to use, possible values are :range, :list and :hash
attributeatomWhat attribute to use for partitioning
name(any, any -> any)Function to generate the name of the partition, expects {:ok, binary()}, the function is provided the table name and the key
generate_default_partition?booleanfalseFor :range and :list types this allows to generate a default partition for data not fitting into the specified values
optskeyword[]When using :hash type, you must provide a :count option with number of partitions to create