-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
adde8f3
commit f85b11e
Showing
5 changed files
with
72 additions
and
1 deletion.
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
7 changes: 7 additions & 0 deletions
7
db/migrate/20220601165646_add_json_fields_for_taggings_to_notices.rb
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,7 @@ | ||
class AddJsonFieldsForTaggingsToNotices < ActiveRecord::Migration[6.1] | ||
def change | ||
add_column :notices, :tags_json, :jsonb | ||
add_column :notices, :jurisdictions_json, :jsonb | ||
add_column :notices, :regulations_json, :jsonb | ||
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
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,57 @@ | ||
#!/bin/sh | ||
|
||
set -e | ||
|
||
beg=0 | ||
inc=1000 | ||
max=20000000 | ||
|
||
while getopts b:i:m: flag | ||
do | ||
case "${flag}" in | ||
b) beg=${OPTARG};; | ||
i) inc=${OPTARG};; | ||
m) max=${OPTARG};; | ||
esac | ||
done | ||
|
||
for i in $(seq $beg $inc $max); do | ||
j=$(expr $i + $inc) | ||
|
||
cat << EOM | ||
PROCESSING $i UNTIL $j ($(expr 100 '*' $i / $max)%) | ||
EOM | ||
|
||
psql -v ON_ERROR_STOP=1 << EOM | ||
SET enable_hashjoin = false; | ||
SET enable_mergejoin = false; | ||
WITH | ||
notice_tags AS ( | ||
SELECT | ||
tg.name AS tag_name, | ||
tgg.context AS context, | ||
tgg.taggable_id | ||
FROM taggings tgg | ||
JOIN tags tg ON tg.id = tgg.tag_id | ||
WHERE tgg.taggable_id >= $i AND tgg.taggable_id < $j | ||
), | ||
f AS ( | ||
SELECT | ||
notice_tags.taggable_id AS notice_id, | ||
jsonb_agg(tag_name) FILTER (WHERE context = 'tags' AND tag_name IS NOT NULL) AS tags, | ||
jsonb_agg(tag_name) FILTER (WHERE context = 'jurisdictions' AND tag_name IS NOT NULL) AS jurisdictions, | ||
jsonb_agg(tag_name) FILTER (WHERE context = 'regulations' AND tag_name IS NOT NULL) AS regulations | ||
FROM notice_tags | ||
GROUP BY notice_tags.taggable_id | ||
) | ||
UPDATE notices n | ||
SET tags_json = COALESCE((SELECT f.tags FROM f WHERE f.notice_id = n.id), '[]'), | ||
jurisdictions_json = COALESCE((SELECT f.jurisdictions FROM f WHERE f.notice_id = n.id), '[]'), | ||
regulations_json = COALESCE((SELECT f.regulations FROM f WHERE f.notice_id = n.id), '[]') | ||
WHERE n.id >= $i AND n.id < $j; | ||
VACUUM notices; | ||
EOM | ||
|
||
done |
File renamed without changes.