Commit 6b58586e authored by Andreas Brandl's avatar Andreas Brandl

Merge branch 'ali/test-migration' into 'master'

DB testing: Artifact full query log for each migration

See merge request gitlab-org/gitlab!60020
parents 8415ff28 b48bb39a
...@@ -7,7 +7,8 @@ module Gitlab ...@@ -7,7 +7,8 @@ module Gitlab
def self.all_observers def self.all_observers
[ [
TotalDatabaseSizeChange.new, TotalDatabaseSizeChange.new,
QueryStatistics.new QueryStatistics.new,
QueryLog.new
] ]
end end
end end
......
# frozen_string_literal: true
module Gitlab
module Database
module Migrations
module Observers
class QueryLog < MigrationObserver
def before
@logger_was = ActiveRecord::Base.logger
@log_file_path = File.join(Instrumentation::RESULT_DIR, 'current.log')
@logger = Logger.new(@log_file_path)
ActiveRecord::Base.logger = @logger
end
def after
ActiveRecord::Base.logger = @logger_was
@logger.close
end
def record(observation)
File.rename(@log_file_path, File.join(Instrumentation::RESULT_DIR, "#{observation.migration}.log"))
end
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Database::Migrations::Observers::QueryLog do
subject { described_class.new }
let(:observation) { Gitlab::Database::Migrations::Observation.new(migration) }
let(:connection) { ActiveRecord::Base.connection }
let(:query) { 'select 1' }
let(:directory_path) { Dir.mktmpdir }
let(:log_file) { "#{directory_path}/current.log" }
let(:migration) { 20210422152437 }
before do
stub_const('Gitlab::Database::Migrations::Instrumentation::RESULT_DIR', directory_path)
end
after do
FileUtils.remove_entry(directory_path)
end
it 'writes a file with the query log' do
observe
expect(File.read("#{directory_path}/#{migration}.log")).to include(query)
end
it 'does not change the default logger' do
expect { observe }.not_to change { ActiveRecord::Base.logger }
end
def observe
subject.before
connection.execute(query)
subject.after
subject.record(observation)
end
end
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