Commit 932d1d20 authored by Simon Tomlinson's avatar Simon Tomlinson Committed by Mayra Cabrera

Metadata file indicating migration output version

Start with version 2, as the first version was implicit and did not use
a metadata file.
parent 881dadc0
......@@ -17,7 +17,11 @@ module Gitlab
def observe(version:, name:, connection:, &block)
observation = Observation.new(version: version, name: name, success: false)
observers = observer_classes.map { |c| c.new(observation, @result_dir, connection) }
per_migration_result_dir = File.join(@result_dir, name)
FileUtils.mkdir_p(per_migration_result_dir)
observers = observer_classes.map { |c| c.new(observation, per_migration_result_dir, connection) }
on_each_observer(observers) { |observer| observer.before }
......
......@@ -6,7 +6,7 @@ module Gitlab
module Observers
class QueryDetails < MigrationObserver
def before
file_path = File.join(output_dir, "#{observation.version}_#{observation.name}-query-details.json")
file_path = File.join(output_dir, "query-details.json")
@file = File.open(file_path, 'wb')
@writer = Oj::StreamWriter.new(@file, {})
@writer.push_array
......
......@@ -7,7 +7,7 @@ module Gitlab
class QueryLog < MigrationObserver
def before
@logger_was = ActiveRecord::Base.logger
file_path = File.join(output_dir, "#{observation.version}_#{observation.name}.log")
file_path = File.join(output_dir, "migration.log")
@logger = Logger.new(file_path)
ActiveRecord::Base.logger = @logger
end
......
......@@ -6,7 +6,7 @@ module Gitlab
module Observers
class TransactionDuration < MigrationObserver
def before
file_path = File.join(output_dir, "#{observation.version}_#{observation.name}-transaction-duration.json")
file_path = File.join(output_dir, "transaction-duration.json")
@file = File.open(file_path, 'wb')
@writer = Oj::StreamWriter.new(@file, {})
@writer.push_array
......
......@@ -5,6 +5,8 @@ module Gitlab
module Migrations
class Runner
BASE_RESULT_DIR = Rails.root.join('tmp', 'migration-testing').freeze
METADATA_FILENAME = 'metadata.json'
SCHEMA_VERSION = 2 # Version of the output format produced by the runner
class << self
def up
......@@ -75,9 +77,11 @@ module Gitlab
end
ensure
if instrumentation
File.open(File.join(result_dir, Gitlab::Database::Migrations::Instrumentation::STATS_FILENAME), 'wb+') do |io|
io << instrumentation.observations.to_json
end
stats_filename = File.join(result_dir, Gitlab::Database::Migrations::Instrumentation::STATS_FILENAME)
File.write(stats_filename, instrumentation.observations.to_json)
metadata_filename = File.join(result_dir, METADATA_FILENAME)
File.write(metadata_filename, { version: SCHEMA_VERSION }.to_json)
end
# We clear the cache here to mirror the cache clearing that happens at the end of `db:migrate` tasks
......
......@@ -9,7 +9,7 @@ RSpec.describe Gitlab::Database::Migrations::Observers::QueryDetails do
let(:query) { "select date_trunc('day', $1::timestamptz) + $2 * (interval '1 hour')" }
let(:query_binds) { [Time.current, 3] }
let(:directory_path) { Dir.mktmpdir }
let(:log_file) { "#{directory_path}/#{migration_version}_#{migration_name}-query-details.json" }
let(:log_file) { "#{directory_path}/query-details.json" }
let(:query_details) { Gitlab::Json.parse(File.read(log_file)) }
let(:migration_version) { 20210422152437 }
let(:migration_name) { 'test' }
......
......@@ -18,7 +18,7 @@ RSpec.describe Gitlab::Database::Migrations::Observers::QueryLog do
it 'writes a file with the query log' do
observe
expect(File.read("#{directory_path}/#{migration_version}_#{migration_name}.log")).to include(query)
expect(File.read("#{directory_path}/migration.log")).to include(query)
end
it 'does not change the default logger' do
......
......@@ -7,7 +7,7 @@ RSpec.describe Gitlab::Database::Migrations::Observers::TransactionDuration do
let(:connection) { ActiveRecord::Migration.connection }
let(:observation) { Gitlab::Database::Migrations::Observation.new(version: migration_version, name: migration_name) }
let(:directory_path) { Dir.mktmpdir }
let(:log_file) { "#{directory_path}/#{migration_version}_#{migration_name}-transaction-duration.json" }
let(:log_file) { "#{directory_path}/transaction-duration.json" }
let(:transaction_duration) { Gitlab::Json.parse(File.read(log_file)) }
let(:migration_version) { 20210422152437 }
let(:migration_name) { 'test' }
......
......@@ -79,6 +79,15 @@ RSpec.describe Gitlab::Database::Migrations::Runner do
expect(migration_runs.map(&:dir)).to match_array([:up, :up])
expect(migration_runs.map(&:version_to_migrate)).to eq(pending_migrations.map(&:version))
end
it 'writes a metadata file with the current schema version' do
up.run
metadata_file = result_dir.join('up', described_class::METADATA_FILENAME)
expect(metadata_file.exist?).to be_truthy
metadata = Gitlab::Json.parse(File.read(metadata_file))
expect(metadata).to match('version' => described_class::SCHEMA_VERSION)
end
end
end
......@@ -105,5 +114,14 @@ RSpec.describe Gitlab::Database::Migrations::Runner do
expect(migration_runs.map(&:version_to_migrate)).to eq(applied_migrations_this_branch.reverse.map(&:version))
end
end
it 'writes a metadata file with the current schema version' do
down.run
metadata_file = result_dir.join('down', described_class::METADATA_FILENAME)
expect(metadata_file.exist?).to be_truthy
metadata = Gitlab::Json.parse(File.read(metadata_file))
expect(metadata).to match('version' => described_class::SCHEMA_VERSION)
end
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