Skip to content

[Refactor:API] Semester to Term in Database Calls #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nightly_db_backup/db_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def main():

# GET ACTIVE COURSES FROM 'MASTER' DB
try:
sql = "select course from courses where semester='{}'".format(semester)
sql = "select course from courses where term='{}'".format(semester)
# psql postgresql://user:password@host/dbname?sslmode=prefer -c "COPY (SQL code) TO STDOUT"
process = "psql postgresql://{}:{}@{}/submitty?sslmode=prefer -c \"COPY ({}) TO STDOUT\"".format(DB_USER, DB_PASS, DB_HOST, sql)
result = list(subprocess.check_output(process, shell=True).decode('utf-8').split(os.linesep))[:-1]
Expand Down
6 changes: 3 additions & 3 deletions pam_accounts/accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private function init() {
self::$db_query = <<<SQL
SELECT DISTINCT user_id
FROM courses_users
WHERE semester=$1
WHERE term=$1
SQL;
self::$workflow_function = 'add_user';
break;
Expand All @@ -141,7 +141,7 @@ private function init() {
self::$db_query = <<<SQL
SELECT DISTINCT cu.user_id
FROM courses_users as cu
LEFT OUTER JOIN courses as c ON cu.course=c.course AND cu.semester=c.semester
LEFT OUTER JOIN courses as c ON cu.course=c.course AND cu.term=c.term
WHERE cu.user_group=1 OR (cu.user_group<>1 AND c.status=1)
SQL;
self::$workflow_function = 'add_user';
Expand All @@ -163,7 +163,7 @@ private function init() {
self::$db_query = <<<SQL
SELECT DISTINCT cu.user_id
FROM courses_users as cu
LEFT OUTER JOIN courses as c ON cu.course=c.course AND cu.semester=c.semester
LEFT OUTER JOIN courses as c ON cu.course=c.course AND cu.term=c.term
WHERE cu.user_group<>1 AND c.status<>1
SQL;
self::$workflow_function = 'remove_user';
Expand Down
6 changes: 3 additions & 3 deletions sample_bin/registration_section_resync.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private function process() {
//Loop through academic terms
foreach (self::$academic_terms as $term) {
//Get courses list for $term
$res = pg_query_params(self::$db_conn['master'], "SELECT course FROM courses WHERE semester = $1", array($term));
$res = pg_query_params(self::$db_conn['master'], "SELECT course FROM courses WHERE term = $1", array($term));
if ($res === false) {
exit(sprintf("Error reading course list for %s.%s", $term, PHP_EOL));
}
Expand All @@ -107,7 +107,7 @@ private function process() {
}

//First retrieve registration sections in master DB
$res = pg_query_params(self::$db_conn['master'], "SELECT registration_section_id FROM courses_registration_sections WHERE semester=$1 AND course=$2", array($term, $course));
$res = pg_query_params(self::$db_conn['master'], "SELECT registration_section_id FROM courses_registration_sections WHERE term=$1 AND course=$2", array($term, $course));
if ($res === false) {
fprintf(STDERR, "Error reading registration sections from master DB: %s %s.%sSkipping %s %s.%s", $term, $course, PHP_EOL, $term, $course, PHP_EOL);
$this->close_db_conn('course');
Expand Down Expand Up @@ -135,7 +135,7 @@ private function process() {

//INSERT $sync_list to master DB, ON CONFLICT DO NOTHING (prevents potential PK violations). We're using DB schema trigger to complete resync.
foreach($sync_list as $section) {
$res = pg_query_params(self::$db_conn['master'], "INSERT INTO courses_registration_sections (semester, course, registration_section_id) VALUES ($1, $2, $3) ON CONFLICT ON CONSTRAINT courses_registration_sections_pkey DO UPDATE SET semester=$1, course=$2, registration_section_id=$3", array($term, $course, $section));
$res = pg_query_params(self::$db_conn['master'], "INSERT INTO courses_registration_sections (term, course, registration_section_id) VALUES ($1, $2, $3) ON CONFLICT ON CONSTRAINT courses_registration_sections_pkey DO UPDATE SET term=$1, course=$2, registration_section_id=$3", array($term, $course, $section));
if ($res === false) {
fprintf(STDERR, "Error during re-sync procedure: %s %s section %s.%s.This section could not be synced.%s", $term, $course, $section, PHP_EOL, PHP_EOL);
$this->close_db_conn('course');
Expand Down
16 changes: 8 additions & 8 deletions student_auto_feed/add_drop_report.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static function get_courses($term) {
}

// Undergraduate courses from DB.
$sql = "SELECT course FROM courses WHERE semester=$1 AND status=1";
$sql = "SELECT course FROM courses WHERE term=$1 AND status=1";
$params = array($term);
$res = pg_query_params(self::$db, $sql, $params);
if ($res === false)
Expand All @@ -170,7 +170,7 @@ public static function get_mapped_courses($term) {
}

// mapped courses from DB
$sql = "SELECT course, mapped_course FROM mapped_courses WHERE semester=$1";
$sql = "SELECT course, mapped_course FROM mapped_courses WHERE term=$1";
$params = array($term);
$res = pg_query_params(self::$db, $sql, $params);
if ($res === false) {
Expand Down Expand Up @@ -206,44 +206,44 @@ public static function count_enrollments($term, $course_list, $mapped_courses) {
$grad_course = array_search($course, $mapped_courses);
if ($grad_course === false) {
// COURSE HAS NO GRAD SECTION (not mapped).
$sql = "SELECT COUNT(*) FROM courses_users WHERE semester=$1 AND course=$2 AND user_group=4 AND registration_section IS NOT NULL";
$sql = "SELECT COUNT(*) FROM courses_users WHERE term=$1 AND course=$2 AND user_group=4 AND registration_section IS NOT NULL";
$params = array($term, $course);
$res = pg_query_params(self::$db, $sql, $params);
if ($res === false)
die("Failed to lookup enrollments for {$course}\n");
$course_enrollments[$course] = (int) pg_fetch_result($res, 0);

// Get manual flag count
$sql = "SELECT COUNT(*) FROM courses_users WHERE semester=$1 AND course=$2 AND user_group=4 AND registration_section IS NOT NULL AND manual_registration=TRUE";
$sql = "SELECT COUNT(*) FROM courses_users WHERE term=$1 AND course=$2 AND user_group=4 AND registration_section IS NOT NULL AND manual_registration=TRUE";
$res = pg_query_params(self::$db, $sql, $params);
if ($res === false)
die("Failed to lookup counts with manual flag set for {$course}\n");
$manual_flags[$course] = (int) pg_fetch_result($res, 0);
} else {
// UNDERGRADUATE SECTION
$sql = "SELECT COUNT(*) FROM courses_users WHERE semester=$1 AND course=$2 AND user_group=4 AND registration_section='1'";
$sql = "SELECT COUNT(*) FROM courses_users WHERE term=$1 AND course=$2 AND user_group=4 AND registration_section='1'";
$params = array($term, $course);
$res = pg_query_params(self::$db, $sql, $params);
if ($res === false)
die("Failed to lookup enrollments for {$course}\n");
$course_enrollments[$course] = (int) pg_fetch_result($res, 0);

// Get manual flag count
$sql = "SELECT COUNT(*) FROM courses_users WHERE semester=$1 AND course=$2 AND user_group=4 AND registration_section='1' AND manual_registration=TRUE";
$sql = "SELECT COUNT(*) FROM courses_users WHERE term=$1 AND course=$2 AND user_group=4 AND registration_section='1' AND manual_registration=TRUE";
$res = pg_query_params(self::$db, $sql, $params);
if ($res === false)
die("Failed to lookup counts with manual flag set for {$course} (undergrads)\n");
$manual_flags[$course] = (int) pg_fetch_result($res, 0);

// GRADUATE SECTION
$sql = "SELECT COUNT(*) FROM courses_users WHERE semester=$1 AND course=$2 AND user_group=4 AND registration_section='2'";
$sql = "SELECT COUNT(*) FROM courses_users WHERE term=$1 AND course=$2 AND user_group=4 AND registration_section='2'";
$res = pg_query_params(self::$db, $sql, $params);
if ($res === false)
die("Failed to lookup enrollments for {$grad_course}\n");
$course_enrollments[$grad_course] = (int) pg_fetch_result($res, 0);

// Get manual flag count
$sql = "SELECT COUNT(*) FROM courses_users WHERE semester=$1 AND course=$2 AND user_group=4 AND registration_section='2' AND manual_registration=TRUE";
$sql = "SELECT COUNT(*) FROM courses_users WHERE term=$1 AND course=$2 AND user_group=4 AND registration_section='2' AND manual_registration=TRUE";
$res = pg_query_params(self::$db, $sql, $params);
if ($res === false)
die("Failed to lookup counts with manual flag set for {$course} (grads)\n");
Expand Down
16 changes: 8 additions & 8 deletions student_auto_feed/ssaf_sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ class sql {
public const GET_COURSES = <<<SQL
SELECT course
FROM courses
WHERE semester=$1
WHERE term=$1
AND status=1
SQL;

public const GET_MAPPED_COURSES = <<<SQL
SELECT course, registration_section, mapped_course, mapped_section
FROM mapped_courses
WHERE semester=$1
WHERE term=$1
SQL;

public const GET_COURSE_ENROLLMENT_COUNT = <<<SQL
SELECT count(*) AS num_students FROM courses_users
WHERE semester=$1
WHERE term=$1
AND course=$2
AND user_group=4
AND registration_section IS NOT NULL
Expand Down Expand Up @@ -72,15 +72,15 @@ class sql {
// UPSERT courses_users table
public const UPSERT_COURSES_USERS = <<<SQL
INSERT INTO courses_users (
semester,
term,
course,
user_id,
user_group,
registration_section,
registration_type,
manual_registration
) VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (semester, course, user_id) DO UPDATE
ON CONFLICT (term, course, user_id) DO UPDATE
SET registration_section=
CASE WHEN courses_users.user_group=4
AND courses_users.manual_registration=FALSE
Expand All @@ -98,7 +98,7 @@ class sql {
// INSERT courses_registration_sections table
public const INSERT_REG_SECTION = <<<SQL
INSERT INTO courses_registration_sections (
semester,
term,
course,
registration_section_id,
course_section_id
Expand Down Expand Up @@ -135,12 +135,12 @@ class sql {
LEFT OUTER JOIN tmp_enrolled
ON courses_users.user_id=tmp_enrolled.user_id
WHERE tmp_enrolled.user_id IS NULL
AND courses_users.semester=$1
AND courses_users.term=$1
AND courses_users.course=$2
AND courses_users.user_group=4
) AS dropped
WHERE courses_users.user_id=dropped.user_id
AND courses_users.semester=$1
AND courses_users.term=$1
AND courses_users.course=$2
AND courses_users.user_group=4
AND courses_users.manual_registration=FALSE
Expand Down