feat(pgsql): unique constraints on apply, matching unique index names, FK deferral
- migration writer now emits unique constraints (direct apply and live diff previously dropped them); idempotent via the existing DO block - unique constraint adopts a same-named model unique index with ADD CONSTRAINT ... UNIQUE USING INDEX instead of building a second index - dbml reader names unnamed unique indexes ukey_<table>_<cols> to match the unique constraint naming - foreign key deferral: Constraint.Deferrable/InitiallyDeferred are *bool (nil = unspecified, never diffed); writers emit DEFERRABLE, DEFERRABLE INITIALLY DEFERRED and NOT DEFERRABLE; live reader reads condeferrable and condeferred; dbml reader/writer support deferrable and initially deferred - tests and README/naming docs updated
This commit is contained in:
@@ -154,6 +154,29 @@ When `migration_mode` is enabled, generates migration scripts with:
|
||||
- Transactional DDL
|
||||
- Rollback support
|
||||
|
||||
### Unique constraints and indexes
|
||||
|
||||
- Unique constraint → `ADD CONSTRAINT <name> UNIQUE (cols)`, idempotent (checked by name)
|
||||
- Migration mode and direct apply both emit them (not only `convert`)
|
||||
- Same-named model unique index → `UNIQUE USING INDEX` (no duplicate index). Requires: same name, same columns, btree, non-partial, no `INCLUDE`
|
||||
- Otherwise a plain `ADD CONSTRAINT` (second index is built)
|
||||
- Naming: unique constraint and unique index both `ukey_<table>_<cols>`; see `NAMING_CONVENTIONS.md`
|
||||
- Renaming `uidx_…` → `ukey_…` drops the old index and re-adds the constraint on the next diff
|
||||
|
||||
### FK deferral
|
||||
|
||||
| Model (`Deferrable` / `InitiallyDeferred`) | SQL |
|
||||
| ------------------------------------------ | --- |
|
||||
| unset | create: `DEFERRABLE` (migration/apply), none (full DDL constraints); existing FK never diffed |
|
||||
| `true` / unset or `false` | `DEFERRABLE` |
|
||||
| `true` / `true`, or unset / `true` | `DEFERRABLE INITIALLY DEFERRED` |
|
||||
| `false` | `NOT DEFERRABLE` |
|
||||
|
||||
- Both fields are `*bool`; `nil` = unspecified
|
||||
- Specified value differing from the live DB → FK dropped and re-added
|
||||
- Live reader reads `condeferrable` / `condeferred`
|
||||
- Initial mode compared only when both sides state it
|
||||
|
||||
### Audit Triggers
|
||||
|
||||
When `include_audit` is enabled, adds:
|
||||
|
||||
Reference in New Issue
Block a user