Commit 158e92ec authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '350651-fix-remaining-code-offenses-using-activerecord-base-methods-4' into 'master'

Fix use of ActiveRecord::Base in Seeder

See merge request gitlab-org/gitlab!81482
parents 9f23f82b e9af31bb
......@@ -62,10 +62,6 @@ module Gitlab
end
def self.quiet
# Disable database insertion logs so speed isn't limited by ability to print to console
old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil
# Additional seed logic for models.
Project.include(ProjectSeed)
User.include(UserSeed)
......@@ -75,9 +71,11 @@ module Gitlab
SeedFu.quiet = true
without_statement_timeout do
without_new_note_notifications do
yield
without_database_logging do
without_statement_timeout do
without_new_note_notifications do
yield
end
end
end
......@@ -85,7 +83,6 @@ module Gitlab
ensure
SeedFu.quiet = false
ActionMailer::Base.perform_deliveries = old_perform_deliveries
ActiveRecord::Base.logger = old_logger
end
def self.without_gitaly_timeout
......@@ -112,10 +109,30 @@ module Gitlab
end
def self.without_statement_timeout
ActiveRecord::Base.connection.execute('SET statement_timeout=0')
Gitlab::Database::EachDatabase.each_database_connection do |connection|
connection.execute('SET statement_timeout=0')
end
yield
ensure
Gitlab::Database::EachDatabase.each_database_connection do |connection|
connection.execute('RESET statement_timeout')
end
end
def self.without_database_logging
old_loggers = Gitlab::Database.database_base_models.transform_values do |model|
model.logger
end
Gitlab::Database.database_base_models.each do |_, model|
model.logger = nil
end
yield
ensure
ActiveRecord::Base.connection.execute('RESET statement_timeout')
Gitlab::Database.database_base_models.each do |connection_name, model|
model.logger = old_loggers[connection_name]
end
end
end
end
......
......@@ -4,6 +4,26 @@ require 'spec_helper'
RSpec.describe Gitlab::Seeder do
describe '.quiet' do
let(:database_base_models) do
{
main: ApplicationRecord,
ci: Ci::ApplicationRecord
}
end
it 'disables database logging' do
allow(Gitlab::Database).to receive(:database_base_models)
.and_return(database_base_models.with_indifferent_access)
described_class.quiet do
expect(ApplicationRecord.logger).to be_nil
expect(Ci::ApplicationRecord.logger).to be_nil
end
expect(ApplicationRecord.logger).not_to be_nil
expect(Ci::ApplicationRecord.logger).not_to be_nil
end
it 'disables mail deliveries' do
expect(ActionMailer::Base.perform_deliveries).to eq(true)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment