Skip to content

Commit d30e65d

Browse files
committed
Add migration mode for db migrations
Can be executed with `bundle exec rake db:migrate WITH_BENCHMARK=true`
1 parent b8730d2 commit d30e65d

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/cloud_controller/db_migrator.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,25 @@ def initialize(db, max_migration_duration_in_minutes=nil, max_migration_statemen
2929
def apply_migrations(opts={})
3030
Sequel.extension :migration
3131
require 'vcap/sequel_case_insensitive_string_monkeypatch'
32-
Sequel::Migrator.run(@db, SEQUEL_MIGRATIONS, opts)
32+
33+
if ENV.fetch('WITH_BENCHMARK', false)
34+
# rubocop:disable Rails/Output
35+
puts('######################################')
36+
puts('# Starting migrations with benchmark #')
37+
puts('######################################')
38+
require 'benchmark'
39+
bm_output = Benchmark.measure { Sequel::Migrator.run(@db, SEQUEL_MIGRATIONS, opts) }
40+
puts('###########')
41+
puts('# Results #')
42+
puts('###########')
43+
puts("Total time for all migrations: #{bm_output.total} seconds")
44+
puts("System time for all migrations: #{bm_output.stime} seconds")
45+
puts("User time for all migrations: #{bm_output.utime} seconds")
46+
puts("Real time for all migrations: #{bm_output.real} seconds")
47+
# rubocop:enable Rails/Output
48+
else
49+
Sequel::Migrator.run(@db, SEQUEL_MIGRATIONS, opts)
50+
end
3351
end
3452

3553
def rollback(number_to_rollback)

spec/unit/lib/cloud_controller/db_migrator_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,22 @@
7676
DBMigrator.new(db, nil, nil, 1234)
7777
end
7878
end
79+
80+
describe 'benchmark' do
81+
let(:migrator) { DBMigrator.new(db) }
82+
83+
it 'runs migrations without benchmark' do
84+
expect(Benchmark).not_to receive(:measure)
85+
migrator.apply_migrations
86+
end
87+
88+
context 'when setting WITH_BENCHMARK env' do
89+
before { ENV['WITH_BENCHMARK'] = 'true' }
90+
91+
it 'runs migrations with benchmark' do
92+
expect(Benchmark).to receive(:measure).and_return(double(:bm_output, total: 1, stime: 2, utime: 3, real: 4))
93+
expect { migrator.apply_migrations }.to output(/Starting migrations with benchmark/).to_stdout
94+
end
95+
end
96+
end
7997
end

0 commit comments

Comments
 (0)