From 44091f20c6e3530b4b97f1258d0d4d855621eff0 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 20 Jun 2018 17:50:11 +1000 Subject: [PATCH] DEV: allow for method deprecation using Discourse.deprecate New method deprecator will ensure one log message an hour happens for all deprecated method calls per call site Also removes unused monkey patches to ActiveRecord::Base --- lib/discourse.rb | 17 +++++++++++ lib/freedom_patches/active_record_base.rb | 17 ++--------- spec/components/discourse_spec.rb | 37 +++++++++++++++++++++++ 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/lib/discourse.rb b/lib/discourse.rb index 83e95d51311..d8eec52897c 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -527,6 +527,23 @@ module Discourse end end + def self.deprecate(warning) + location = caller_locations[1] + warning = "Deprecation Notice: #{warning}\nAt: #{location.label} #{location.path}:#{location.lineno}" + if Rails.env == "development" + STDERR.puts(warning) + end + + digest = Digest::MD5.hexdigest(warning) + redis_key = "deprecate-notice-#{digest}" + + if !$redis.without_namespace.get(redis_key) + Rails.logger.warn(warning) + $redis.without_namespace.setex(redis_key, 3600, "x") + end + warning + end + SIDEKIQ_NAMESPACE ||= 'sidekiq'.freeze def self.sidekiq_redis_config diff --git a/lib/freedom_patches/active_record_base.rb b/lib/freedom_patches/active_record_base.rb index b65ebc0616b..a15d08fba33 100644 --- a/lib/freedom_patches/active_record_base.rb +++ b/lib/freedom_patches/active_record_base.rb @@ -2,19 +2,14 @@ class ActiveRecord::Base # Execute SQL manually def self.exec_sql(*args) + + Discourse.deprecate("exec_sql should not be used anymore, please use DB.exec or DB.query instead!") + conn = ActiveRecord::Base.connection sql = ActiveRecord::Base.send(:sanitize_sql_array, args) conn.raw_connection.async_exec(sql) end - def self.exec_sql_row_count(*args) - exec_sql(*args).cmd_tuples - end - - def self.sql_fragment(*sql_array) - ActiveRecord::Base.send(:sanitize_sql_array, sql_array) - end - def exec_sql(*args) ActiveRecord::Base.exec_sql(*args) end @@ -36,10 +31,4 @@ class ActiveRecord::Base end end - # Support for psql. If we want to support multiple RDBMs in the future we can - # split this. - def exec_sql_row_count(*args) - exec_sql(*args).cmd_tuples - end - end diff --git a/spec/components/discourse_spec.rb b/spec/components/discourse_spec.rb index 64d3c6c112b..b8030d84f75 100644 --- a/spec/components/discourse_spec.rb +++ b/spec/components/discourse_spec.rb @@ -226,4 +226,41 @@ describe Discourse do end end + context '#deprecate' do + + class FakeLogger + attr_reader :warnings + def warn(m) + @warnings ||= [] + @warnings << m + end + end + + def old_method(m) + Discourse.deprecate(m) + end + + def old_method_caller(m) + old_method(m) + end + + before do + @orig_logger = Rails.logger + Rails.logger = @fake_logger = FakeLogger.new + end + + after do + Rails.logger = @orig_logger + end + + it 'can deprecate usage' do + k = SecureRandom.hex + expect(old_method_caller(k)).to include("old_method_caller") + expect(old_method_caller(k)).to include("discourse_spec") + expect(old_method_caller(k)).to include(k) + + expect(@fake_logger.warnings).to eq([old_method_caller(k)]) + end + end + end