Add Support for Postgres Triggers
- Dominant language
- JavaScript
- Stars
- 3.7k
- Forks
- 233
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 4
Description
### Enhancement
Add support for Postgres `CREATE OR REPLACE TRIGGER` statements.
### Syntax
```dbml
Trigger {
name 'trg_foo_before_insert'
schema public
table foo
when before
event [insert, update]
update_of [col1, col2] // optional — only valid when event includes update
for_each row
condition `NEW.status IS NOT NULL` // optional — the WHEN clause
function my_trigger_fn
constraint true // optional — makes this a CONSTRAINT TRIGGER
deferrable true // optional — only valid when constraint is true
timing initially_deferred // optional — only valid when deferrable is true
}
```
### Field Reference
| Field | Required | Type | Accepted Values |
|---|---|---|---|
| `name` | yes | string | Any valid trigger name |
| `schema` | no | string | defaults to `public` |
| `table` | yes | identifier | An existing table name |
| `when` | yes | enum | `before`, `after`, `instead_of` |
| `event` | yes | list | `insert`, `delete`, `update`, `truncate` — one or more |
| `update_of` | no | list of column names | Only valid when `event` includes `update` |
| `for_each` | yes | enum | `row`, `statement` |
| `condition` | no | expression string | Backtick-quoted boolean expression |
| `function` | yes | identifier | Name of the trigger function to execute |
| `constraint` | no | boolean | `true` \| `false` (default `false`) |
| `deferrable` | no | boolean | `true` \| `false` — only valid when `constraint` is `true` |
| `timing` | no | enum | `initially_immediate`, `initially_deferred` — only valid when `deferrable` is `true` |
### Example
```dbml
Trigger {
name 'trg_orders_before_insert'
table orders
when before
event [insert]
for_each row
function audit_fn
}
```
Emits:
```sql
CREATE OR REPLACE TRIGGER trg_orders_before_insert
BEFORE INSERT
ON public.orders
FOR EACH ROW
EXECUTE FUNCTION audit_fn();
```
Contributor guide
Assessment
This issue has not been assessed yet.