Skip to content

Commit

Permalink
Merge pull request #347 from blackcandy-org/clean-duplicate
Browse files Browse the repository at this point in the history
Prevent syncing duplicate songs with same md5_hash
  • Loading branch information
aidewoode committed Feb 6, 2024
2 parents fd67668 + 6cc0eae commit fb90381
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/models/song.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Song < ApplicationRecord
include SortableConcern

validates :name, :file_path, :file_path_hash, :md5_hash, presence: true
validates :md5_hash, uniqueness: true

belongs_to :album, touch: true
belongs_to :artist, touch: true
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20240206051609_add_unique_index_on_song_md5_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class AddUniqueIndexOnSongMd5Hash < ActiveRecord::Migration[7.1]
def up
# Remove duplicate songs
Song.where.not(id: Song.group(:md5_hash).select("min(id)")).destroy_all

remove_index :songs, :md5_hash
add_index :songs, :md5_hash, unique: true
end

def down
remove_index :songs, :md5_hash
add_index :songs, :md5_hash
end
end
4 changes: 2 additions & 2 deletions 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.1].define(version: 2024_02_02_024633) do
ActiveRecord::Schema[7.1].define(version: 2024_02_06_051609) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
Expand Down Expand Up @@ -211,7 +211,7 @@
t.index ["album_id"], name: "index_songs_on_album_id"
t.index ["artist_id"], name: "index_songs_on_artist_id"
t.index ["file_path_hash"], name: "index_songs_on_file_path_hash"
t.index ["md5_hash"], name: "index_songs_on_md5_hash"
t.index ["md5_hash"], name: "index_songs_on_md5_hash", unique: true
t.index ["name"], name: "index_songs_on_name"
end

Expand Down
6 changes: 3 additions & 3 deletions test/models/album_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class AlbumTest < ActiveSupport::TestCase

album.songs.create!(
[
{name: "test_song_1", file_path: "fake_path", file_path_hash: "fake_path_hash", md5_hash: "fake_md5", tracknum: 2, discnum: 2, artist: artist},
{name: "test_song_2", file_path: "fake_path", file_path_hash: "fake_path_hash", md5_hash: "fake_md5", tracknum: 3, discnum: 1, artist: artist},
{name: "test_song_3", file_path: "fake_path", file_path_hash: "fake_path_hash", md5_hash: "fake_md5", tracknum: 1, discnum: 1, artist: artist}
{name: "test_song_1", file_path: "fake_path", file_path_hash: "fake_path_hash", md5_hash: "fake_md5_0", tracknum: 2, discnum: 2, artist: artist},
{name: "test_song_2", file_path: "fake_path", file_path_hash: "fake_path_hash", md5_hash: "fake_md5_1", tracknum: 3, discnum: 1, artist: artist},
{name: "test_song_3", file_path: "fake_path", file_path_hash: "fake_path_hash", md5_hash: "fake_md5_2", tracknum: 1, discnum: 1, artist: artist}
]
)

Expand Down
14 changes: 14 additions & 0 deletions test/models/song_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,18 @@ class SongTest < ActiveSupport::TestCase
test "should use default sort when use invalid sort value" do
assert_equal songs(:flac_sample), Song.sort_records(:invalid).first
end

test "should get unique error when create song with same md5_hash" do
song = Song.new(
name: "song_test",
file_path: Rails.root.join("test/fixtures/files/artist1_album2.mp3"),
file_path_hash: "fake_path_hash",
md5_hash: songs(:flac_sample).md5_hash,
artist_id: artists(:artist1).id,
album_id: albums(:album1).id
)

assert_not song.valid?
assert_equal ["has already been taken"], song.errors[:md5_hash]
end
end
2 changes: 1 addition & 1 deletion test/system/songs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SongsSystemTest < ApplicationSystemTestCase
name: "song_test_#{n}",
file_path: Rails.root.join("test/fixtures/files/artist1_album2.mp3"),
file_path_hash: "fake_path_hash",
md5_hash: "fake_md5",
md5_hash: "fake_md5_#{n}",
artist_id: artists(:artist1).id,
album_id: albums(:album1).id
)
Expand Down

0 comments on commit fb90381

Please sign in to comment.