Skip to content

Commit 926632d

Browse files
committed
Fix the special items
1 parent 72d96d1 commit 926632d

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed
Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,47 @@
1+
# Update terms of the three items whose value is nil or not in date or year format
12
class UpdateFixTermItemRelation < ActiveRecord::Migration[7.0]
23
def up
3-
after_1999_id = select_value("SELECT id FROM terms WHERE value = 'After 1999'")
4-
after_2020_id = select_value("SELECT id FROM terms WHERE value = 'After 2020'")
5-
return unless after_1999_id && after_2020_id
6-
7-
execute <<~SQL.squish
8-
WITH target AS (
9-
SELECT it.term_id, it.item_id
10-
FROM items i
11-
JOIN items_terms it ON i.id = it.item_id
12-
JOIN terms t ON it.term_id = t.id
13-
WHERE t.id = #{after_1999_id}
14-
AND i.date = '[2020]'
15-
)
16-
UPDATE items_terms it
17-
SET term_id = #{after_2020_id}
18-
FROM target
19-
WHERE it.item_id = target.item_id
20-
AND it.term_id = target.term_id;
21-
SQL
4+
facet = Facet.find_by!(name: 'Decade')
5+
t_after_1999 = Term.find_by!(facet: facet, value: 'After 1999')
6+
t2000_2009 = Term.find_by!(facet: facet, value: '2000-2009')
7+
t2020_plus = Term.find_by!(facet: facet, value: 'After 2020')
8+
9+
# Items with nil date
10+
insert_and_delete(t_after_1999.id, t2000_2009.id, "i.date is null AND i.title in ('Loading a Cannon', 'Dense Populations')")
11+
# Items with date = '[2020]'
12+
insert_and_delete(t_after_1999.id, t2020_plus.id, "date = '[2020]'")
2213
end
2314

2415
def down
25-
after_1999_id = select_value("SELECT id FROM terms WHERE value = 'After 1999'")
26-
after_2020_id = select_value("SELECT id FROM terms WHERE value = 'After 2020'")
27-
return unless after_1999_id && after_2020_id
16+
facet = Facet.find_by!(name: 'Decade')
17+
t_after_1999 = Term.find_by!(facet: facet, value: 'After 1999')
18+
t2000_2009 = Term.find_by!(facet: facet, value: '2000-2009')
19+
t2020_plus = Term.find_by!(facet: facet, value: 'After 2020')
20+
21+
# revert items with nil date
22+
insert_and_delete(t2000_2009.id,t_after_1999.id, "i.date is null AND i.title in ('Loading a Cannon', 'Dense Populations')")
23+
# revert items with date = '[2020]'
24+
insert_and_delete(t2020_plus.id, t_after_1999.id, "date = '[2020]'")
25+
end
26+
27+
private
28+
29+
def insert_and_delete(old_term_id, new_term_id, condition_sql)
2830

2931
execute <<~SQL.squish
3032
WITH target AS (
31-
SELECT it.term_id, it.item_id
33+
SELECT it.term_id, it.item_id, i.title
3234
FROM items i
3335
JOIN items_terms it ON i.id = it.item_id
3436
JOIN terms t ON it.term_id = t.id
35-
WHERE t.id = #{after_2020_id}
36-
AND i.date = '[2020]'
37+
WHERE t.id = #{old_term_id}
38+
AND #{condition_sql}
3739
)
3840
UPDATE items_terms it
39-
SET term_id = #{after_1999_id}
41+
SET term_id = #{new_term_id}
4042
FROM target
4143
WHERE it.item_id = target.item_id
4244
AND it.term_id = target.term_id;
4345
SQL
4446
end
45-
4647
end

0 commit comments

Comments
 (0)