Skip to content

Commit

Permalink
[BUG FIX] Handle quasi dupes during migration [MER-2954] (#4604)
Browse files Browse the repository at this point in the history
* adjust migration to be robust against bad data

* Auto format

---------

Co-authored-by: darrensiegel <darrensiegel@users.noreply.github.com>
  • Loading branch information
darrensiegel and darrensiegel authored Jan 26, 2024
1 parent fcce7f8 commit e8a343f
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions priv/repo/migrations/20240125204755_fix_revision_parts.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,52 @@ defmodule Oli.Repo.Migrations.FixRevisionParts do
use Ecto.Migration

def change do
# Extract the unique entries in a new table. This will take records
# like this:
#
# revision_id | part_id | grading_approach
# 1 | 1 | automatic
# 1 | 1 | NULL
# 1 | 1 | NULL
# 1 | 2 | NULL
# 1 | 2 | NULL
# 1 | 2 | NULL
# 1 | 2 | NULL
# 1 | 2 | NULL
# 1 | 2 | NULL
# 1 | 2 | NULL
#
# To:
# revision_id | part_id | grading_approach
# 1 | 1 | automatic
# 1 | 1 | NULL
# 1 | 2 | NULL
execute """
CREATE TABLE temp_revision_parts AS
SELECT DISTINCT ON (revision_id, grading_approach, part_id) *
FROM revision_parts;
"""

# Delete the duplicate entries that have a NULL grading_approach
# This will take records like this and delete the record with the NULL:
#
# revision_id | part_id | grading_approach
# 1 | 1 | automatic
# 1 | 1 | NULL
execute """
DELETE FROM temp_revision_parts
WHERE (revision_id, part_id) IN (
SELECT revision_id, part_id
FROM temp_revision_parts
GROUP BY revision_id, part_id
HAVING COUNT(*) > 1
)
AND grading_approach IS NULL;
"""

# At this point we have a correctly structured copy of revision_parts, so
# we can backup the origin, drop its indices and rename the new table to
# to be revision_parts:
execute """
ALTER TABLE revision_parts RENAME TO backup_revision_parts;
"""
Expand All @@ -30,6 +70,7 @@ defmodule Oli.Repo.Migrations.FixRevisionParts do

flush()

# Finally, add back in the indices on the new revision_parts table:
create unique_index(:revision_parts, [:revision_id, :part_id, :grading_approach])
create index(:revision_parts, [:revision_id])
end
Expand Down

0 comments on commit e8a343f

Please sign in to comment.