Skip to content

Commit

Permalink
Update schema to match Postgres syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam-Cordry committed Feb 15, 2024
1 parent dc4be5f commit 081104a
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 107 deletions.
11 changes: 8 additions & 3 deletions backend/schema/academicterms.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@

-- TABLE CREATION ----------------------------------------------------------
CREATE TABLE IF NOT EXISTS academicterms (
`term` SMALLINT(5) UNSIGNED PRIMARY KEY,
`start` DATE NOT NULL,
`end` DATE NOT NULL
term SMALLINT PRIMARY KEY,
start_date DATE NOT NULL,
end_date DATE NOT NULL
);

-- CONSTRAINTS -------------------------------------------------------------
ALTER TABLE academicterms
ADD CONSTRAINT CH_terms_pos
CHECK(term >= 0);

8 changes: 4 additions & 4 deletions backend/schema/buildings.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

-- TABLE CREATION ----------------------------------------------------------
CREATE TABLE IF NOT EXISTS buildings (
`number` VARCHAR(5) PRIMARY KEY,
`code` VARCHAR(5) UNIQUE NOT NULL,
`name` VARCHAR(100) NOT NULL,
`off_campus` BOOLEAN NOT NULL DEFAULT TRUE
number VARCHAR(5) PRIMARY KEY,
code VARCHAR(5) UNIQUE NOT NULL,
name VARCHAR(100) NOT NULL,
off_campus BOOLEAN NOT NULL DEFAULT TRUE
);

61 changes: 40 additions & 21 deletions backend/schema/courses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,49 @@

