-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration for user group field (#1635)
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
modules/fbs-core/api/src/main/resources/migrations/21_user_group_field_added.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
BEGIN; | ||
|
||
CREATE TABLE IF NOT EXISTS `fbs`.`user_group` ( | ||
`user_id` INT NOT NULL, | ||
`course_id` INT NOT NULL, | ||
`group_id` INT NOT NULL, | ||
PRIMARY KEY (`user_id`, `course_id`, `group_id`), | ||
INDEX `user_has_groups_users_user_id_fk` (`user_id` ASC), | ||
CONSTRAINT `user_has_groups_users_user_id_fk` | ||
FOREIGN KEY (`user_id`) | ||
REFERENCES `fbs`.`user` (`user_id`) | ||
ON DELETE CASCADE | ||
ON UPDATE CASCADE, | ||
CONSTRAINT `user_group_course_course_id_fk` | ||
FOREIGN KEY (`course_id`) | ||
REFERENCES `fbs`.`course` (`course_id`) | ||
ON DELETE CASCADE | ||
ON UPDATE CASCADE, | ||
CONSTRAINT `user_group_group_group_id_fk` | ||
FOREIGN KEY (`group_id`) | ||
REFERENCES `fbs`.`group` (`group_id`) | ||
ON DELETE CASCADE | ||
ON UPDATE CASCADE | ||
) ENGINE = InnoDB | ||
DEFAULT CHARACTER SET = utf8mb4 | ||
COLLATE = utf8mb4_0900_ai_ci; | ||
|
||
-- ----------------------------------------------------- | ||
-- trigger to check if user is participant of the course | ||
-- ----------------------------------------------------- | ||
|
||
CREATE TRIGGER check_user_course_before_insert | ||
BEFORE INSERT ON `user_group` | ||
FOR EACH ROW | ||
BEGIN | ||
DECLARE user_in_course INT; | ||
SELECT COUNT(*) | ||
INTO user_in_course | ||
FROM user_course | ||
WHERE user_id = NEW.user_id AND course_id = NEW.course_id; | ||
|
||
IF user_in_course = 0 THEN | ||
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Der Nutzer gehört nicht zum angegebenen Kurs.'; | ||
END IF; | ||
END; | ||
|
||
|
||
INSERT INTO migration (number) VALUES (21); | ||
|
||
COMMIT; | ||
|