Skip to content

Commit

Permalink
Remove Duplicate episodes and enforce the rule (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
brand-it authored Dec 10, 2024
1 parent f18a358 commit e711d65
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/models/episode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#
# Indexes
#
# index_episodes_on_season_id (season_id)
# index_episodes_on_season_id (season_id)
# index_episodes_on_the_movie_db_id (the_movie_db_id) UNIQUE
#
class Episode < ApplicationRecord
include ActionView::Helpers::DateHelper
Expand All @@ -30,6 +31,7 @@ class Episode < ApplicationRecord
has_many :uploaded_video_blobs, -> { uploaded }, class_name: 'VideoBlob', dependent: false, inverse_of: :episode

validates :episode_number, presence: true
validates :the_movie_db_id, uniqueness: true

scope :order_by_episode_number, -> { order(:episode_number) }

Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20241210214438_remove_dups_from_episodes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class RemoveDupsFromEpisodes < ActiveRecord::Migration[7.2]
def change
Episode.find_each do |episode|
episodes = Episode.where(the_movie_db_id: episode.the_movie_db_id)
if episodes.size > 1
episodes.each.with_index do |dup, index|
next if index.zero?
dup.destroy
end
end
end
add_index :episodes, [:the_movie_db_id], if_not_exists: true, unique: true
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2024_11_23_005122) do
ActiveRecord::Schema[7.2].define(version: 2024_12_10_214438) do
create_table "configs", force: :cascade do |t|
t.string "type", default: "Config", null: false
t.text "settings"
Expand Down Expand Up @@ -70,6 +70,7 @@
t.bigint "season_id"
t.integer "runtime"
t.index ["season_id"], name: "index_episodes_on_season_id"
t.index ["the_movie_db_id"], name: "index_episodes_on_the_movie_db_id", unique: true
end

create_table "jobs", force: :cascade do |t|
Expand Down
3 changes: 2 additions & 1 deletion spec/factories/episodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#
# Indexes
#
# index_episodes_on_season_id (season_id)
# index_episodes_on_season_id (season_id)
# index_episodes_on_the_movie_db_id (the_movie_db_id) UNIQUE
#
FactoryBot.define do
factory :episode do
Expand Down
3 changes: 2 additions & 1 deletion spec/models/episode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#
# Indexes
#
# index_episodes_on_season_id (season_id)
# index_episodes_on_season_id (season_id)
# index_episodes_on_the_movie_db_id (the_movie_db_id) UNIQUE
#
require 'rails_helper'

Expand Down

0 comments on commit e711d65

Please sign in to comment.