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 ...@@ -62,10 +62,6 @@ module Gitlab
end end
def self.quiet 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. # Additional seed logic for models.
Project.include(ProjectSeed) Project.include(ProjectSeed)
User.include(UserSeed) User.include(UserSeed)
...@@ -75,17 +71,18 @@ module Gitlab ...@@ -75,17 +71,18 @@ module Gitlab
SeedFu.quiet = true SeedFu.quiet = true
without_database_logging do
without_statement_timeout do without_statement_timeout do
without_new_note_notifications do without_new_note_notifications do
yield yield
end end
end end
end
puts "\nOK".color(:green) puts "\nOK".color(:green)
ensure ensure
SeedFu.quiet = false SeedFu.quiet = false
ActionMailer::Base.perform_deliveries = old_perform_deliveries ActionMailer::Base.perform_deliveries = old_perform_deliveries
ActiveRecord::Base.logger = old_logger
end end
def self.without_gitaly_timeout def self.without_gitaly_timeout
...@@ -112,10 +109,30 @@ module Gitlab ...@@ -112,10 +109,30 @@ module Gitlab
end end
def self.without_statement_timeout 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 yield
ensure ensure
ActiveRecord::Base.connection.execute('RESET statement_timeout') 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
Gitlab::Database.database_base_models.each do |connection_name, model|
model.logger = old_loggers[connection_name]
end
end end
end end
end end
......
...@@ -4,6 +4,26 @@ require 'spec_helper' ...@@ -4,6 +4,26 @@ require 'spec_helper'
RSpec.describe Gitlab::Seeder do RSpec.describe Gitlab::Seeder do
describe '.quiet' 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 it 'disables mail deliveries' do
expect(ActionMailer::Base.perform_deliveries).to eq(true) 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