From 7d3c070328b33b45341f0daf7e26e0a44c5f362e Mon Sep 17 00:00:00 2001 From: Daniel Trowbridge Date: Sat, 15 Jul 2023 12:17:37 +0100 Subject: [PATCH] Add NO ACTION cascade action and make it the default --- persistent/Database/Persist/Quasi.hs | 5 +++-- persistent/Database/Persist/Sql/Internal.hs | 6 +++--- persistent/Database/Persist/Types/Base.hs | 13 +++++++++++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/persistent/Database/Persist/Quasi.hs b/persistent/Database/Persist/Quasi.hs index e020401ae..4f19a9bc3 100644 --- a/persistent/Database/Persist/Quasi.hs +++ b/persistent/Database/Persist/Quasi.hs @@ -423,8 +423,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 since 2.15.0.0. It checks constraints at the end of the transaction, aborting it if a violation is found. +* 'Restrict' - This is the default prior to 2.15.0.0. It immediately checks constraints, aborting the transaction if a violation is found. +* '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. diff --git a/persistent/Database/Persist/Sql/Internal.hs b/persistent/Database/Persist/Sql/Internal.hs index c8e099fee..a8875e966 100644 --- a/persistent/Database/Persist/Sql/Internal.hs +++ b/persistent/Database/Persist/Sql/Internal.hs @@ -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 diff --git a/persistent/Database/Persist/Types/Base.hs b/persistent/Database/Persist/Types/Base.hs index b17def38b..483013e54 100644 --- a/persistent/Database/Persist/Types/Base.hs +++ b/persistent/Database/Persist/Types/Base.hs @@ -555,7 +555,10 @@ 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' in versions @>=2.15.0.0@ +-- * 'Restrict' in versions @<2.15.0.0@ -- -- @since 2.11.0 data FieldCascade = FieldCascade @@ -585,7 +588,12 @@ renderFieldCascade (FieldCascade onUpdate onDelete) = -- change. -- -- @since 2.11.0 -data CascadeAction = Cascade | Restrict | SetNull | SetDefault +data CascadeAction + = Cascade + | NoAction -- ^ @since 2.15.0.0 + | Restrict + | SetNull + | SetDefault deriving (Show, Eq, Read, Ord, Lift) -- | Render a 'CascadeAction' to 'Text' such that it can be used in a SQL @@ -595,6 +603,7 @@ data CascadeAction = Cascade | Restrict | SetNull | SetDefault renderCascadeAction :: CascadeAction -> Text renderCascadeAction action = case action of Cascade -> "CASCADE" + NoAction -> "NO ACTION" Restrict -> "RESTRICT" SetNull -> "SET NULL" SetDefault -> "SET DEFAULT"