Skip to content

Commit

Permalink
Adds ON DELETE SET DEFAULT to schema designer
Browse files Browse the repository at this point in the history
Fixes #372
  • Loading branch information
mpscholten committed Nov 20, 2020
1 parent 831a124 commit c84a7af
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions IHP/IDE/SchemaDesigner/Compiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ compileOnDelete Nothing = ""
compileOnDelete (Just NoAction) = "ON DELETE NO ACTION"
compileOnDelete (Just Restrict) = "ON DELETE RESTRICT"
compileOnDelete (Just SetNull) = "ON DELETE SET NULL"
compileOnDelete (Just SetDefault) = "ON DELETE SET DEFAULT"
compileOnDelete (Just Cascade) = "ON DELETE CASCADE"

compileColumn :: PrimaryKeyConstraint -> Column -> Text
Expand Down
2 changes: 2 additions & 0 deletions IHP/IDE/SchemaDesigner/Controller/Columns.hs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ instance Controller ColumnsController where
Just NoAction -> do pure "NoAction"
Just Restrict -> do pure "Restrict"
Just SetNull -> do pure "SetNull"
Just SetDefault -> do pure "SetDefault"
Just Cascade -> do pure "Cascade"
Nothing -> do pure "NoAction"
render EditForeignKeyView { .. }
Expand All @@ -157,6 +158,7 @@ instance Controller ColumnsController where
let onDelete = case onDeleteParam of
"Restrict" -> Restrict
"SetNull" -> SetNull
"SetDefault" -> SetDefault
"Cascade" -> Cascade
_ -> NoAction
case constraintId of
Expand Down
2 changes: 1 addition & 1 deletion IHP/IDE/SchemaDesigner/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ parseUniqueConstraint = do
parseOnDelete = choice
[ (lexeme "NO" >> lexeme "ACTION") >> pure NoAction
, (lexeme "RESTRICT" >> pure Restrict)
, (lexeme "SET" >> lexeme "NULL") >> pure SetNull
, (lexeme "SET" >> ((lexeme "NULL" >> pure SetNull) <|> (lexeme "DEFAULT" >> pure SetDefault)))
, (lexeme "CASCADE" >> pure Cascade)
]

Expand Down
1 change: 1 addition & 0 deletions IHP/IDE/SchemaDesigner/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ data OnDelete
= NoAction
| Restrict
| SetNull
| SetDefault
| Cascade
deriving (Show, Eq)

Expand Down
1 change: 1 addition & 0 deletions IHP/IDE/SchemaDesigner/View/Columns/EditForeignKey.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ instance View EditForeignKeyView where
{onDeleteSelector "NoAction"}
{onDeleteSelector "Restrict"}
{onDeleteSelector "SetNull"}
{onDeleteSelector "SetDefault"}
{onDeleteSelector "Cascade"}
</select>
</div>
Expand Down
1 change: 1 addition & 0 deletions IHP/IDE/SchemaDesigner/View/Columns/NewForeignKey.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ instance View NewForeignKeyView where
{onDeleteSelector "NoAction"}
{onDeleteSelector "Restrict"}
{onDeleteSelector "SetNull"}
{onDeleteSelector "SetDefault"}
{onDeleteSelector "Cascade"}
</select>
</div>
Expand Down
15 changes: 14 additions & 1 deletion Test/IDE/SchemaDesigner/CompilerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,20 @@ tests = do
}
}
compileSql [statement] `shouldBe` "ALTER TABLE users ADD CONSTRAINT users_ref_company_id FOREIGN KEY (company_id) REFERENCES companies (id) ON DELETE CASCADE;\n"


it "should compile ALTER TABLE .. ADD FOREIGN KEY .. ON DELETE SET DEFAULT" do
let statement = AddConstraint
{ tableName = "users"
, constraintName = "users_ref_company_id"
, constraint = ForeignKeyConstraint
{ columnName = "company_id"
, referenceTable = "companies"
, referenceColumn = "id"
, onDelete = Just SetDefault
}
}
compileSql [statement] `shouldBe` "ALTER TABLE users ADD CONSTRAINT users_ref_company_id FOREIGN KEY (company_id) REFERENCES companies (id) ON DELETE SET DEFAULT;\n"

it "should compile ALTER TABLE .. ADD FOREIGN KEY .. ON DELETE SET NULL" do
let statement = AddConstraint
{ tableName = "users"
Expand Down
12 changes: 12 additions & 0 deletions Test/IDE/SchemaDesigner/ParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ tests = do
}
}

it "should parse ALTER TABLE .. ADD FOREIGN KEY .. ON DELETE SET DEFAULT" do
parseSql "ALTER TABLE users ADD CONSTRAINT users_ref_company_id FOREIGN KEY (company_id) REFERENCES companies (id) ON DELETE SET DEFAULT;" `shouldBe` AddConstraint
{ tableName = "users"
, constraintName = "users_ref_company_id"
, constraint = ForeignKeyConstraint
{ columnName = "company_id"
, referenceTable = "companies"
, referenceColumn = "id"
, onDelete = Just SetDefault
}
}

it "should parse ALTER TABLE .. ADD FOREIGN KEY .. ON DELETE SET NULL" do
parseSql "ALTER TABLE users ADD CONSTRAINT users_ref_company_id FOREIGN KEY (company_id) REFERENCES companies (id) ON DELETE SET NULL;" `shouldBe` AddConstraint
{ tableName = "users"
Expand Down

0 comments on commit c84a7af

Please sign in to comment.