Make removal of topic columns more resilient to deploys

This commit is contained in:
Sam 2016-12-05 12:11:46 +11:00
parent 33d0a23d84
commit 1db9d17756
4 changed files with 77 additions and 1 deletions

View File

@ -110,6 +110,32 @@ class ApplicationController < ActionController::Base
end
end
def self.last_ar_cache_reset
@last_ar_cache_reset
end
def self.last_ar_cache_reset=(val)
@last_ar_cache_reset
end
rescue_from ActiveRecord::StatementInvalid do |e|
last_cache_reset = ApplicationController.last_ar_cache_reset
if e.message =~ /UndefinedColumn/ && (last_cache_reset.nil? || last_cache_reset < 30.seconds.ago)
Rails.logger.warn "Clear Active Record cache cause schema appears to have changed!"
ApplicationController.last_ar_cache_reset = Time.zone.now
ActiveRecord::Base.connection.query_cache.clear
(ActiveRecord::Base.connection.tables - %w[schema_migrations]).each do |table|
table.classify.constantize.reset_column_information rescue nil
end
end
raise e
end
class PluginDisabled < StandardError; end
# Handles requests for giant IDs that throw pg exceptions

View File

@ -32,7 +32,7 @@ duration = Rails.env.production? ? 60 : 0
if User.exec_sql("SELECT 1 FROM schema_migration_details
WHERE EXISTS(
SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'users' AND column_name = 'last_redirected_to_top_at'
WHERE table_schema = 'public' AND table_name = 'users' AND column_name = 'last_redirected_to_top_at'
) AND
name = 'MoveTrackingOptionsToUserOptions' AND
created_at < (current_timestamp at time zone 'UTC' - interval '#{duration} minutes')

View File

@ -64,3 +64,32 @@ if seed_welcome_topics
skip_validations: true,
category: staff ? staff.name : nil)
end
# run this later, cause we need to make sure new application controller resilience is in place first
duration = Rails.env.production? ? 60 : 0
if Topic.exec_sql("SELECT 1 FROM schema_migration_details
WHERE EXISTS(
SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'public' AND table_name = 'topics' AND column_name = 'inappropriate_count'
) AND
name = 'AddTopicColumnsBack' AND
created_at < (current_timestamp at time zone 'UTC' - interval '#{duration} minutes')
").to_a.length > 0
Topic.transaction do
STDERR.puts "Removing superflous topic columns!"
%w[
inappropriate_count
bookmark_count
off_topic_count
illegal_count
notify_user_count
].each do |column|
User.exec_sql("ALTER TABLE topics DROP COLUMN IF EXISTS #{column}")
end
end
end

View File

@ -0,0 +1,21 @@
class AddTopicColumnsBack < ActiveRecord::Migration
# This really sucks big time, we have no use for these columns yet can not remove them
# if we remove them then sites will be down during migration
def up
add_column :topics, :bookmark_count, :int
add_column :topics, :off_topic_count, :int
add_column :topics, :illegal_count, :int
add_column :topics, :inappropriate_count, :int
add_column :topics, :notify_user_count, :int
end
def down
remove_column :topics, :bookmark_count
remove_column :topics, :off_topic_count
remove_column :topics, :illegal_count
remove_column :topics, :inappropriate_count
remove_column :topics, :notify_user_count
end
end