ash_postgres v0.41.5 AshPostgres.DataLayer View Source

A postgres data layer that levereges Ecto's postgres capabilities.

Table of Contents

  • postgres
    • custom_indexes
      • index
    • manage_tenant
    • references
      • reference
    • check_constraints
      • check_constraint

postgres

Postgres data layer configuration

Examples:

postgres do
  repo MyApp.Repo
  table "organizations"
end

  • :repo - Required. The repo that will be used to fetch your data. See the AshPostgres.Repo documentation for more

  • :migrate? - Whether or not to include this resource in the generated migrations with mix ash.generate_migrations The default value is true.

  • :migration_types - A keyword list of attribute names to the ecto migration type that should be used for that attribute. Only necessary if you need to override the defaults. The default value is [].

  • :base_filter_sql - A raw sql version of the base_filter, e.g representative = true. Required if trying to create a unique constraint on a resource with a base_filter

  • :skip_unique_indexes - Skip generating unique indexes when generating migrations The default value is false.

  • :unique_index_names - A list of unique index names that could raise errors, or an mfa to a function that takes a changeset and returns the list. Must be in the format {[:affected, :keys], "name_of_constraint"} or {[:affected, :keys], "name_of_constraint", "custom error message"}

    Note that this is not used to rename the unique indexes created from identities. Use identity_index_names for that. This is used to tell ash_postgres about unique indexes that exist in the database that it didn't create. The default value is [].

  • :identity_index_names - A keyword list of identity names to the unique index name that they should use when being managed by the migration generator. The default value is [].

  • :foreign_key_names - A list of foreign keys that could raise errors, or an mfa to a function that takes a changeset and returns the list. Must be in the format {:key, "name_of_constraint"} or {:key, "name_of_constraint", "custom error message"} The default value is [].

  • :table - The table to store and read the resource from. Required unless polymorphic? is true.

  • :polymorphic? - Declares this resource as polymorphic.

    Polymorphic resources cannot be read or updated unless the table is provided in the query/changeset context.

    For example:

    PolymorphicResource |> Ash.Query.set_context(%{data_layer: %{table: "table"}}) |> MyApi.read!()

    When relating to polymorphic resources, you'll need to use the context option on relationships, e.g

    belongs_to :polymorphic_association, PolymorphicResource, context: %{data_layer: %{table: "table"}} The default value is false.

custom_indexes

A section for configuring indexes to be created by the migration generator.

In general, prefer to use identities for simple unique constraints. This is a tool to allow for declaring more complex indexes.

Examples:

custom_indexes do
  index [:column1, :column2], unique: true, where: "thing = TRUE"
end

index

Add an index to be managed by the migration generator.

Introspection Target:

AshPostgres.CustomIndex

Examples:

index ["column", "column2"], unique: true, where: "thing = TRUE"
  • :fields - The fields to include in the index.

  • :name - the name of the index. Defaults to "#{table}_#{column}_index".

  • :unique - indicates whether the index should be unique. The default value is false.

  • :concurrently - indicates whether the index should be created/dropped concurrently. The default value is false.

  • :using - configures the index type.

  • :prefix - specify an optional prefix for the index.

  • :where - specify conditions for a partial index.

  • :include - specify fields for a covering index. This is not supported by all databases. For more information on PostgreSQL support, please read the official docs.

manage_tenant

Configuration for the behavior of a resource that manages a tenant

Examples:

manage_tenant do
  template ["organization_", :id]
  create? true
  update? false
end

  • :template - Required. A template that will cause the resource to create/manage the specified schema.

    Use this if you have a resource that, when created, it should create a new tenant for you. For example, if you have a customer resource, and you want to create a schema for each customer based on their id, e.g customer_10 set this option to ["customer_", :id]. Then, when this is created, it will create a schema called ["customer_", :id], and run your tenant migrations on it. Then, if you were to change that customer's id to 20, it would rename the schema to customer_20. Generally speaking you should avoid changing the tenant id.

  • :create? - Whether or not to automatically create a tenant when a record is created The default value is true.

  • :update? - Whether or not to automatically update the tenant name if the record is udpated The default value is true.

references

A section for configuring the references (foreign keys) in resource migrations.

This section is only relevant if you are using the migration generator with this resource. Otherwise, it has no effect.

Examples:

references do
  reference :post, on_delete: :delete, on_update: :update, name: "comments_to_posts_fkey"
end

  • :polymorphic_on_delete - For polymorphic resources, configures the on_delete behavior of the automatically generated foreign keys to source tables.

  • :polymorphic_on_update - For polymorphic resources, configures the on_update behavior of the automatically generated foreign keys to source tables.

  • :polymorphic_name - For polymorphic resources, configures the on_update behavior of the automatically generated foreign keys to source tables.

reference

Configures the reference for a relationship in resource migrations.

Keep in mind that multiple relationships can theoretically involve the same destination and foreign keys. In those cases, you only need to configure the reference behavior for one of them. Any conflicts will result in an error, across this resource and any other resources that share a table with this one. For this reason, instead of adding a reference configuration for :nothing, its best to just leave the configuration out, as that is the default behavior if no relationship anywhere has configured the behavior of that reference.

Introspection Target:

AshPostgres.Reference

Examples:

reference :post, on_delete: :delete, on_update: :update, name: "comments_to_posts_fkey"
  • :relationship - Required. The relationship to be configured

  • :on_delete - What should happen to records of this resource when the referenced record of the destination resource is deleted.

    The difference between :nothing and :restrict is subtle and, if you are unsure, choose :nothing (the default behavior). :restrict will prevent the deletion from happening before the end of the database transaction, whereas :nothing allows the transaction to complete before doing so. This allows for things like deleting the destination row and then deleting the source row.

    Important!

    No resource logic is applied with this operation! No authorization rules or validations take place, and no notifications are issued. This operation happens directly in the database.

    This option is called on_delete, instead of on_destroy, because it is hooking into the database level deletion, not a destroy action in your resource.

  • :on_update - What should happen to records of this resource when the referenced destination_field of the destination record is update.

    The difference between :nothing and :restrict is subtle and, if you are unsure, choose :nothing (the default behavior). :restrict will prevent the deletion from happening before the end of the database transaction, whereas :nothing allows the transaction to complete before doing so. This allows for things like updating the destination row and then updating the reference as long as you are in a transaction.

    Important!

    No resource logic is applied with this operation! No authorization rules or validations take place, and no notifications are issued. This operation happens directly in the database.

  • :name - The name of the foreign key to generate in the database. Defaults to <table>_<source_field>_fkey

check_constraints

A section for configuring the check constraints for a given table.

This can be used to automatically create those check constraints, or just to provide message when they are raised

Examples:

check_constraints do
  check_constraint :price, "price_must_be_positive", check: "price > 0", message: "price must be positive"
end

check_constraint

Add a check constraint to be validated.

If a check constraint exists on the table but not in this section, and it produces an error, a runtime error will be raised.

Provide a list of attributes instead of a single attribute to add the message to multiple attributes.

By adding the check option, the migration generator will include it when generating migrations.

Introspection Target:

AshPostgres.CheckConstraint

Examples:

check_constraint :price, "price_must_be_positive", check: "price > 0", message: "price must be positive"
  • :attribute - Required. The attribute or list of attributes to which an error will be added if the check constraint fails

  • :name - Required. The name of the constraint

  • :message - The message to be added if the check constraint fails

  • :check - The contents of the check. If this is set, the migration generator will include it when generating migrations