diff --git a/migrations/20250906104612_users.down.sql b/migrations/20250906104612_users.down.sql deleted file mode 100644 index a615fa5..0000000 --- a/migrations/20250906104612_users.down.sql +++ /dev/null @@ -1,3 +0,0 @@ --- Add down migration script here - -DROP TABLE IF EXISTS users; diff --git a/migrations/20250906104612_users.up.sql b/migrations/20250906104612_users.up.sql deleted file mode 100644 index b23f28a..0000000 --- a/migrations/20250906104612_users.up.sql +++ /dev/null @@ -1,13 +0,0 @@ --- Add up migration script here - -CREATE TABLE IF NOT EXISTS users ( - id BIGSERIAL PRIMARY KEY, - email TEXT NOT NULL UNIQUE, - verified_email BOOLEAN NOT NULL, - username TEXT NOT NULL UNIQUE, - pw_hash TEXT NOT NULL, - pw_salt TEXT NOT NULL, - pfp_id BIGINT NOT NULL, - rank_id BIGINT NOT NULL -); - diff --git a/migrations/20250906202709_sessions.down.sql b/migrations/20250906202709_sessions.down.sql deleted file mode 100644 index 7dad582..0000000 --- a/migrations/20250906202709_sessions.down.sql +++ /dev/null @@ -1,3 +0,0 @@ --- Add down migration script here - -DROP TABLE IF EXISTS sessions; diff --git a/migrations/20250906202709_sessions.up.sql b/migrations/20250906202709_sessions.up.sql deleted file mode 100644 index 8594fbb..0000000 --- a/migrations/20250906202709_sessions.up.sql +++ /dev/null @@ -1,8 +0,0 @@ --- Add up migration script here - -CREATE TABLE IF NOT EXISTS sessions ( - user_id BIGINT NOT NULL, - session_key TEXT NOT NULL UNIQUE, - expires BIGINT NOT NULL, - CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE -) diff --git a/migrations/20250913111557_ranks.down.sql b/migrations/20250913111557_ranks.down.sql deleted file mode 100644 index f37b13f..0000000 --- a/migrations/20250913111557_ranks.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE IF EXISTS ranks; diff --git a/migrations/20250913111557_ranks.up.sql b/migrations/20250913111557_ranks.up.sql deleted file mode 100644 index cd3f547..0000000 --- a/migrations/20250913111557_ranks.up.sql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE IF NOT EXISTS ranks ( - id BIGSERIAL PRIMARY KEY NOT NULL, - name TEXT NOT NULL, - description TEXT NOT NULL, - created_at BIGINT NOT NULL, - modified_at BIGINT NOT NULL -); diff --git a/migrations/20250913111613_awards.down.sql b/migrations/20250913111613_awards.down.sql deleted file mode 100644 index 396e1ae..0000000 --- a/migrations/20250913111613_awards.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE IF EXISTS awards; diff --git a/migrations/20250913111613_awards.up.sql b/migrations/20250913111613_awards.up.sql deleted file mode 100644 index 9d037f7..0000000 --- a/migrations/20250913111613_awards.up.sql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE IF NOT EXISTS awards ( - id BIGSERIAL PRIMARY KEY NOT NULL, - name TEXT NOT NULL, - description TEXT NOT NULL, - created_at BIGINT NOT NULL, - modified_at BIGINT NOT NULL -); diff --git a/migrations/20250913111614_trainings.down.sql b/migrations/20250913111614_trainings.down.sql deleted file mode 100644 index 45f557a..0000000 --- a/migrations/20250913111614_trainings.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE IF EXISTS trainings; diff --git a/migrations/20250913111614_trainings.up.sql b/migrations/20250913111614_trainings.up.sql deleted file mode 100644 index 5931fb8..0000000 --- a/migrations/20250913111614_trainings.up.sql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE IF NOT EXISTS trainings ( - id BIGSERIAL PRIMARY KEY NOT NULL, - name TEXT NOT NULL, - description TEXT NOT NULL, - created_at BIGINT NOT NULL, - modified_at BIGINT NOT NULL -); diff --git a/migrations/20250913111615_missions.down.sql b/migrations/20250913111615_missions.down.sql deleted file mode 100644 index 751b303..0000000 --- a/migrations/20250913111615_missions.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE IF EXISTS missions; diff --git a/migrations/20250913111615_missions.up.sql b/migrations/20250913111615_missions.up.sql deleted file mode 100644 index 8517e23..0000000 --- a/migrations/20250913111615_missions.up.sql +++ /dev/null @@ -1,9 +0,0 @@ -CREATE TABLE IF NOT EXISTS missions ( - id BIGSERIAL PRIMARY KEY NOT NULL, - name TEXT NOT NULL, - description TEXT NOT NULL, - starting_at BIGINT NOT NULL, - estimated_length BIGINT NOT NULL, - created_at BIGINT NOT NULL, - modified_at BIGINT NOT NULL -); diff --git a/migrations/20250913111617_qualifications.down.sql b/migrations/20250913111617_qualifications.down.sql deleted file mode 100644 index 0e7e962..0000000 --- a/migrations/20250913111617_qualifications.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE IF EXISTS qualifications; diff --git a/migrations/20250913111617_qualifications.up.sql b/migrations/20250913111617_qualifications.up.sql deleted file mode 100644 index a298b10..0000000 --- a/migrations/20250913111617_qualifications.up.sql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE IF NOT EXISTS qualifications ( - id BIGSERIAL PRIMARY KEY NOT NULL, - name TEXT NOT NULL, - description TEXT NOT NULL, - created_at BIGINT NOT NULL, - modified_at BIGINT NOT NULL -); diff --git a/migrations/20250913111736_records_ranks.down.sql b/migrations/20250913111736_records_ranks.down.sql deleted file mode 100644 index 59761a9..0000000 --- a/migrations/20250913111736_records_ranks.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE IF EXISTS record_ranks; diff --git a/migrations/20250913111736_records_ranks.up.sql b/migrations/20250913111736_records_ranks.up.sql deleted file mode 100644 index 8690371..0000000 --- a/migrations/20250913111736_records_ranks.up.sql +++ /dev/null @@ -1,11 +0,0 @@ -CREATE TABLE IF NOT EXISTS records_ranks ( - id BIGSERIAL PRIMARY KEY NOT NULL, - user_id BIGINT NOT NULL, - rank_id BIGINT NOT NULL, - author_id BIGINT NOT NULL, - created_at BIGINT NOT NULL, - - CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, - CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE SET NULL, - CONSTRAINT fk_rank FOREIGN KEY (rank_id) REFERENCES ranks(id) ON DELETE CASCADE -); diff --git a/migrations/20250913111748_records_awards.down.sql b/migrations/20250913111748_records_awards.down.sql deleted file mode 100644 index 343108e..0000000 --- a/migrations/20250913111748_records_awards.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE IF EXISTS record_awards; diff --git a/migrations/20250913111748_records_awards.up.sql b/migrations/20250913111748_records_awards.up.sql deleted file mode 100644 index b2acceb..0000000 --- a/migrations/20250913111748_records_awards.up.sql +++ /dev/null @@ -1,11 +0,0 @@ -CREATE TABLE IF NOT EXISTS records_awards ( - id BIGSERIAL PRIMARY KEY NOT NULL, - user_id BIGINT NOT NULL, - award_id BIGINT NOT NULL, - author_id BIGINT NOT NULL, - created_at BIGINT NOT NULL, - - CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, - CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE SET NULL, - CONSTRAINT fk_award FOREIGN KEY (award_id) REFERENCES awards(id) ON DELETE CASCADE -); diff --git a/migrations/20250913111802_records_training.down.sql b/migrations/20250913111802_records_training.down.sql deleted file mode 100644 index 63ca939..0000000 --- a/migrations/20250913111802_records_training.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE IF EXISTS record_training; diff --git a/migrations/20250913111802_records_training.up.sql b/migrations/20250913111802_records_training.up.sql deleted file mode 100644 index c2623e0..0000000 --- a/migrations/20250913111802_records_training.up.sql +++ /dev/null @@ -1,11 +0,0 @@ -CREATE TABLE IF NOT EXISTS records_trainings ( - id BIGSERIAL PRIMARY KEY NOT NULL, - user_id BIGINT NOT NULL, - training_id BIGINT NOT NULL, - author_id BIGINT NOT NULL, - created_at BIGINT NOT NULL, - - CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, - CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE SET NULL, - CONSTRAINT fk_training FOREIGN KEY (training_id) REFERENCES trainings(id) ON DELETE CASCADE -); diff --git a/migrations/20250913111810_records_qualifications.down.sql b/migrations/20250913111810_records_qualifications.down.sql deleted file mode 100644 index 3e2a7f4..0000000 --- a/migrations/20250913111810_records_qualifications.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE IF EXISTS record_qualifications; diff --git a/migrations/20250913111810_records_qualifications.up.sql b/migrations/20250913111810_records_qualifications.up.sql deleted file mode 100644 index 7702c35..0000000 --- a/migrations/20250913111810_records_qualifications.up.sql +++ /dev/null @@ -1,11 +0,0 @@ -CREATE TABLE IF NOT EXISTS records_qualifications ( - id BIGSERIAL PRIMARY KEY NOT NULL, - user_id BIGINT NOT NULL, - qualification_id BIGINT NOT NULL, - author_id BIGINT NOT NULL, - created_at BIGINT NOT NULL, - - CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, - CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE SET NULL, - CONSTRAINT fk_qualification FOREIGN KEY (qualification_id) REFERENCES qualifications(id) ON DELETE CASCADE -); diff --git a/migrations/20250913112041_records_missions.down.sql b/migrations/20250913112041_records_missions.down.sql deleted file mode 100644 index 6955a54..0000000 --- a/migrations/20250913112041_records_missions.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE IF EXISTS records_missions; diff --git a/migrations/20250913112041_records_missions.up.sql b/migrations/20250913112041_records_missions.up.sql deleted file mode 100644 index 35e9bb8..0000000 --- a/migrations/20250913112041_records_missions.up.sql +++ /dev/null @@ -1,11 +0,0 @@ -CREATE TABLE IF NOT EXISTS records_missions ( - id BIGSERIAL PRIMARY KEY NOT NULL, - user_id BIGINT NOT NULL, - mission_id BIGINT NOT NULL, - author_id BIGINT NOT NULL, - created_at BIGINT NOT NULL, - - CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, - CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE SET NULL, - CONSTRAINT fk_mission FOREIGN KEY (mission_id) REFERENCES missions(id) ON DELETE CASCADE -); diff --git a/migrations/20250913130129_attendance.down.sql b/migrations/20250913130129_attendance.down.sql deleted file mode 100644 index ebde145..0000000 --- a/migrations/20250913130129_attendance.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE IF EXISTS attendance; diff --git a/migrations/20250913130129_attendance.up.sql b/migrations/20250913130129_attendance.up.sql deleted file mode 100644 index eb7d586..0000000 --- a/migrations/20250913130129_attendance.up.sql +++ /dev/null @@ -1,10 +0,0 @@ -CREATE TABLE IF NOT EXISTS attendance ( - id BIGSERIAL PRIMARY KEY NOT NULL, - user_id BIGINT NOT NULL, - mission_id BIGINT NOT NULL, - confirmed_at BIGINT NOT NULL, - attending BOOL NOT NULL, - - CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL, - CONSTRAINT fk_mission FOREIGN KEY (mission_id) REFERENCES missions(id) ON DELETE CASCADE -); diff --git a/migrations/20250914121222_main.down.sql b/migrations/20250914121222_main.down.sql new file mode 100644 index 0000000..d6e8b83 --- /dev/null +++ b/migrations/20250914121222_main.down.sql @@ -0,0 +1,14 @@ +DROP TABLE IF EXISTS users; +DROP TABLE IF EXISTS sessions; +DROP TABLE IF EXISTS ranks; +DROP TABLE IF EXISTS awards; +DROP TABLE IF EXISTS trainings; +DROP TABLE IF EXISTS missions; +DROP TABLE IF EXISTS qualifications; +DROP TABLE IF EXISTS record_ranks; +DROP TABLE IF EXISTS record_awards; +DROP TABLE IF EXISTS record_training; +DROP TABLE IF EXISTS record_qualifications; +DROP TABLE IF EXISTS records_missions; +DROP TABLE IF EXISTS attendance; +DROP TABLE IF EXISTS roster_groups; diff --git a/migrations/20250914121222_main.up.sql b/migrations/20250914121222_main.up.sql new file mode 100644 index 0000000..c80cff2 --- /dev/null +++ b/migrations/20250914121222_main.up.sql @@ -0,0 +1,147 @@ +CREATE TABLE IF NOT EXISTS users ( + id BIGSERIAL PRIMARY KEY, + email TEXT NOT NULL UNIQUE, + verified_email BOOLEAN NOT NULL, + username TEXT NOT NULL UNIQUE, + pw_hash TEXT NOT NULL, + pw_salt TEXT NOT NULL, + pfp_id BIGINT NOT NULL, + rank_id BIGINT NOT NULL, + group_id BIGINT NOT NULL + + -- NOTE: Constraint added later to deal with circular dependency + -- CONSTRAINT fk_group FOREIGN KEY (group_id) REFERENCES roster_group(id) + -- CONSTRAINT fk_rank FOREIGN KEY (rank_id) REFERENCES ranks(id) +); + +CREATE TABLE IF NOT EXISTS sessions ( + user_id BIGINT NOT NULL, + session_key TEXT NOT NULL UNIQUE, + expires BIGINT NOT NULL, + CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS ranks ( + id BIGSERIAL PRIMARY KEY NOT NULL, + name TEXT NOT NULL, + description TEXT NOT NULL, + created_at BIGINT NOT NULL, + modified_at BIGINT NOT NULL +); +CREATE TABLE IF NOT EXISTS awards ( + id BIGSERIAL PRIMARY KEY NOT NULL, + name TEXT NOT NULL, + description TEXT NOT NULL, + created_at BIGINT NOT NULL, + modified_at BIGINT NOT NULL +); +CREATE TABLE IF NOT EXISTS trainings ( + id BIGSERIAL PRIMARY KEY NOT NULL, + name TEXT NOT NULL, + description TEXT NOT NULL, + created_at BIGINT NOT NULL, + modified_at BIGINT NOT NULL +); +CREATE TABLE IF NOT EXISTS missions ( + id BIGSERIAL PRIMARY KEY NOT NULL, + name TEXT NOT NULL, + description TEXT NOT NULL, + starting_at BIGINT NOT NULL, + estimated_length BIGINT NOT NULL, + created_at BIGINT NOT NULL, + modified_at BIGINT NOT NULL +); +CREATE TABLE IF NOT EXISTS qualifications ( + id BIGSERIAL PRIMARY KEY NOT NULL, + name TEXT NOT NULL, + description TEXT NOT NULL, + created_at BIGINT NOT NULL, + modified_at BIGINT NOT NULL +); + +CREATE TABLE IF NOT EXISTS attendance ( + id BIGSERIAL PRIMARY KEY NOT NULL, + user_id BIGINT NOT NULL, + mission_id BIGINT NOT NULL, + confirmed_at BIGINT NOT NULL, + attending BOOL NOT NULL, + + CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE SET NULL, + CONSTRAINT fk_mission FOREIGN KEY(mission_id) REFERENCES missions(id) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS roster_groups ( + id BIGSERIAL NOT NULL PRIMARY KEY, + name TEXT NOT NULL, + sort_order BIGINT NOT NULL, + manager_id BIGINT NOT NULL, + + CONSTRAINT fk_manager FOREIGN KEY(manager_id) REFERENCES users(id) ON DELETE SET NULL +); + + +-- NOTE: For table 'users' +ALTER TABLE users ADD FOREIGN KEY (group_id) REFERENCES roster_groups(id) ON DELETE SET NULL; +ALTER TABLE users ADD FOREIGN KEY (rank_id) REFERENCES ranks(id) ON DELETE SET NULL; + +-------------------- +-- NOTE: Records +-------------------- + + +CREATE TABLE IF NOT EXISTS records_ranks ( + id BIGSERIAL PRIMARY KEY NOT NULL, + user_id BIGINT NOT NULL, + rank_id BIGINT NOT NULL, + author_id BIGINT NOT NULL, + created_at BIGINT NOT NULL, + + CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, + CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE SET NULL, + CONSTRAINT fk_rank FOREIGN KEY (rank_id) REFERENCES ranks(id) ON DELETE CASCADE +); +CREATE TABLE IF NOT EXISTS records_awards ( + id BIGSERIAL PRIMARY KEY NOT NULL, + user_id BIGINT NOT NULL, + award_id BIGINT NOT NULL, + author_id BIGINT NOT NULL, + created_at BIGINT NOT NULL, + + CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, + CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE SET NULL, + CONSTRAINT fk_award FOREIGN KEY (award_id) REFERENCES awards(id) ON DELETE CASCADE +); +CREATE TABLE IF NOT EXISTS records_trainings ( + id BIGSERIAL PRIMARY KEY NOT NULL, + user_id BIGINT NOT NULL, + training_id BIGINT NOT NULL, + author_id BIGINT NOT NULL, + created_at BIGINT NOT NULL, + + CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, + CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE SET NULL, + CONSTRAINT fk_training FOREIGN KEY (training_id) REFERENCES trainings(id) ON DELETE CASCADE +); +CREATE TABLE IF NOT EXISTS records_qualifications ( + id BIGSERIAL PRIMARY KEY NOT NULL, + user_id BIGINT NOT NULL, + qualification_id BIGINT NOT NULL, + author_id BIGINT NOT NULL, + created_at BIGINT NOT NULL, + + CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, + CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE SET NULL, + CONSTRAINT fk_qualification FOREIGN KEY (qualification_id) REFERENCES qualifications(id) ON DELETE CASCADE +); +CREATE TABLE IF NOT EXISTS records_missions ( + id BIGSERIAL PRIMARY KEY NOT NULL, + user_id BIGINT NOT NULL, + mission_id BIGINT NOT NULL, + author_id BIGINT NOT NULL, + created_at BIGINT NOT NULL, + + CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, + CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE SET NULL, + CONSTRAINT fk_mission FOREIGN KEY (mission_id) REFERENCES missions(id) ON DELETE CASCADE +); +