-- TABLE CREATION ----------------------------------------------------------
CREATE TABLE IF NOT EXISTS courses (
`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`term` SMALLINT UNSIGNED NOT NULL,
`department` INT UNSIGNED NOT NULL,
`course` VARCHAR(4) NOT NULL,
`credits` TINYINT(2) UNSIGNED NOT NULL,
`title` VARCHAR(50) NOT NULL,
`description` TEXT NOT NULL
id SERIAL PRIMARY KEY,
term SMALLINT NOT NULL,
department INT NOT NULL,
course VARCHAR(4) NOT NULL,
credits SMALLINT NOT NULL,
title VARCHAR(50) NOT NULL,
description TEXT NOT NULL
);

-- UNSIGNED CONSTRAINTS ----------------------------------------------------
ALTER TABLE courses
ADD CONSTRAINT CH_courses_id_pos
CHECK (id >= 0);

ALTER TABLE courses
ADD CONSTRAINT CH_courses_term_pos
CHECK (term >= 0);

ALTER TABLE courses
ADD CONSTRAINT CH_courses_dept_pos
CHECK (department >= 0);

ALTER TABLE courses
ADD CONSTRAINT CH_courses_credits_pos
CHECK (credits >= 0);

-- UNIQUE CONSTRAINT -------------------------------------------------------
ALTER TABLE `courses`
ADD CONSTRAINT UQ_courses_quarter_department_course
UNIQUE (`term`, `department`, `course`);
ALTER TABLE courses
ADD CONSTRAINT UQ_courses_term_dept_course
UNIQUE (term, department, course);

-- FOREIGN KEYS ------------------------------------------------------------
ALTER TABLE `courses`
ADD FOREIGN KEY FK_courses_quarter(`quarter`)
REFERENCES `academicterms`(`term`)
ON DELETE CASCADE
ON UPDATE CASCADE;

ALTER TABLE `courses`
ADD FOREIGN KEY FK_courses_dept(`department`)
REFERENCES `departments`(`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
ALTER TABLE courses
ADD CONSTRAINT FK_courses_term
FOREIGN KEY (term)
REFERENCES academicterms(term)
ON UPDATE CASCADE
ON DELETE CASCADE;

ALTER TABLE courses
ADD CONSTRAINT FK_courses_dept
FOREIGN KEY (department)
REFERENCES departments(id)
ON UPDATE CASCADE
ON DELETE CASCADE;

24 changes: 17 additions & 7 deletions backend/schema/departments.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,26 @@

-- TABLE CREATION ----------------------------------------------------------
CREATE TABLE IF NOT EXISTS departments (
`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`school` INT UNSIGNED UNIQUE NOT NULL,
`code` VARCHAR(4) UNIQUE NOT NULL,
`title` VARCHAR(100) NOT NULL
id SERIAL PRIMARY KEY,
school INT UNIQUE NOT NULL,
code VARCHAR(4) UNIQUE NOT NULL,
title VARCHAR(100) NOT NULL
);

-- UNSIGNED CONSTRAINT ----------------------------------------------------
ALTER TABLE departments
ADD CONSTRAINT CH_dept_id
CHECK (id >= 0);

ALTER TABLE departments
ADD CONSTRAINT CH_dept_school
CHECK (school >= 0);

-- FOREIGN KEYS -----------------------------------------------------------
ALTER TABLE `departments` ADD INDEX `departments`(`school`);
ALTER TABLE `departments` ADD CONSTRAINT FK_school FOREIGN KEY `departments`(`school`)
REFERENCES `schools`(`id`)
ALTER TABLE departments
ADD CONSTRAINT FK_dept_school
FOREIGN KEY (school)
REFERENCES schools(id)
ON UPDATE CASCADE
ON DELETE CASCADE;

41 changes: 28 additions & 13 deletions backend/schema/schedulecourses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,36 @@

-- TABLE CREATION ----------------------------------------------------------
CREATE TABLE IF NOT EXISTS schedulecourses (
`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`schedule` INT UNSIGNED NOT NULL,
`section` INT UNSIGNED NOT NULL
id SERIAL PRIMARY KEY,
schedule INT NOT NULL,
section INT NOT NULL
);

-- UNSIGNED CONSTRAINTS ----------------------------------------------------
ALTER TABLE schedulecourses
ADD CONSTRAINT CH_sc_id_pos
CHECK (id >= 0);

ALTER TABLE schedulecourses
ADD CONSTRAINT CH_sc_schedule_pos
CHECK (schedule >= 0);

ALTER TABLE schedulecourses
ADD CONSTRAINT CH_sc_section_pos
CHECK (section >= 0);

-- FOREIGN KEYS ------------------------------------------------------------
ALTER TABLE `schedulecourses`
ADD FOREIGN KEY FK_schedcourses_schedule(`schedule`)
REFERENCES `schedules`(`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
ALTER TABLE schedulecourses
ADD CONSTRAINT FK_sc_schedule
FOREIGN KEY (schedule)
REFERENCES schedules(id)
ON UPDATE CASCADE
ON DELETE CASCADE;

ALTER TABLE `schedulecourses`
ADD FOREIGN KEY FK schedcourses_section(`section`)
REFERENCES `sections`(`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
ALTER TABLE schedulecourses
ADD CONSTRAINT FK_sc_section
FOREIGN KEY (section)
REFERENCES schedules(id)
ON UPDATE CASCADE
ON DELETE CASCADE;

44 changes: 33 additions & 11 deletions backend/schema/schedulenoncourses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,40 @@

-- TABLE CREATION ----------------------------------------------------------
CREATE TABLE IF NOT EXISTS schedulenoncourses (
`id` INT UNSIGNED PRIMARY KEY,
`schedule` INT UNSIGNED NOT NULL,
`title` VARCHAR(30) NOT NULL,
`day` TINYINT(1) UNSIGNED NOT NULL,
`start` SMALLINT(4) UNSIGNED NOT NULL,
`end` SMALLINT(4) UNSIGNED NOT NULL
id SERIAL PRIMARY KEY,
schedule INT NOT NULL,
title VARCHAR(30) NOT NULL,
day SMALLINT NOT NULL,
start SMALLINT NOT NULL,
end SMALLINT NOT NULL
);

-- UNSIGNED CONSTRAINT -----------------------------------------------------
ALTER TABLE schedulenoncourses
ADD CONSTRAINT CH_snc_id_pos
CHECK (id >= 0);

ALTER TABLE schedulenoncourses
ADD CONSTRAINT CH_snc_schedule_pos
CHECK (schedule >= 0);

ALTER TABLE schedulenoncourses
ADD CONSTRAINT CH_snc_day_pos
CHECK (day >= 0);

ALTER TABLE schedulenoncourses
ADD CONSTRAINT CH_snc_start_pos
CHECK (start_time >= 0);

ALTER TABLE schedulenoncourses
ADD CONSTRAINT CH_snc_end_pos
CHECK (end_time >= 0);

-- FOREIGN KEYS ------------------------------------------------------------
ALTER TABLE `schedulenoncourses`
ADD FOREIGN KEY FK_schednoncourses_schedule(`schedule`)
REFERENCES `schedules`(`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
ALTER TABLE schedulenoncourses
ADD CONSTRAINT FK_snc_schedule
FOREIGN KEY (schedule)
REFERENCES schedules(id)
ON UPDATE CASCADE
ON DELETE CASCADE;

47 changes: 36 additions & 11 deletions backend/schema/schedules.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,46 @@

-- TABLE CREATION ----------------------------------------------------------
CREATE TABLE IF NOT EXISTS schedules (
`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`lastaccessed` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`startday` TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
`endday` TINYINT(1) UNSIGNED NOT NULL DEFAULT 6,
`starttime` TINYINT(2) UNSIGNED NOT NULL DEFAULT 8,
`endtime` TINYINT(2) UNSIGNED NOT NULL DEFAULT 22,
`term` SMALLINT(4) UNSIGNED NOT NULL,
`image` BOOL NOT NULL DEFAULT FALSE
id SERIAL PRIMARY KEY,
last_accessed TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
start_day SMALLINT NOT NULL DEFAULT 0,
end_day SMALLINT NOT NULL DEFAULT 6,
start_time SMALLINT NOT NULL DEFAULT 8,
end_time SMALLINT NOT NULL DEFAULT 22,
term SMALLINT NOT NULL,
image BOOLEAN NOT NULL DEFAULT FALSE
);

-- UNSIGNED CONSTRAINTS ---------------------------------------------------
ALTER TABLE schedules
ADD CONSTRAINT CH_sch_id_pos
CHECK (id >= 0);

ALTER TABLE schedules
ADD CONSTRAINT CH_sch_sday_pos
CHECK (start_day >= 0);

ALTER TABLE schedules
ADD CONSTRAINT CH_sch_eday_pos
CHECK (end_day >= 0);

ALTER TABLE schedules
ADD CONSTRAINT CH_sch_stime_pos
CHECK (start_time >= 0);

ALTER TABLE schedules
ADD CONSTRAINT CH_sch_etime_pos
CHECK (end_time >= 0);

ALTER TABLE schedules
ADD CONSTRAINT CH_sch_term_pos
CHECK (term >= 0);

-- FOREIGN KEY ------------------------------------------------------------
ALTER TABLE `schedules`
ALTER TABLE schedules
ADD CONSTRAINT FK_schedules_term
FOREIGN KEY `schedules`(`term`)
REFERENCES `academicterms`(`term`)
FOREIGN KEY (term)
REFERENCES academicterms(term)
ON UPDATE CASCADE
ON DELETE CASCADE;

11 changes: 8 additions & 3 deletions backend/schema/schools.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@

-- TABLE CREATION ----------------------------------------------------------
CREATE TABLE IF NOT EXISTS schools (
`id` INT UNSIGNED PRIMARY KEY,
`code` VARCHAR(5) UNIQUE NOT NULL,
`title` VARCHAR(30) NOT NULL
id SERIAL PRIMARY KEY,
code VARCHAR(5) UNIQUE NOT NULL,
title VARCHAR(30) NOT NULL
);

-- UNSIGNED CONSTRAINT ----------------------------------------------------
ALTER TABLE schools
ADD CONSTRAINT CH_schools_id_pos
CHECK (id >= 0);

49 changes: 41 additions & 8 deletions backend/schema/scrapelog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,46 @@

-- TABLE CREATION ----------------------------------------------------------
CREATE TABLE IF NOT EXISTS scrapelog (
`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`timestarted` INT(11) UNSIGNED NOT NULL,
`timeended` INT(11) UNSIGNED NOT NULL,
`termsadded` TINYINT(3) UNSIGNED NOT NULL,
`coursesadded` INT UNSIGNED NOT NULL,
`coursesupdated` INT UNSIGNED NOT NULL,
`sectionsadded` INT UNSIGNED NOT NULL,
`failures` INT UNSIGNED NOT NULL
id SERIAL PRIMARY KEY,
time_started INT NOT NULL,
time_ended INT NOT NULL,
terms_added SMALLINT NOT NULL,
courses_added INT NOT NULL,
courses_updated INT NOT NULL,
sections_added INT NOT NULL,
failures INT NOT NULL
);

-- UNSIGNED CONSTRAINTS ---------------------------------------------------
ALTER TABLE scrapelog
ADD CONSTRAINT CH_sl_id_pos
CHECK (id >= 0);

ALTER TABLE scrapelog
ADD CONSTRAINT CH_sl_timestart_pos
CHECK (time_started >= 0);

ALTER TABLE scrapelog
ADD CONSTRAINT CH_sl_timeend_pos
CHECK (time_ended >= 0);

ALTER TABLE scrapelog
ADD CONSTRAINT CH_sl_terms_pos
CHECK (terms_added >= 0);

ALTER TABLE scrapelog
ADD CONSTRAINT CH_sl_courseadd_pos
CHECK (courses_added >= 0);

ALTER TABLE scrapelog
ADD CONSTRAINT CH_sl_courseupdate_pos
CHECK (courses_updated >= 0);

ALTER TABLE scrapelog
ADD CONSTRAINT CH_sl_sectionadd_pos
CHECK (sections_added >= 0);

ALTER TABLE scrapelog
ADD CONSTRAINT CH_sl_falure_pos
CHECK (failures >= 0);

Loading

0 comments on commit 081104a

Please sign in to comment.