1+ -- -------------------------------------------------------------------------
2+ -- Migration Script for f_4charcourses
3+ --
4+ -- @author Benjamin Russell ([email protected] )5+ -- @descrip Migration script to implement the changes for f_4charcourses.
6+ -- basically sets the course number field to a varchar(4) to enable
7+ -- courses that have letters in their number (eg. PHYS-211A)
8+ -- -------------------------------------------------------------------------
9+
10+ ALTER TABLE ` courses`
11+ CHANGE ` course` ` course` VARCHAR (4 ) NOT NULL ;
12+
13+ delimiter //
14+ DROP PROCEDURE IF EXISTS InsertOrUpdateCourse//
15+ CREATE PROCEDURE InsertOrUpdateCourse(
16+ IN p_quarter INT ,
17+ IN p_department_num INT ,
18+ IN p_department_code VARCHAR (4 ),
19+ IN p_course VARCHAR (4 ),
20+ IN p_credits INT ,
21+ IN p_title VARCHAR (50 ),
22+ IN p_description TEXT
23+ )
24+ BEGIN
25+ -- Determine if the course already exists
26+ DECLARE recordFound INT (1 );
27+ DECLARE v_department INT (10 );
28+
29+ -- Select the department ID for the course
30+ IF p_quarter > 20130 THEN
31+ -- NOTE: This LIMIT 1 looks jank as fuck. Here's why it's important: There are some
32+ -- departments under semesters that condensed multiple departments from quarters
33+ -- (eg: CSCI is made from 4003 and 4005). That's ok. Problems arose because when deciding
34+ -- which department record would own a course under semesters, this query would return
35+ -- multiple records. Limiting it to one seems like a cop-out since (eg here) what if
36+ -- a CSCI course belongs to 4003?? The truth: It doesn't matter. By limiting it to one, we
37+ -- are storing all semester courses under the same department record (a "random" CSCI
38+ -- one not the 4003 CSCI or the 4005 CSCI). Ordering it will guarantee it's the same one each time.
39+ SET v_department = (SELECT d .id FROM departments AS d WHERE d .code = p_department_code ORDER BY d .number LIMIT 1 );
40+ ELSE
41+ SET v_department = (SELECT d .id FROM departments AS d WHERE d .number = p_department_num);
42+ END IF;
43+
44+ -- Does the record exist?
45+ SET recordFound = (
46+ SELECT COUNT (* )
47+ FROM courses AS c
48+ WHERE c .department = v_department
49+ AND c .course = p_course
50+ AND c .quarter = p_quarter
51+ );
52+ IF recordFound > 0 THEN
53+ -- Course exists, so update it
54+ UPDATE courses AS c
55+ SET c .title = p_title, c .description = p_description, c .credits = p_credits
56+ WHERE c .department = v_department AND course = p_course AND quarter = p_quarter;
57+ SELECT " updated" AS action;
58+ ELSE
59+ -- Course doesn't exist, so insert it
60+ INSERT INTO courses (quarter, department, course, title, description, credits)
61+ VALUES (p_quarter, v_department, p_course, p_title, p_description, p_credits);
62+ SELECT " inserted" AS action;
63+ END IF;
64+
65+ -- Return the id of the course
66+ SELECT c .id FROM courses AS c WHERE c .department = v_department AND c .course = p_course AND c .quarter = p_quarter;
67+ END//
0 commit comments