Skip to content

Commit d88aa06

Browse files
authored
Add delete procedures (#9)
* Update foreign key constraints in database schema and add delete_person procedure * Add delete_comment procedure to remove comments by ID * Add delete_comment_like procedure to remove likes from comments by ID * Add delete procedures for ingredient and ingredient_translation * Add delete procedures for language and language_icon * Add delete procedures for person_avatar, person_responsibility, and person_setting * Add delete procedure for picture to remove pictures by ID * Add delete procedures for recipe, recipe engagement, recipe ingredient, recipe rating, recipe tag, and recipe translation * Add delete procedures for responsibility and tag
1 parent 2b0ff11 commit d88aa06

20 files changed

+326
-5
lines changed

database.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ CREATE OR REPLACE TABLE person (
2020
registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
2121
last_login TIMESTAMP NULL,
2222
language_id INT NULL,
23-
FOREIGN KEY (language_id) REFERENCES lang (language_id) ON DELETE SET NULL
23+
FOREIGN KEY (language_id) REFERENCES lang (language_id) ON DELETE RESTRICT
2424
) ENGINE = InnoDB;
2525

2626
CREATE OR REPLACE TABLE picture (
@@ -88,7 +88,7 @@ CREATE OR REPLACE TABLE ingredient_translation (
8888

8989
CREATE OR REPLACE TABLE recipe (
9090
recipe_id INT AUTO_INCREMENT PRIMARY KEY,
91-
author_id INT,
91+
author_id INT NULL,
9292
publication_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
9393
modification_date TIMESTAMP NULL,
9494
picture_id INT NULL,
@@ -97,8 +97,8 @@ CREATE OR REPLACE TABLE recipe (
9797
number_of_reviews INT NULL,
9898
source VARCHAR(255) NULL,
9999
recipe_status ENUM('draft', 'published', 'hidden', 'archived', 'pending review', 'rejected', 'scheduled', 'needs update', 'unlisted', 'deleted') NOT NULL DEFAULT 'draft',
100-
FOREIGN KEY (author_id) REFERENCES person (person_id) ON DELETE CASCADE,
101-
FOREIGN KEY (picture_id) REFERENCES picture (picture_id) ON DELETE SET NULL
100+
FOREIGN KEY (author_id) REFERENCES person (person_id) ON DELETE SET NULL,
101+
FOREIGN KEY (picture_id) REFERENCES picture (picture_id) ON DELETE CASCADE
102102
) ENGINE = InnoDB;
103103

104104
CREATE OR REPLACE TABLE recipe_ingredient (
@@ -121,7 +121,7 @@ CREATE OR REPLACE TABLE recipe_translation (
121121
video_url VARCHAR(255) NULL,
122122
PRIMARY KEY (recipe_id, language_id),
123123
FOREIGN KEY (recipe_id) REFERENCES recipe (recipe_id) ON DELETE CASCADE,
124-
FOREIGN KEY (language_id) REFERENCES lang (language_id) ON DELETE CASCADE
124+
FOREIGN KEY (language_id) REFERENCES lang (language_id) ON DELETE RESTRICT
125125
) ENGINE = InnoDB;
126126

127127
CREATE OR REPLACE TABLE recipe_engagement (

procedures/delete/comment.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Use the database
2+
USE smartcooking;
3+
4+
DELIMITER //
5+
6+
CREATE OR REPLACE PROCEDURE delete_comment(
7+
IN p_comment_id INT
8+
)
9+
BEGIN
10+
DELETE FROM comment
11+
WHERE comment_id = p_comment_id;
12+
END //
13+
14+
DELIMITER ;

procedures/delete/comment_like.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Use the database
2+
USE smartcooking;
3+
4+
DELIMITER //
5+
6+
CREATE OR REPLACE PROCEDURE delete_comment_like(
7+
IN p_comment_id INT,
8+
IN p_person_id INT
9+
)
10+
BEGIN
11+
DELETE FROM comment_like
12+
WHERE comment_id = p_comment_id AND person_id = p_person_id;
13+
END //
14+
15+
DELIMITER ;

procedures/delete/ingredient.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Use the database
2+
USE smartcooking;
3+
4+
DELIMITER //
5+
6+
CREATE OR REPLACE PROCEDURE delete_ingredient(
7+
IN p_ingredient_id INT
8+
)
9+
BEGIN
10+
DELETE FROM ingredient
11+
WHERE id = p_ingredient_id;
12+
END //
13+
14+
DELIMITER ;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Use the database
2+
USE smartcooking;
3+
4+
DELIMITER //
5+
6+
CREATE OR REPLACE PROCEDURE delete_ingredient_translation(
7+
IN p_ingredient_id INT,
8+
IN p_language_id INT
9+
)
10+
BEGIN
11+
DELETE FROM ingredient_translation
12+
WHERE ingredient_id = p_ingredient_id AND language_id = p_language_id;
13+
END //
14+
15+
DELIMITER ;

procedures/delete/language.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- Use the database
2+
USE smartcooking;
3+
4+
DELIMITER //
5+
6+
CREATE OR REPLACE PROCEDURE delete_language(
7+
IN p_language_id INT
8+
)
9+
BEGIN
10+
DELETE FROM lang
11+
WHERE id = p_language_id;
12+
END //
13+
14+
CREATE OR REPLACE PROCEDURE delete_language_by_iso_code(
15+
IN p_iso_code VARCHAR(2)
16+
)
17+
BEGIN
18+
DELETE FROM lang
19+
WHERE iso_code = p_iso_code;
20+
END //
21+
22+
DELIMITER ;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Use the database
2+
USE smartcooking;
3+
4+
DELIMITER //
5+
6+
CREATE OR REPLACE PROCEDURE delete_language_icon(
7+
IN p_language_id INT
8+
)
9+
BEGIN
10+
DELETE FROM language_icon
11+
WHERE language_id = p_language_id;
12+
END //
13+
14+
DELIMITER ;

procedures/delete/person.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Use the database
2+
USE smartcooking;
3+
4+
DELIMITER //
5+
6+
CREATE OR REPLACE PROCEDURE delete_person(
7+
IN p_person_id INT
8+
)
9+
BEGIN
10+
DELETE FROM person
11+
WHERE person_id = p_person_id;
12+
END //
13+
14+
15+
DELIMITER ;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Use the database
2+
USE smartcooking;
3+
4+
DELIMITER //
5+
6+
CREATE OR REPLACE PROCEDURE delete_person_avatar(
7+
IN p_person_id INT
8+
)
9+
BEGIN
10+
DELETE FROM person_avatar
11+
WHERE person_id = p_person_id;
12+
END //
13+
14+
DELIMITER ;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Use the database
2+
USE smartcooking;
3+
4+
DELIMITER //
5+
6+
CREATE OR REPLACE PROCEDURE delete_person_responsibility(
7+
IN p_person_id INT,
8+
IN p_responsibility_id INT
9+
)
10+
BEGIN
11+
DELETE FROM person_responsibility
12+
WHERE person_id = p_person_id AND responsibility_id = p_responsibility_id;
13+
END //
14+
15+
DELIMITER ;

0 commit comments

Comments
 (0)