-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #523 from performant-software/bugfix/dupe-uids
Deduplicate highlights with identical UIDs and prevent future UID collisions
- Loading branch information
Showing
12 changed files
with
58 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
release: rails db:migrate | ||
release: rails db:migrate:with_data | ||
web: bundle exec puma -C config/puma.rb | ||
worker: bundle exec sidekiq -e production -C config/sidekiq.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9569,6 +9569,11 @@ [email protected]: | |
version "1.0.1" | ||
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" | ||
|
||
uuid@^10.0.0: | ||
version "10.0.0" | ||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-10.0.0.tgz#5a95aa454e6e002725c79055fd42aaba30ca6294" | ||
integrity sha512-8XkAphELsDnEGrDxUOHB3RGvXz6TeuYSGEZBOjtTtPm2lwhGBjLgOzLHB63IUWfBpNucQjND6d3AOudO+H3RWQ== | ||
|
||
uuid@^3.0.1, uuid@^3.3.2: | ||
version "3.3.3" | ||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.3.tgz#4568f0216e78760ee1dbf3a4d2cf53e224112866" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
class DeduplicateHighlights < ActiveRecord::Migration[6.1] | ||
# one-time data migration to deduplicate highlights with identical UIDs | ||
def up | ||
# NOTE: Document.highlight_map would always return one record per UID, even when there were | ||
# multiple records with identical UIDs. As such, since Document.highlight_map was relying on | ||
# default database ordering with no ORDER BY, it is impossible to replicate the correct order | ||
# with ORDER BY; we can only use the ordering from the same SQL statement executed previosuly | ||
# by highlight_map. Once we retrieve the correct highlight per each UID we can delete all | ||
# others sharing its UID. | ||
Document.pluck(:id).each do |doc_id| | ||
# get the correct highlight database ID per each UID; MUST be in order returned by this query | ||
query = <<-SQL | ||
SELECT "highlights".* FROM "highlights" WHERE "highlights"."document_id" = #{doc_id}; | ||
SQL | ||
doc_highlights = Highlight.find_by_sql(query) | ||
correct_highlights = doc_highlights.to_h { |hl| [hl[:uid], hl[:id]] } | ||
# find all highlights with matching UIDs but non-matching database IDs (i.e. unused duplicates) | ||
to_delete_ids= doc_highlights.select { | ||
|hl| correct_highlights[hl[:uid]] != hl[:id] | ||
}.pluck(:id) | ||
# destroy unused duplicate records | ||
Highlight.where(:id => to_delete_ids).destroy_all | ||
end | ||
end | ||
|
||
def down | ||
raise ActiveRecord::IrreversibleMigration | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DataMigrate::Data.define(version: 20241002192349) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters