diff options
| author | mo khan <mo@mokhan.ca> | 2016-06-24 17:24:50 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2016-06-24 17:24:50 -0600 |
| commit | 6a1bc5070813d80c1716ebee4b150f70ed683b78 (patch) | |
| tree | 6cea0ee5dccdce98387e294e086f88c93a15234b /db | |
| parent | 5514ec6bafa5417e500c203cc22fc5689550ead9 (diff) | |
refactor model.
Diffstat (limited to 'db')
9 files changed, 95 insertions, 37 deletions
diff --git a/db/migrate/20160611165543_migrate_to_exercise_sets.rb b/db/migrate/20160611165543_migrate_to_exercise_sets.rb index 18be288..467377e 100644 --- a/db/migrate/20160611165543_migrate_to_exercise_sets.rb +++ b/db/migrate/20160611165543_migrate_to_exercise_sets.rb @@ -1,13 +1,17 @@ class MigrateToExerciseSets < ActiveRecord::Migration def up - ExerciseSession.find_each do |exercise_session| - target_reps = exercise_session.exercise_workout.repetitions - exercise_session.actual_sets.each do |n| - say "Creating set for: #{exercise_session.name}: set: #{n}" - exercise_session.exercise_sets.create!( + execute("SELECT * FROM exercise_sessions").each do |exercise_session| + actual_sets = exercise_session["actual_sets"].gsub(/{/, '').gsub(/}/, '').split(',').map(&:to_i) + actual_sets.each do |n| + say "Creating set for: #{exercise_session["name"]}: set: #{n}" + + workout_id = exercise_session["exercise_workout_id"] + target_reps = execute("SELECT repetitions FROM exercise_workouts where id = '#{workout_id}'").first["repetitions"].to_i + ExerciseSet.create!( + exercise_session_id: exercise_session["id"], actual_repetitions: n, target_repetitions: target_reps, - target_weight: exercise_session.target_weight, + target_weight: exercise_session["target_weight"], ) end end diff --git a/db/migrate/20160623202817_migrate_exercise_id_to_exercise_sets.rb b/db/migrate/20160623202817_migrate_exercise_id_to_exercise_sets.rb index 7d6a56e..e97f87b 100644 --- a/db/migrate/20160623202817_migrate_exercise_id_to_exercise_sets.rb +++ b/db/migrate/20160623202817_migrate_exercise_id_to_exercise_sets.rb @@ -1,8 +1,17 @@ class MigrateExerciseIdToExerciseSets < ActiveRecord::Migration def change ExerciseSet.where(exercise_id: nil).find_each do |set| - set.exercise = set.exercise_session.exercise - set.save! + result = execute( + <<-SQL +SELECT ew.exercise_id +FROM exercise_sessions es +INNER JOIN exercise_workouts ew on ew.id = es.exercise_workout_id +WHERE es.id = '#{set.exercise_session_id}' + SQL + ) + exercise_id = result.first["exercise_id"] + say "updating set: #{set.id} to exercise: #{exercise_id}" + set.update_column(:exercise_id, exercise_id) end end end diff --git a/db/migrate/20160623205309_migrate_training_session_id_to_exercise_sets.rb b/db/migrate/20160623205309_migrate_training_session_id_to_exercise_sets.rb index 928049b..aade4fe 100644 --- a/db/migrate/20160623205309_migrate_training_session_id_to_exercise_sets.rb +++ b/db/migrate/20160623205309_migrate_training_session_id_to_exercise_sets.rb @@ -1,8 +1,16 @@ class MigrateTrainingSessionIdToExerciseSets < ActiveRecord::Migration def change ExerciseSet.where(training_session_id: nil).find_each do |set| - set.training_session_id = set.exercise_session.training_session.id - set.save! + result = execute( + <<-SQL +SELECT training_session_id +FROM exercise_sessions +WHERE id = '#{set.exercise_session_id}' + SQL + ) + training_session_id = result.first["training_session_id"] + say "updating set: #{set.id} to training_session: #{training_session_id}" + set.update_column(:training_session_id, training_session_id) end end end diff --git a/db/migrate/20160624192245_rename_workouts_to_routines.rb b/db/migrate/20160624192245_rename_workouts_to_routines.rb new file mode 100644 index 0000000..9394696 --- /dev/null +++ b/db/migrate/20160624192245_rename_workouts_to_routines.rb @@ -0,0 +1,7 @@ +class RenameWorkoutsToRoutines < ActiveRecord::Migration + def change + rename_table :workouts, :routines + rename_column :exercise_workouts, :workout_id, :routine_id + rename_column :training_sessions, :workout_id, :routine_id + end +end diff --git a/db/migrate/20160624195736_rename_training_session_to_workouts.rb b/db/migrate/20160624195736_rename_training_session_to_workouts.rb new file mode 100644 index 0000000..0c837c7 --- /dev/null +++ b/db/migrate/20160624195736_rename_training_session_to_workouts.rb @@ -0,0 +1,6 @@ +class RenameTrainingSessionToWorkouts < ActiveRecord::Migration + def change + rename_table :training_sessions, :workouts + rename_column :exercise_sets, :training_session_id, :workout_id + end +end diff --git a/db/migrate/20160624205748_rename_exercise_workouts_to_recommendations.rb b/db/migrate/20160624205748_rename_exercise_workouts_to_recommendations.rb new file mode 100644 index 0000000..bbebb7b --- /dev/null +++ b/db/migrate/20160624205748_rename_exercise_workouts_to_recommendations.rb @@ -0,0 +1,5 @@ +class RenameExerciseWorkoutsToRecommendations < ActiveRecord::Migration + def change + rename_table :exercise_workouts, :recommendations + end +end diff --git a/db/migrate/20160624210652_add_missing_indexes.rb b/db/migrate/20160624210652_add_missing_indexes.rb new file mode 100644 index 0000000..56ab337 --- /dev/null +++ b/db/migrate/20160624210652_add_missing_indexes.rb @@ -0,0 +1,10 @@ +class AddMissingIndexes < ActiveRecord::Migration + def change + add_index :exercise_sets, :exercise_id + add_index :exercise_sets, [:exercise_id, :workout_id] + add_index :routines, :program_id + add_index :workouts, :routine_id + add_index :users, :username + add_index :users, [:username, :email] + end +end diff --git a/db/schema.rb b/db/schema.rb index e2fdc40..6f42507 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,30 +11,24 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160624034644) do +ActiveRecord::Schema.define(version: 20160624210652) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" enable_extension "uuid-ossp" create_table "exercise_sets", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t| - t.integer "target_repetitions", null: false + t.integer "target_repetitions", null: false t.integer "actual_repetitions" - t.float "target_weight", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.uuid "exercise_id", null: false - t.uuid "training_session_id" + t.float "target_weight", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.uuid "exercise_id", null: false + t.uuid "workout_id" end - create_table "exercise_workouts", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t| - t.uuid "exercise_id", null: false - t.uuid "workout_id", null: false - t.integer "sets", null: false - t.integer "repetitions", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end + add_index "exercise_sets", ["exercise_id", "workout_id"], name: "index_exercise_sets_on_exercise_id_and_workout_id", using: :btree + add_index "exercise_sets", ["exercise_id"], name: "index_exercise_sets_on_exercise_id", using: :btree create_table "exercises", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t| t.string "name", null: false @@ -101,16 +95,23 @@ ActiveRecord::Schema.define(version: 20160624034644) do add_index "received_emails", ["user_id"], name: "index_received_emails_on_user_id", using: :btree - create_table "training_sessions", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t| - t.uuid "user_id", null: false + create_table "recommendations", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t| + t.uuid "exercise_id", null: false + t.uuid "routine_id", null: false + t.integer "sets", null: false + t.integer "repetitions", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.uuid "workout_id", null: false - t.datetime "occurred_at", null: false - t.float "body_weight" end - add_index "training_sessions", ["user_id"], name: "index_training_sessions_on_user_id", using: :btree + create_table "routines", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t| + t.uuid "program_id", null: false + t.string "name", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "routines", ["program_id"], name: "index_routines_on_program_id", using: :btree create_table "user_sessions", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t| t.uuid "user_id", null: false @@ -132,13 +133,21 @@ ActiveRecord::Schema.define(version: 20160624034644) do t.datetime "updated_at", null: false end + add_index "users", ["username", "email"], name: "index_users_on_username_and_email", using: :btree + add_index "users", ["username"], name: "index_users_on_username", using: :btree + create_table "workouts", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t| - t.uuid "program_id", null: false - t.string "name", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.uuid "user_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.uuid "routine_id", null: false + t.datetime "occurred_at", null: false + t.float "body_weight" end + add_index "workouts", ["routine_id"], name: "index_workouts_on_routine_id", using: :btree + add_index "workouts", ["user_id"], name: "index_workouts_on_user_id", using: :btree + add_foreign_key "profiles", "gyms" add_foreign_key "received_emails", "users" add_foreign_key "user_sessions", "users" diff --git a/db/seeds.rb b/db/seeds.rb index 83669b5..271ed22 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -9,13 +9,13 @@ deadlift = Exercise.find_or_create_by!(name: "Deadlift") dips = Exercise.find_or_create_by!(name: "Dips") chin_ups = Exercise.find_or_create_by!(name: "Chinups") -workout_a = program.workouts.find_or_create_by(name: "A") +workout_a = program.routines.find_or_create_by(name: "A") workout_a.add_exercise(squat, sets: 5, repetitions: 5) workout_a.add_exercise(bench_press, sets: 5, repetitions: 5) workout_a.add_exercise(barbell_row, sets: 5, repetitions: 5) workout_a.add_exercise(dips, sets: 3, repetitions: 5) -workout_b = program.workouts.find_or_create_by(name: "B") +workout_b = program.routines.find_or_create_by(name: "B") workout_b.add_exercise(squat, sets: 5, repetitions: 5) workout_b.add_exercise(overhead_press, sets: 5, repetitions: 5) workout_b.add_exercise(deadlift, sets: 1, repetitions: 5) |
