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
This commit is contained in:
Sam 2018-06-20 17:50:11 +10:00
parent cb824a6b33
commit 44091f20c6
3 changed files with 57 additions and 14 deletions

View File

@ -527,6 +527,23 @@ module Discourse
end end
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 SIDEKIQ_NAMESPACE ||= 'sidekiq'.freeze
def self.sidekiq_redis_config def self.sidekiq_redis_config

View File

@ -2,19 +2,14 @@ class ActiveRecord::Base
# Execute SQL manually # Execute SQL manually
def self.exec_sql(*args) 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 conn = ActiveRecord::Base.connection
sql = ActiveRecord::Base.send(:sanitize_sql_array, args) sql = ActiveRecord::Base.send(:sanitize_sql_array, args)
conn.raw_connection.async_exec(sql) conn.raw_connection.async_exec(sql)
end 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) def exec_sql(*args)
ActiveRecord::Base.exec_sql(*args) ActiveRecord::Base.exec_sql(*args)
end end
@ -36,10 +31,4 @@ class ActiveRecord::Base
end end
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 end

View File

@ -226,4 +226,41 @@ describe Discourse do
end end
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 end