Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make NO ACTION the default action for foreign keys #1560

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions persistent-postgresql/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* [#1547](https://github.com/yesodweb/persistent/pull/1547)
* Bump `libpq` bounds
* [#1560](https://github.com/yesodweb/persistent/pull/1560)
* Treat `NO ACTION` as the default action, rather than `RESTRICT`.

## 2.13.6.2

Expand Down
6 changes: 3 additions & 3 deletions persistent-postgresql/Database/Persist/Postgresql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ findAlters defs edef col@(Column name isNull sqltype def _gen _defConstraintName
)

-- We check if we should alter a foreign key. This is almost an equality check,
-- except we consider 'Nothing' and 'Just Restrict' equivalent.
-- except we consider 'Nothing' and 'Just NoAction' equivalent.
equivalentRef :: Maybe ColumnReference -> Maybe ColumnReference -> Bool
equivalentRef Nothing Nothing = True
equivalentRef (Just cr1) (Just cr2) =
Expand All @@ -1204,8 +1204,8 @@ equivalentRef (Just cr1) (Just cr2) =
where
eqCascade :: Maybe CascadeAction -> Maybe CascadeAction -> Bool
eqCascade Nothing Nothing = True
eqCascade Nothing (Just Restrict) = True
eqCascade (Just Restrict) Nothing = True
eqCascade Nothing (Just NoAction) = True
eqCascade (Just NoAction) Nothing = True
eqCascade (Just cs1) (Just cs2) = cs1 == cs2
eqCascade _ _ = False
equivalentRef _ _ = False
Expand Down
6 changes: 6 additions & 0 deletions persistent/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog for persistent

## Unreleased

* [#1560](https://github.com/yesodweb/persistent/pull/1560)
* Add a `NoAction` option for foreign key constraints.
* Change the default action from `Restrict` to `NoAction`.

## 2.14.6.3

* [#1544](https://github.com/yesodweb/persistent/pull/1544)
Expand Down
5 changes: 3 additions & 2 deletions persistent/Database/Persist/Quasi.hs
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,9 @@ AnotherVeryLongTableName
These options affects how a referring record behaves when the target record is changed.
There are several options:

* 'Restrict' - This is the default. It prevents the action from occurring.
* 'Cascade' - this copies the change to the child record. If a parent record is deleted, then the child record will be deleted too.
* 'NoAction' - This is the default. It prevents the action from occurring, but the check can be deferred until the end of the transaction.
* 'Restrict' - This prevents the action from occurring by immediately aborting a transaction.
* 'Cascade' - This copies the change to the child record. If a parent record is deleted, then the child record will be deleted too.
* 'SetNull' - If the parent record is modified, then this sets the reference to @NULL@. This only works on @Maybe@ foreign keys.
* 'SetDefault' - This will set the column's value to the @default@ for the column, if specified.

Expand Down
6 changes: 3 additions & 3 deletions persistent/Database/Persist/Sql/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ mkColumns allDefs t overrides =
$ ref (fieldDB fd) (fieldReference fd) (fieldAttrs fd)

-- a 'Nothing' in the definition means that the QQ migration doesn't
-- specify behavior. the default is RESTRICT. setting this here
-- specify behavior. the default is NO ACTION. setting this here
-- explicitly makes migrations run smoother.
overrideNothings (FieldCascade { fcOnUpdate = upd, fcOnDelete = del }) =
FieldCascade
{ fcOnUpdate = upd <|> Just Restrict
, fcOnDelete = del <|> Just Restrict
{ fcOnUpdate = upd <|> Just NoAction
, fcOnDelete = del <|> Just NoAction
}

ref :: FieldNameDB
Expand Down
5 changes: 3 additions & 2 deletions persistent/Database/Persist/Types/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ data ForeignDef = ForeignDef
-- This type is used in both parsing the model definitions and performing
-- migrations. A 'Nothing' in either of the field values means that the
-- user has not specified a 'CascadeAction'. An unspecified 'CascadeAction'
-- is defaulted to 'Restrict' when doing migrations.
-- is defaulted to 'NoAction' when doing migrations.
--
-- @since 2.11.0
data FieldCascade = FieldCascade
Expand Down Expand Up @@ -604,7 +604,7 @@ renderFieldCascade (FieldCascade onUpdate onDelete) =
-- change.
--
-- @since 2.11.0
data CascadeAction = Cascade | Restrict | SetNull | SetDefault
data CascadeAction = NoAction | Cascade | Restrict | SetNull | SetDefault
deriving (Show, Eq, Read, Ord, Lift)

-- | Render a 'CascadeAction' to 'Text' such that it can be used in a SQL
Expand All @@ -613,6 +613,7 @@ data CascadeAction = Cascade | Restrict | SetNull | SetDefault
-- @since 2.11.0
renderCascadeAction :: CascadeAction -> Text
renderCascadeAction action = case action of
NoAction -> "NO ACTION"
Cascade -> "CASCADE"
Restrict -> "RESTRICT"
SetNull -> "SET NULL"
Expand Down