|
| 1 | +# Update terms of the three items whose value is nil or not in date or year format |
1 | 2 | class UpdateFixTermItemRelation < ActiveRecord::Migration[7.0] |
2 | 3 | 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]'") |
22 | 13 | end |
23 | 14 |
|
24 | 15 | 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) |
28 | 30 |
|
29 | 31 | execute <<~SQL.squish |
30 | 32 | WITH target AS ( |
31 | | - SELECT it.term_id, it.item_id |
| 33 | + SELECT it.term_id, it.item_id, i.title |
32 | 34 | FROM items i |
33 | 35 | JOIN items_terms it ON i.id = it.item_id |
34 | 36 | 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} |
37 | 39 | ) |
38 | 40 | UPDATE items_terms it |
39 | | - SET term_id = #{after_1999_id} |
| 41 | + SET term_id = #{new_term_id} |
40 | 42 | FROM target |
41 | 43 | WHERE it.item_id = target.item_id |
42 | 44 | AND it.term_id = target.term_id; |
43 | 45 | SQL |
44 | 46 | end |
45 | | - |
46 | 47 | end |
0 commit comments