Skip to content

Commit 7155754

Browse files
docs: add configuration example and reference in exceptions (#435)
* docs: add configuration example and reference in exceptions * doc: replace module references with hex URLs This makes for better developer experience with terminals smart enough to use hyperlinks.
1 parent 813944b commit 7155754

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

documentation/dsls/DSL-AshPostgres.DataLayer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ end
4444
| [`migration_types`](#postgres-migration_types){: #postgres-migration_types } | `keyword` | `[]` | 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. |
4545
| [`migration_defaults`](#postgres-migration_defaults){: #postgres-migration_defaults } | `keyword` | `[]` | 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\\"`. |
4646
| [`calculations_to_sql`](#postgres-calculations_to_sql){: #postgres-calculations_to_sql } | `keyword` | | A keyword list of calculations and their SQL representation. Used when creating unique indexes for identities over calculations |
47-
| [`identity_wheres_to_sql`](#postgres-identity_wheres_to_sql){: #postgres-identity_wheres_to_sql } | `keyword` | | A keyword list of identity names and the SQL representation of their `where` clause. Used when creating unique indexes for identities over calculations |
47+
| [`identity_wheres_to_sql`](#postgres-identity_wheres_to_sql){: #postgres-identity_wheres_to_sql } | `keyword` | | A keyword list of identity names and the SQL representation of their `where` clause. See `AshPostgres.DataLayer.Info.identity_wheres_to_sql/1` for more details. |
4848
| [`base_filter_sql`](#postgres-base_filter_sql){: #postgres-base_filter_sql } | `String.t` | | 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 |
4949
| [`simple_join_first_aggregates`](#postgres-simple_join_first_aggregates){: #postgres-simple_join_first_aggregates } | `list(atom)` | `[]` | A list of `:first` type aggregate names that can be joined to using a simple join. Use when you have a `:first` aggregate that uses a to-many relationship , but your `filter` statement ensures that there is only one result. Optimizes the generated query. |
5050
| [`skip_unique_indexes`](#postgres-skip_unique_indexes){: #postgres-skip_unique_indexes } | `atom \| list(atom)` | `[]` | Skip generating unique indexes when generating migrations |

lib/data_layer.ex

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ defmodule AshPostgres.DataLayer do
315315
identity_wheres_to_sql: [
316316
type: :keyword_list,
317317
doc:
318-
"A keyword list of identity names and the SQL representation of their `where` clause. Used when creating unique indexes for identities over calculations"
318+
"A keyword list of identity names and the SQL representation of their `where` clause. See `AshPostgres.DataLayer.Info.identity_wheres_to_sql/1` for more details."
319319
],
320320
base_filter_sql: [
321321
type: :string,
@@ -2668,9 +2668,11 @@ defmodule AshPostgres.DataLayer do
26682668
case identity do
26692669
%{name: name, where: where} when not is_nil(where) ->
26702670
AshPostgres.DataLayer.Info.identity_where_to_sql(resource, name) ||
2671-
raise(
2672-
"Must provide an entry for :#{identity.name} in `postgres.identity_wheres_to_sql` to use it as an upsert_identity"
2673-
)
2671+
raise("""
2672+
Must provide an entry for :#{identity.name} in `postgres.identity_wheres_to_sql` to use it as an upsert_identity.
2673+
2674+
See https://hexdocs.pm/ash_postgres/AshPostgres.DataLayer.Info.html#identity_wheres_to_sql/1 for an example.
2675+
""")
26742676

26752677
_ ->
26762678
nil

lib/data_layer/info.ex

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,39 @@ defmodule AshPostgres.DataLayer.Info do
2323
calculations_to_sql(resource)[calc]
2424
end
2525

26-
@doc "A keyword list of identity names to the sql representation of their where clauses"
26+
@doc """
27+
A keyword list of identity names to the literal SQL string representation of
28+
the `where` clause portion of identity's partial unique index.
29+
30+
For example, given the following identity for a resource:
31+
32+
identities do
33+
identity :active, [:status] do
34+
where expr(status == "active")
35+
end
36+
end
37+
38+
An appropriate `identity_wheres_to_sql` would need to be made to generate the
39+
correct migration for the partial index used by the identity:
40+
41+
postgres do
42+
...
43+
44+
identity_wheres_to_sql active: "status = 'active'"
45+
end
46+
"""
47+
@spec identity_wheres_to_sql(Ash.Resource.t()) :: keyword(String.t())
2748
def identity_wheres_to_sql(resource) do
2849
Extension.get_opt(resource, [:postgres], :identity_wheres_to_sql, [])
2950
end
3051

52+
@doc """
53+
Returns the literal SQL for the `where` clause given a resource and an
54+
identity name.
55+
56+
See `identity_wheres_to_sql/1` for more details.
57+
"""
58+
@spec identity_where_to_sql(Ash.Resource.t(), atom()) :: String.t() | nil
3159
def identity_where_to_sql(resource, identity) do
3260
identity_wheres_to_sql(resource)[identity]
3361
end

lib/migration_generator/migration_generator.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3006,9 +3006,11 @@ defmodule AshPostgres.MigrationGenerator do
30063006
where:
30073007
if identity.where do
30083008
AshPostgres.DataLayer.Info.identity_where_to_sql(resource, identity.name) ||
3009-
raise(
3010-
"Must provide an entry for :#{identity.name} in `postgres.identity_wheres_to_sql`, or skip this identity with `postgres.skip_unique_indexes`"
3011-
)
3009+
raise("""
3010+
Must provide an entry for :#{identity.name} in `postgres.identity_wheres_to_sql`, or skip this identity with `postgres.skip_unique_indexes`.
3011+
3012+
See https://hexdocs.pm/ash_postgres/AshPostgres.DataLayer.Info.html#identity_wheres_to_sql/1 for an example.
3013+
""")
30123014
end
30133015
}
30143016
end)

0 commit comments

Comments
 (0)