From a7e915621e69304aa98b438cbbedaa2d85c70ef7 Mon Sep 17 00:00:00 2001 From: Brenda Date: Thu, 26 Dec 2019 18:12:15 +1300 Subject: [PATCH] Index into elastic during db migration --- ...0191209202348_add_harvest_count_to_crop.rb | 66 ------------------- db/migrate/20191226051019_elastic_indexing.rb | 16 +++++ db/schema.rb | 6 +- 3 files changed, 17 insertions(+), 71 deletions(-) delete mode 100644 db/migrate/20191209202348_add_harvest_count_to_crop.rb create mode 100644 db/migrate/20191226051019_elastic_indexing.rb diff --git a/db/migrate/20191209202348_add_harvest_count_to_crop.rb b/db/migrate/20191209202348_add_harvest_count_to_crop.rb deleted file mode 100644 index 46b5f0737..000000000 --- a/db/migrate/20191209202348_add_harvest_count_to_crop.rb +++ /dev/null @@ -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 diff --git a/db/migrate/20191226051019_elastic_indexing.rb b/db/migrate/20191226051019_elastic_indexing.rb new file mode 100644 index 000000000..b1f357433 --- /dev/null +++ b/db/migrate/20191226051019_elastic_indexing.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index a701305f5..2b9dceff2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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