Skip to content

Commit

Permalink
Redesign initial table design to be more idiomatic
Browse files Browse the repository at this point in the history
- Use generated identity columns rather than the serial data type.
- Add a sequence order column for each set to keep track of relative
  order and combine that with the foreign key id to represent the
  primary key.
- Add a missing date column for each log entry.
- Mark the combination of user id and exercise name as unique.
  • Loading branch information
arnavb committed Jul 6, 2024
1 parent 11cc7d8 commit 5900204
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions Migrations/Scripts/postgres_migration.sql
Original file line number Diff line number Diff line change
@@ -1,46 +1,55 @@
CREATE TABLE bot_user
(
id SERIAL PRIMARY KEY,
id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
discord_id VARCHAR(64) UNIQUE NOT NULL
);

CREATE TYPE exercise_type as ENUM ('set', 'speed', 'time');

-- Users have exercises, but not multiple of the same
CREATE TABLE exercise
(
id SERIAL PRIMARY KEY,
id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
name VARCHAR(100) NOT NULL,
type exercise_type NOT NULL,
bot_user_id INT NOT NULL REFERENCES bot_user (id)
bot_user_id INT NOT NULL REFERENCES bot_user (id),

UNIQUE (bot_user_id, name)
);

-- Exercises are logged on a particular day, referencing a particular exercise
CREATE TABLE
exercise_log
(
id SERIAL PRIMARY KEY,
exercise_id INT NOT NULL REFERENCES exercise (id)
id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
exercise_id INT NOT NULL REFERENCES exercise (id),
log_date DATE NOT NULL DEFAULT CURRENT_DATE
);

-- Represents all sets for a particular log, e.g 1 warmup set + 3x8 regular sets
CREATE TABLE
all_set_detail
(
id INT PRIMARY KEY REFERENCES exercise_log (id)
id INTEGER PRIMARY KEY REFERENCES exercise_log (id)
);

-- Represents a single exercise set
CREATE TABLE
set_exercise_detail
(
id SERIAL PRIMARY KEY,
id INTEGER NOT NULL REFERENCES all_set_detail (id),
reps INT NOT NULL,
weight INT NOT NULL,
is_warmup BOOLEAN NOT NULL,
all_set_detail_id INT NOT NULL REFERENCES all_set_detail (id)
sequence_order INT NOT NULL,

PRIMARY KEY (id, sequence_order)
);

CREATE TABLE
speed_exercise_detail
(
id INT PRIMARY KEY REFERENCES exercise_log (id),
id INTEGER PRIMARY KEY REFERENCES exercise_log (id),
duration_sec INT NOT NULL,
distance_miles REAL NOT NULL,
exercise_log_id INT NOT NULL
Expand All @@ -49,7 +58,7 @@ CREATE TABLE
CREATE TABLE
time_exercise_details
(
id INT PRIMARY KEY REFERENCES exercise_log (id),
id INTEGER PRIMARY KEY REFERENCES exercise_log (id),
duration_sec INT NOT NULL,
exercise_log_id INT NOT NULL
);

0 comments on commit 5900204

Please sign in to comment.