Index into elastic during db migration

This commit is contained in:
Brenda
2019-12-26 18:12:15 +13:00
parent fca21f85c0
commit a7e915621e
3 changed files with 17 additions and 71 deletions

View File

@@ -1,66 +0,0 @@
# frozen_string_literal: true
class AddHarvestCountToCrop < ActiveRecord::Migration[5.2]
def change
change_table :crops do |t|
t.integer :harvests_count, default: 0
t.integer :photo_associations_count, default: 0
end
change_table :plant_parts do |t|
t.integer :harvests_count, default: 0
end
change_table :posts do |t|
t.integer :comments_count, default: 0
end
reversible do |dir|
dir.up { data }
end
end
def data
execute <<-SQL.squish
UPDATE crops
SET harvests_count = (
SELECT count(1)
FROM harvests
WHERE harvests.crop_id = crops.id
)
SQL
execute <<-SQL.squish
UPDATE crops
SET photo_associations_count = (
SELECT count(1)
FROM photo_associations
WHERE photo_associations.crop_id = crops.id
)
SQL
execute <<-SQL.squish
UPDATE plant_parts
SET harvests_count = (
SELECT count(1)
FROM harvests
WHERE harvests.plant_part_id = plant_parts.id
)
SQL
execute <<-SQL.squish
UPDATE posts
SET comments_count = (
SELECT count(1)
FROM comments
WHERE comments.post_id = posts.id
)
SQL
say 'indexing crops'
Crop.reindex
say 'indexing plantings'
Planting.reindex
say 'indexing seeds'
Seed.reindex
say 'indexing harvests'
Harvest.reindex
say 'indexing photos'
Photo.reindex
end
end

View File

@@ -0,0 +1,16 @@
class ElasticIndexing < ActiveRecord::Migration[5.2]
def up
say 'indexing crops'
Crop.reindex
say 'indexing plantings'
Planting.reindex
say 'indexing seeds'
Seed.reindex
say 'indexing harvests'
Harvest.reindex
say 'indexing photos'
Photo.reindex
end
def down
end
end

View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_12_09_202348) do
ActiveRecord::Schema.define(version: 2019_12_26_051019) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -196,8 +196,6 @@ ActiveRecord::Schema.define(version: 2019_12_09_202348) do
t.integer "median_days_to_first_harvest"
t.integer "median_days_to_last_harvest"
t.jsonb "openfarm_data"
t.integer "harvests_count", default: 0
t.integer "photo_associations_count", default: 0
t.index ["name"], name: "index_crops_on_name"
t.index ["requester_id"], name: "index_crops_on_requester_id"
t.index ["slug"], name: "index_crops_on_slug", unique: true
@@ -460,7 +458,6 @@ ActiveRecord::Schema.define(version: 2019_12_09_202348) do
t.datetime "created_at"
t.datetime "updated_at"
t.string "slug"
t.integer "harvests_count", default: 0
end
create_table "plantings", id: :serial, force: :cascade do |t|
@@ -494,7 +491,6 @@ ActiveRecord::Schema.define(version: 2019_12_09_202348) do
t.string "slug"
t.integer "forum_id"
t.integer "likes_count", default: 0
t.integer "comments_count", default: 0
t.index ["created_at", "author_id"], name: "index_posts_on_created_at_and_author_id"
t.index ["slug"], name: "index_posts_on_slug", unique: true
end