Skip to content

Commit 0e435d3

Browse files
committed
Move Msf::DBManager#migrate(d) to module
[#50179803] Move Msf::DBManager#migrate and the migrated attribute to Msf::DBManager::Migration module to lower complexity of db_manager.rb and in preparation for more migration related code on this branch.
1 parent 94bc3bf commit 0e435d3

File tree

4 files changed

+152
-33
lines changed

4 files changed

+152
-33
lines changed

lib/msf/core/db_manager.rb

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'msf/base/config'
44
require 'msf/core'
55
require 'msf/core/db'
6+
require 'msf/core/db_manager/migration'
67
require 'msf/core/task_manager'
78
require 'fileutils'
89
require 'shellwords'
@@ -17,6 +18,9 @@ module Msf
1718
###
1819

1920
class DBManager
21+
# Provides :framework and other accessors
22+
include Msf::DBManager::Migration
23+
include Msf::Framework::Offspring
2024

2125
# Mainly, it's Ruby 1.9.1 that cause a lot of problems now, along with Ruby 1.8.6.
2226
# Ruby 1.8.7 actually seems okay, but why tempt fate? Let's say 1.9.3 and beyond.
@@ -28,9 +32,6 @@ def warn_about_rubies
2832
end
2933
end
3034

31-
# Provides :framework and other accessors
32-
include Framework::Offspring
33-
3435
# Returns true if we are ready to load/store data
3536
def active
3637
return false if not @usable
@@ -53,9 +54,6 @@ def active
5354
# Stores a TaskManager for serializing database events
5455
attr_accessor :sink
5556

56-
# Flag to indicate database migration has completed
57-
attr_accessor :migrated
58-
5957
# Flag to indicate that modules are cached
6058
attr_accessor :modules_cached
6159

@@ -278,33 +276,6 @@ def disconnect
278276
end
279277
end
280278

281-
# Migrate database to latest schema version.
282-
#
283-
# @param verbose [Boolean] see ActiveRecord::Migration.verbose
284-
# @return [Array<ActiveRecord::MigrationProxy] List of migrations that ran.
285-
#
286-
# @see ActiveRecord::Migrator.migrate
287-
def migrate(verbose=false)
288-
ran = []
289-
ActiveRecord::Migration.verbose = verbose
290-
291-
ActiveRecord::Base.connection_pool.with_connection do
292-
begin
293-
ran = ActiveRecord::Migrator.migrate(
294-
ActiveRecord::Migrator.migrations_paths
295-
)
296-
# ActiveRecord::Migrator#migrate rescues all errors and re-raises them as
297-
# StandardError
298-
rescue StandardError => error
299-
self.error = error
300-
elog("DB.migrate threw an exception: #{error}")
301-
dlog("Call stack:\n#{error.backtrace.join "\n"}")
302-
end
303-
end
304-
305-
return ran
306-
end
307-
308279
def workspace=(workspace)
309280
@workspace_name = workspace.name
310281
end

lib/msf/core/db_manager/migration.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module Msf
2+
class DBManager
3+
module Migration
4+
# Migrate database to latest schema version.
5+
#
6+
# @param verbose [Boolean] see ActiveRecord::Migration.verbose
7+
# @return [Array<ActiveRecord::MigrationProxy] List of migrations that
8+
# ran.
9+
#
10+
# @see ActiveRecord::Migrator.migrate
11+
def migrate(verbose=false)
12+
ran = []
13+
ActiveRecord::Migration.verbose = verbose
14+
15+
ActiveRecord::Base.connection_pool.with_connection do
16+
begin
17+
ran = ActiveRecord::Migrator.migrate(
18+
ActiveRecord::Migrator.migrations_paths
19+
)
20+
# ActiveRecord::Migrator#migrate rescues all errors and re-raises them
21+
# as StandardError
22+
rescue StandardError => error
23+
self.error = error
24+
elog("DB.migrate threw an exception: #{error}")
25+
dlog("Call stack:\n#{error.backtrace.join "\n"}")
26+
end
27+
end
28+
29+
return ran
30+
end
31+
32+
# Flag to indicate database migration has completed
33+
#
34+
# @return [Boolean]
35+
attr_accessor :migrated
36+
end
37+
end
38+
end

spec/lib/msf/db_manager_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
db_manager
1919
end
2020

21+
it_should_behave_like 'Msf::DBManager::Migration'
2122
it_should_behave_like 'Msf::DBManager::ImportMsfXml'
2223

2324
context '#purge_all_module_details' do
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
shared_examples_for 'Msf::DBManager::Migration' do
2+
it { should be_a Msf::DBManager::Migration }
3+
4+
context '#migrate' do
5+
def migrate
6+
db_manager.migrate
7+
end
8+
9+
it 'should create a connection' do
10+
ActiveRecord::Base.connection_pool.should_receive(:with_connection).twice
11+
12+
migrate
13+
end
14+
15+
it 'should call ActiveRecord::Migrator.migrate' do
16+
ActiveRecord::Migrator.should_receive(:migrate).with(
17+
ActiveRecord::Migrator.migrations_paths
18+
)
19+
20+
migrate
21+
end
22+
23+
it 'should return migrations that were ran from ActiveRecord::Migrator.migrate' do
24+
migrations = [mock('Migration 1')]
25+
ActiveRecord::Migrator.stub(:migrate => migrations)
26+
27+
migrate.should == migrations
28+
end
29+
30+
context 'with StandardError from ActiveRecord::Migration.migrate' do
31+
let(:error) do
32+
StandardError.new(message)
33+
end
34+
35+
let(:message) do
36+
"Error during migration"
37+
end
38+
39+
before(:each) do
40+
ActiveRecord::Migrator.stub(:migrate).and_raise(error)
41+
end
42+
43+
it 'should set Msf::DBManager#error' do
44+
migrate
45+
46+
db_manager.error.should == error
47+
end
48+
49+
it 'should log error message at error level' do
50+
db_manager.should_receive(:elog) do |error_message|
51+
error_message.should include(error.to_s)
52+
end
53+
54+
migrate
55+
end
56+
57+
it 'should log error backtrace at debug level' do
58+
db_manager.should_receive(:dlog) do |debug_message|
59+
debug_message.should include('Call stack')
60+
end
61+
62+
migrate
63+
end
64+
end
65+
66+
context 'with verbose' do
67+
def migrate
68+
db_manager.migrate(verbose)
69+
end
70+
71+
context 'false' do
72+
let(:verbose) do
73+
false
74+
end
75+
76+
it 'should set ActiveRecord::Migration.verbose to false' do
77+
ActiveRecord::Migration.should_receive(:verbose=).with(verbose)
78+
79+
migrate
80+
end
81+
end
82+
83+
context 'true' do
84+
let(:verbose) do
85+
true
86+
end
87+
88+
it 'should set ActiveRecord::Migration.verbose to true' do
89+
ActiveRecord::Migration.should_receive(:verbose=).with(verbose)
90+
91+
migrate
92+
end
93+
end
94+
end
95+
96+
context 'without verbose' do
97+
it 'should set ActiveRecord::Migration.verbose to false' do
98+
ActiveRecord::Migration.should_receive(:verbose=).with(false)
99+
100+
db_manager.migrate
101+
end
102+
end
103+
end
104+
105+
context '#migrated' do
106+
it { should respond_to :migrated }
107+
it { should respond_to :migrated= }
108+
end
109+
end

0 commit comments

Comments
 (0)