View Source DSL: AshSqlite.DataLayer

A sqlite data layer that leverages Ecto's sqlite capabilities.

sqlite

Sqlite data layer configuration

Nested DSLs

Examples

sqlite do
  repo MyApp.Repo
  table "organizations"
end

Options

NameTypeDefaultDocs
repoatomThe repo that will be used to fetch your data. See the AshSqlite.Repo documentation for more
migrate?booleantrueWhether or not to include this resource in the generated migrations with mix ash.generate_migrations
migration_typeskeyword[]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.
migration_defaultskeyword[]A keyword list of attribute names to the ecto migration default that should be used for that attribute. The string you use will be placed verbatim in the migration. Use fragments like fragment(\\"now()\\"), or for nil, use \\"nil\\".
base_filter_sqlString.tA 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_indexesatom | list(atom)falseSkip generating unique indexes when generating migrations
unique_index_nameslist({list(atom), String.t} | {list(atom), String.t, String.t})[]A list of unique index names that could raise errors that are not configured in identities, or an mfa to a function that takes a changeset and returns the list. In the format {[:affected, :keys], "name_of_constraint"} or {[:affected, :keys], "name_of_constraint", "custom error message"}
exclusion_constraint_namesany[]A list of exclusion constraint names that could raise errors. Must be in the format {:affected_key, "name_of_constraint"} or {:affected_key, "name_of_constraint", "custom error message"}
identity_index_namesany[]A keyword list of identity names to the unique index name that they should use when being managed by the migration generator.
foreign_key_nameslist({atom, String.t} | {String.t, String.t})[]A list of foreign keys that could raise errors, or an mfa to a function that takes a changeset and returns a list. In the format: {:key, "name_of_constraint"} or {:key, "name_of_constraint", "custom error message"}
migration_ignore_attributeslist(atom)[]A list of attributes that will be ignored when generating migrations.
tableString.tThe table to store and read the resource from. If this is changed, the migration generator will not remove the old table.
polymorphic?booleanfalseDeclares this resource as polymorphic. See the polymorphic resources guide for more.

sqlite.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.

Nested DSLs

Examples

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

sqlite.custom_indexes.index

index fields

Add an index to be managed by the migration generator.

Examples

index ["column", "column2"], unique: true, where: "thing = TRUE"

Arguments

NameTypeDefaultDocs
fieldsatom | String.t | list(atom | String.t)The fields to include in the index.

Options

NameTypeDefaultDocs
nameString.tthe name of the index. Defaults to "#{table}_#{column}_index".
uniquebooleanfalseindicates whether the index should be unique.
usingString.tconfigures the index type.
whereString.tspecify conditions for a partial index.
messageString.tA custom message to use for unique indexes that have been violated
includelist(String.t)specify fields for a covering index. This is not supported by all databases. For more information on SQLite support, please read the official docs.

Introspection

Target: AshSqlite.CustomIndex

sqlite.custom_statements

A section for configuring custom statements to be added to migrations.

Changing custom statements may require manual intervention, because Ash can't determine what order they should run in (i.e if they depend on table structure that you've added, or vice versa). As such, any down statements we run for custom statements happen first, and any up statements happen last.

Additionally, when changing a custom statement, we must make some assumptions, i.e that we should migrate the old structure down using the previously configured down and recreate it.

This may not be desired, and so what you may end up doing is simply modifying the old migration and deleting whatever was generated by the migration generator. As always: read your migrations after generating them!

Nested DSLs

Examples

custom_statements do
  # the name is used to detect if you remove or modify the statement
  statement :pgweb_idx do
    up "CREATE INDEX pgweb_idx ON pgweb USING GIN (to_tsvector('english', title || ' ' || body));"
    down "DROP INDEX pgweb_idx;"
  end
end

sqlite.custom_statements.statement

statement name

Add a custom statement for migrations.

Examples

statement :pgweb_idx do
  up "CREATE INDEX pgweb_idx ON pgweb USING GIN (to_tsvector('english', title || ' ' || body));"
  down "DROP INDEX pgweb_idx;"
end

Arguments

NameTypeDefaultDocs
nameatomThe name of the statement, must be unique within the resource

Options

NameTypeDefaultDocs
upString.tHow to create the structure of the statement
downString.tHow to tear down the structure of the statement
code?booleanfalseBy default, we place the strings inside of ecto migration's execute/1 function and assume they are sql. Use this option if you want to provide custom elixir code to be placed directly in the migrations

Introspection

Target: AshSqlite.Statement

sqlite.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.

Nested DSLs

Examples

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

Options

NameTypeDefaultDocs
polymorphic_on_delete:delete | :nilify | :nothing | :restrictFor polymorphic resources, configures the on_delete behavior of the automatically generated foreign keys to source tables.
polymorphic_on_update:update | :nilify | :nothing | :restrictFor polymorphic resources, configures the on_update behavior of the automatically generated foreign keys to source tables.
polymorphic_name:update | :nilify | :nothing | :restrictFor polymorphic resources, configures the on_update behavior of the automatically generated foreign keys to source tables.

sqlite.references.reference

reference relationship

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.

Examples

reference :post, on_delete: :delete, on_update: :update, name: "comments_to_posts_fkey"

Arguments

NameTypeDefaultDocs
relationshipatomThe relationship to be configured

Options

NameTypeDefaultDocs
ignore?booleanIf set to true, no reference is created for the given relationship. This is useful if you need to define it in some custom way
on_delete:delete | :nilify | :nothing | :restrictWhat should happen to records of this resource when the referenced record of the destination resource is deleted.
on_update:update | :nilify | :nothing | :restrictWhat should happen to records of this resource when the referenced destination_attribute of the destination record is update.
deferrablefalse | true | :initiallyfalseWether or not the constraint is deferrable. This only affects the migration generator.
nameString.tThe name of the foreign key to generate in the database. Defaults to <table>_<source_attribute>_fkey

Introspection

Target: AshSqlite.Reference