From 1db9d17756f7a5704f6f9f81ace155f4d98b684c Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 5 Dec 2016 12:11:46 +1100 Subject: [PATCH] Make removal of topic columns more resilient to deploys --- app/controllers/application_controller.rb | 26 +++++++++++++++++ db/fixtures/009_users.rb | 2 +- db/fixtures/999_topics.rb | 29 +++++++++++++++++++ .../20161205001727_add_topic_columns_back.rb | 21 ++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20161205001727_add_topic_columns_back.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 795a46dfa4b..8323528ce47 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/db/fixtures/009_users.rb b/db/fixtures/009_users.rb index d65c8f2ef73..dd8a203e6b7 100644 --- a/db/fixtures/009_users.rb +++ b/db/fixtures/009_users.rb @@ -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') diff --git a/db/fixtures/999_topics.rb b/db/fixtures/999_topics.rb index cfe48615bcc..e46419fd20e 100644 --- a/db/fixtures/999_topics.rb +++ b/db/fixtures/999_topics.rb @@ -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 diff --git a/db/migrate/20161205001727_add_topic_columns_back.rb b/db/migrate/20161205001727_add_topic_columns_back.rb new file mode 100644 index 00000000000..d464c54a9e6 --- /dev/null +++ b/db/migrate/20161205001727_add_topic_columns_back.rb @@ -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