You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: usage-rules.md
-317Lines changed: 0 additions & 317 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,321 +10,4 @@ SPDX-License-Identifier: MIT
10
10
11
11
AshPostgres is the PostgreSQL data layer for Ash Framework. It's the most fully-featured Ash data layer and should be your default choice unless you have specific requirements for another data layer. Any PostgreSQL version higher than 13 is fully supported.
12
12
13
-
## Basic Configuration
14
-
15
-
To use AshPostgres, add the data layer to your resource:
16
-
17
-
```elixir
18
-
defmoduleMyApp.Tweetdo
19
-
useAsh.Resource,
20
-
data_layer:AshPostgres.DataLayer
21
-
22
-
attributes do
23
-
integer_primary_key :id
24
-
attribute :text, :string
25
-
end
26
-
27
-
relationships do
28
-
belongs_to :author, MyApp.User
29
-
end
30
-
31
-
postgres do
32
-
table "tweets"
33
-
repo MyApp.Repo
34
-
end
35
-
end
36
-
```
37
-
38
-
## PostgreSQL Configuration
39
-
40
-
### Table & Schema Configuration
41
-
42
-
```elixir
43
-
postgres do
44
-
# Required: Define the table name for this resource
45
-
table "users"
46
-
47
-
# Optional: Define the PostgreSQL schema
48
-
schema "public"
49
-
50
-
# Required: Define the Ecto repo to use
51
-
repo MyApp.Repo
52
-
53
-
# Optional: Control whether migrations are generated for this resource
54
-
migrate? true
55
-
end
56
-
```
57
-
58
-
## Foreign Key References
59
-
60
-
Use the `references` section to configure foreign key behavior:
61
-
62
-
```elixir
63
-
postgres do
64
-
table "comments"
65
-
repo MyApp.Repo
66
-
67
-
references do
68
-
# Simple reference with defaults
69
-
reference :post
70
-
71
-
# Fully configured reference
72
-
reference :user,
73
-
on_delete::delete, # What happens when referenced row is deleted
74
-
on_update::update, # What happens when referenced row is updated
75
-
name:"comments_to_users_fkey", # Custom constraint name
76
-
deferrable:true, # Make constraint deferrable
77
-
initially_deferred:false# Defer constraint check to end of transaction
78
-
end
79
-
end
80
-
```
81
-
82
-
### Foreign Key Actions
83
-
84
-
For `on_delete` and `on_update` options:
85
-
86
-
-`:nothing` or `:restrict` - Prevent the change to the referenced row
87
-
-`:delete` - Delete the row when the referenced row is deleted (for `on_delete` only)
88
-
-`:update` - Update the row according to changes in the referenced row (for `on_update` only)
89
-
-`:nilify` - Set all foreign key columns to NULL
90
-
-`{:nilify, columns}` - Set specific columns to NULL (Postgres 15.0+ only)
91
-
92
-
> **Warning**: These operations happen directly at the database level. No resource logic, authorization rules, validations, or notifications are triggered.
93
-
94
-
## Check Constraints
95
-
96
-
Define database check constraints:
97
-
98
-
```elixir
99
-
postgres do
100
-
check_constraints do
101
-
check_constraint :positive_amount,
102
-
check:"amount > 0",
103
-
name:"positive_amount_check",
104
-
message:"Amount must be positive"
105
-
106
-
check_constraint :status_valid,
107
-
check:"status IN ('pending', 'active', 'completed')"
108
-
end
109
-
end
110
-
```
111
-
112
-
## Custom Indexes
113
-
114
-
Define custom indexes beyond those automatically created for identities and relationships:
115
-
116
-
```elixir
117
-
postgres do
118
-
custom_indexes do
119
-
index [:first_name, :last_name]
120
-
121
-
index :email,
122
-
unique:true,
123
-
name:"users_email_index",
124
-
where:"email IS NOT NULL",
125
-
using::gin
126
-
127
-
index [:status, :created_at],
128
-
concurrently:true,
129
-
include: [:user_id]
130
-
end
131
-
end
132
-
```
133
-
134
-
## Custom SQL Statements
135
-
136
-
Include custom SQL in migrations:
137
-
138
-
```elixir
139
-
postgres do
140
-
custom_statements do
141
-
statement "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\""
142
-
143
-
statement """
144
-
CREATE TRIGGER update_updated_at
145
-
BEFORE UPDATE ON posts
146
-
FOR EACH ROW
147
-
EXECUTE FUNCTION trigger_set_timestamp();
148
-
"""
149
-
150
-
statement "DROP INDEX IF EXISTS posts_title_index",
151
-
on_destroy:true# Only run when resource is destroyed/dropped
152
-
end
153
-
end
154
-
```
155
-
156
-
## Migrations and Codegen
157
-
158
-
### Development Migration Workflow (Recommended)
159
-
160
-
For development iterations, use the dev workflow to avoid naming migrations prematurely:
161
-
162
-
1. Make resource changes
163
-
2. Run `mix ash.codegen --dev` to generate and run dev migrations
164
-
3. Review the migrations and run `mix ash.migrate` to run them
165
-
4. Continue making changes and running `mix ash.codegen --dev` as needed
166
-
5. When your feature is complete, run `mix ash.codegen add_feature_name` to generate final named migrations (this will rollback dev migrations and squash them)
167
-
3. Review the migrations and run `mix ash.migrate` to run them
168
-
169
-
### Traditional Migration Generation
170
-
171
-
For single-step changes or when you know the final feature name:
172
-
173
-
1. Run `mix ash.codegen add_feature_name` to generate migrations
174
-
2. Review the generated migrations in `priv/repo/migrations`
175
-
3. Run `mix ash.migrate` to apply the migrations
176
-
177
-
> **Tip**: The dev workflow (`--dev` flag) is preferred during development as it allows you to iterate without thinking of migration names and provides better development ergonomics.
178
-
179
-
> **Warning**: Always review migrations before applying them to ensure they are correct and safe.
3.**Use custom statements for schema-only changes**: If you need to add database objects not directly tied to resources:
323
-
```elixir
324
-
custom_statements do
325
-
statement "CREATE EXTENSION IF NOT EXISTS \"pgcrypto\""
326
-
statement "CREATE INDEX users_search_idx ON users USING gin(search_vector)"
327
-
end
328
-
```
329
-
330
13
Remember that using AshPostgres provides a full-featured PostgreSQL data layer for your Ash application, giving you both the structure and declarative approach of Ash along with the power and flexibility of PostgreSQL.
0 commit comments