[deploy] Optimization:Migrate Final Small Table PKs to Bigints (#9706)

This commit is contained in:
Molly Struve 2020-08-11 15:51:54 -05:00 committed by GitHub
parent 93e442a3e9
commit b9d7202ec0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 110 additions and 13 deletions

View file

@ -0,0 +1,97 @@
class FinalBigintMigration < ActiveRecord::Migration[6.0]
def up
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.api_secrets")
safety_assured { change_column :api_secrets, :user_id, :bigint }
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.badge_achievements")
safety_assured { change_column :badge_achievements, :rewarder_id, :bigint }
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.broadcasts")
safety_assured { change_column :broadcasts, :broadcastable_id, :bigint }
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.display_ads")
safety_assured { change_column :display_ads, :organization_id, :bigint }
puts "migrating feedback_messages PKs to bigints"
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.feedback_messages")
ActiveRecord::Base.connection.execute(
<<-SQL
ALTER TABLE feedback_messages
ALTER COLUMN affected_id TYPE bigint,
ALTER COLUMN offender_id TYPE bigint,
ALTER COLUMN reporter_id TYPE bigint
SQL
)
puts "migrating html_variant_successes PKs to bigints"
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.html_variant_successes")
ActiveRecord::Base.connection.execute(
<<-SQL
ALTER TABLE html_variant_successes
ALTER COLUMN article_id TYPE bigint,
ALTER COLUMN html_variant_id TYPE bigint
SQL
)
puts "migrating html_variant_trials PKs to bigints"
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.html_variant_trials")
ActiveRecord::Base.connection.execute(
<<-SQL
ALTER TABLE html_variant_trials
ALTER COLUMN article_id TYPE bigint,
ALTER COLUMN html_variant_id TYPE bigint
SQL
)
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.users_roles")
safety_assured { change_column :users_roles, :user_id, :bigint }
end
def down
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.api_secrets")
safety_assured { change_column :api_secrets, :user_id, :int }
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.badge_achievements")
safety_assured { change_column :badge_achievements, :rewarder_id, :int }
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.broadcasts")
safety_assured { change_column :broadcasts, :broadcastable_id, :int }
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.display_ads")
safety_assured { change_column :display_ads, :organization_id, :int }
puts "migrating feedback_messages PKs to ints"
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.feedback_messages")
ActiveRecord::Base.connection.execute(
<<-SQL
ALTER TABLE feedback_messages
ALTER COLUMN affected_id TYPE int,
ALTER COLUMN offender_id TYPE int,
ALTER COLUMN reporter_id TYPE int
SQL
)
puts "migrating html_variant_successes PKs to ints"
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.html_variant_successes")
ActiveRecord::Base.connection.execute(
<<-SQL
ALTER TABLE html_variant_successes
ALTER COLUMN article_id TYPE int,
ALTER COLUMN html_variant_id TYPE int
SQL
)
puts "migrating html_variant_trials PKs to ints"
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.html_variant_trials")
ActiveRecord::Base.connection.execute(
<<-SQL
ALTER TABLE html_variant_trials
ALTER COLUMN article_id TYPE int,
ALTER COLUMN html_variant_id TYPE int
SQL
)
ActiveRecord::Base.connection.execute("DROP VIEW IF EXISTS hypershield.users_roles")
safety_assured { change_column :users_roles, :user_id, :int }
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: 2020_08_06_193438) do
ActiveRecord::Schema.define(version: 2020_08_09_200631) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
@ -71,7 +71,7 @@ ActiveRecord::Schema.define(version: 2020_08_06_193438) do
t.string "description", null: false
t.string "secret"
t.datetime "updated_at", null: false
t.integer "user_id"
t.bigint "user_id"
t.index ["secret"], name: "index_api_secrets_on_secret", unique: true
t.index ["user_id"], name: "index_api_secrets_on_user_id"
end
@ -189,7 +189,7 @@ ActiveRecord::Schema.define(version: 2020_08_06_193438) do
create_table "badge_achievements", force: :cascade do |t|
t.bigint "badge_id", null: false
t.datetime "created_at", null: false
t.integer "rewarder_id"
t.bigint "rewarder_id"
t.text "rewarding_context_message"
t.text "rewarding_context_message_markdown"
t.datetime "updated_at", null: false
@ -280,7 +280,7 @@ ActiveRecord::Schema.define(version: 2020_08_06_193438) do
t.datetime "active_status_updated_at"
t.string "banner_style"
t.text "body_markdown"
t.integer "broadcastable_id"
t.bigint "broadcastable_id"
t.string "broadcastable_type"
t.datetime "created_at"
t.text "processed_html"
@ -457,7 +457,7 @@ ActiveRecord::Schema.define(version: 2020_08_06_193438) do
t.integer "clicks_count", default: 0
t.datetime "created_at", null: false
t.integer "impressions_count", default: 0
t.integer "organization_id"
t.bigint "organization_id"
t.string "placement_area"
t.text "processed_html"
t.boolean "published", default: false
@ -496,14 +496,14 @@ ActiveRecord::Schema.define(version: 2020_08_06_193438) do
end
create_table "feedback_messages", force: :cascade do |t|
t.integer "affected_id"
t.bigint "affected_id"
t.string "category"
t.datetime "created_at"
t.string "feedback_type"
t.text "message"
t.integer "offender_id"
t.bigint "offender_id"
t.string "reported_url"
t.integer "reporter_id"
t.bigint "reporter_id"
t.string "status", default: "Open"
t.datetime "updated_at"
t.index ["affected_id"], name: "index_feedback_messages_on_affected_id"
@ -593,17 +593,17 @@ ActiveRecord::Schema.define(version: 2020_08_06_193438) do
end
create_table "html_variant_successes", force: :cascade do |t|
t.integer "article_id"
t.bigint "article_id"
t.datetime "created_at", null: false
t.integer "html_variant_id"
t.bigint "html_variant_id"
t.datetime "updated_at", null: false
t.index ["html_variant_id", "article_id"], name: "index_html_variant_successes_on_html_variant_id_and_article_id"
end
create_table "html_variant_trials", force: :cascade do |t|
t.integer "article_id"
t.bigint "article_id"
t.datetime "created_at", null: false
t.integer "html_variant_id"
t.bigint "html_variant_id"
t.datetime "updated_at", null: false
t.index ["html_variant_id", "article_id"], name: "index_html_variant_trials_on_html_variant_id_and_article_id"
end
@ -1313,7 +1313,7 @@ ActiveRecord::Schema.define(version: 2020_08_06_193438) do
create_table "users_roles", id: false, force: :cascade do |t|
t.bigint "role_id"
t.integer "user_id"
t.bigint "user_id"
t.index ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id"
end