From b3d7f76dd01788ce60c51182afa7d1f771bd8001 Mon Sep 17 00:00:00 2001 From: Molly Struve Date: Sat, 8 Aug 2020 18:45:01 -0500 Subject: [PATCH] [deploy] Optimization: Change Large Table Primary Keys to Bigints (#9476) --- ...438_large_table_primary_keys_to_bigints.rb | 74 +++++++++++++++++++ db/schema.rb | 26 +++---- release-tasks.sh | 2 +- 3 files changed, 88 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20200806193438_large_table_primary_keys_to_bigints.rb diff --git a/db/migrate/20200806193438_large_table_primary_keys_to_bigints.rb b/db/migrate/20200806193438_large_table_primary_keys_to_bigints.rb new file mode 100644 index 000000000..e575ebba8 --- /dev/null +++ b/db/migrate/20200806193438_large_table_primary_keys_to_bigints.rb @@ -0,0 +1,74 @@ +class LargeTablePrimaryKeysToBigints < ActiveRecord::Migration[6.0] + def up + puts "migrating ahoy_message PKs to bigints" + ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.ahoy_messages") + ActiveRecord::Base.connection.execute( + <<-SQL + ALTER TABLE ahoy_messages + ALTER COLUMN id TYPE bigint, + ALTER COLUMN user_id TYPE bigint, + ALTER COLUMN feedback_message_id TYPE bigint + SQL + ) + + puts "migrating article PKs to bigints" + ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.articles") + ActiveRecord::Base.connection.execute( + <<-SQL + ALTER TABLE articles + ALTER COLUMN id TYPE bigint, + ALTER COLUMN user_id TYPE bigint, + ALTER COLUMN second_user_id TYPE bigint, + ALTER COLUMN third_user_id TYPE bigint, + ALTER COLUMN organization_id TYPE bigint, + ALTER COLUMN collection_id TYPE bigint + SQL + ) + + puts "migrating notification PKs to bigints" + ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.notifications") + ActiveRecord::Base.connection.execute( + <<-SQL + ALTER TABLE notifications + ALTER COLUMN id TYPE bigint, + ALTER COLUMN notifiable_id TYPE bigint, + ALTER COLUMN user_id TYPE bigint + SQL + ) + end + + def down + ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.ahoy_messages") + ActiveRecord::Base.connection.execute( + <<-SQL + ALTER TABLE ahoy_messages + ALTER COLUMN id TYPE int, + ALTER COLUMN user_id TYPE int, + ALTER COLUMN feedback_message_id TYPE int + SQL + ) + + ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.articles") + ActiveRecord::Base.connection.execute( + <<-SQL + ALTER TABLE articles + ALTER COLUMN id TYPE int, + ALTER COLUMN user_id TYPE int, + ALTER COLUMN second_user_id TYPE int, + ALTER COLUMN third_user_id TYPE int, + ALTER COLUMN organization_id TYPE int, + ALTER COLUMN collection_id TYPE int + SQL + ) + + ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.notifications") + ActiveRecord::Base.connection.execute( + <<-SQL + ALTER TABLE notifications + ALTER COLUMN id TYPE int, + ALTER COLUMN notifiable_id TYPE int, + ALTER COLUMN user_id TYPE int + SQL + ) + end +end diff --git a/db/schema.rb b/db/schema.rb index 4a5bd643f..8a6ed6f6e 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: 2020_08_05_050048) do +ActiveRecord::Schema.define(version: 2020_08_06_193438) do # These are extensions that must be enabled in order to support this database enable_extension "citext" @@ -29,17 +29,17 @@ ActiveRecord::Schema.define(version: 2020_08_05_050048) do t.index ["visit_id"], name: "index_ahoy_events_on_visit_id" end - create_table "ahoy_messages", id: :serial, force: :cascade do |t| + create_table "ahoy_messages", force: :cascade do |t| t.datetime "clicked_at" t.text "content" - t.integer "feedback_message_id" + t.bigint "feedback_message_id" t.string "mailer" t.datetime "opened_at" t.datetime "sent_at" t.text "subject" t.text "to" t.string "token" - t.integer "user_id" + t.bigint "user_id" t.string "user_type" t.string "utm_campaign" t.string "utm_content" @@ -76,7 +76,7 @@ ActiveRecord::Schema.define(version: 2020_08_05_050048) do t.index ["user_id"], name: "index_api_secrets_on_user_id" end - create_table "articles", id: :serial, force: :cascade do |t| + create_table "articles", force: :cascade do |t| t.boolean "any_comments_hidden", default: false t.boolean "approved", default: false t.boolean "archived", default: false @@ -89,7 +89,7 @@ ActiveRecord::Schema.define(version: 2020_08_05_050048) do t.string "cached_user_name" t.string "cached_user_username" t.string "canonical_url" - t.integer "collection_id" + t.bigint "collection_id" t.integer "comment_score", default: 0 t.string "comment_template" t.integer "comments_count", default: 0, null: false @@ -115,7 +115,7 @@ ActiveRecord::Schema.define(version: 2020_08_05_050048) do t.integer "organic_page_views_count", default: 0 t.integer "organic_page_views_past_month_count", default: 0 t.integer "organic_page_views_past_week_count", default: 0 - t.integer "organization_id" + t.bigint "organization_id" t.datetime "originally_published_at" t.integer "page_views_count", default: 0 t.string "password" @@ -135,15 +135,15 @@ ActiveRecord::Schema.define(version: 2020_08_05_050048) do t.integer "score", default: 0 t.string "search_optimized_description_replacement" t.string "search_optimized_title_preamble" - t.integer "second_user_id" + t.bigint "second_user_id" t.boolean "show_comments", default: true t.text "slug" t.string "social_image" t.integer "spaminess_rating", default: 0 - t.integer "third_user_id" + t.bigint "third_user_id" t.string "title" t.datetime "updated_at", null: false - t.integer "user_id" + t.bigint "user_id" t.integer "user_subscriptions_count", default: 0, null: false t.string "video" t.string "video_closed_caption_track_url" @@ -678,17 +678,17 @@ ActiveRecord::Schema.define(version: 2020_08_05_050048) do t.index ["user_id", "notifiable_type", "notifiable_id"], name: "idx_notification_subs_on_user_id_notifiable_type_notifiable_id", unique: true end - create_table "notifications", id: :serial, force: :cascade do |t| + create_table "notifications", force: :cascade do |t| t.string "action" t.datetime "created_at", null: false t.jsonb "json_data" - t.integer "notifiable_id" + t.bigint "notifiable_id" t.string "notifiable_type" t.datetime "notified_at" t.bigint "organization_id" t.boolean "read", default: false t.datetime "updated_at", null: false - t.integer "user_id" + t.bigint "user_id" t.index ["created_at"], name: "index_notifications_on_created_at" t.index ["json_data"], name: "index_notifications_on_json_data", using: :gin t.index ["notifiable_id", "notifiable_type", "action"], name: "index_notifications_on_notifiable_id_notifiable_type_and_action" diff --git a/release-tasks.sh b/release-tasks.sh index f85c91fe6..f76338ad4 100755 --- a/release-tasks.sh +++ b/release-tasks.sh @@ -16,6 +16,6 @@ set -Eex # runs migration for Postgres, setups/updates Elasticsearch # and boots the app to check there are no errors -STATEMENT_TIMEOUT=180000 bundle exec rails app_initializer:setup +STATEMENT_TIMEOUT=4500000 bundle exec rails app_initializer:setup bundle exec rake fastly:update_configs bundle exec rails runner "puts 'app load success'"