@@ -56,9 +56,6 @@ def active
56
56
# Flag to indicate database migration has completed
57
57
attr_accessor :migrated
58
58
59
- # Array of additional migration paths
60
- attr_accessor :migration_paths
61
-
62
59
# Flag to indicate that modules are cached
63
60
attr_accessor :modules_cached
64
61
@@ -69,7 +66,6 @@ def initialize(framework, opts = {})
69
66
70
67
self . framework = framework
71
68
self . migrated = false
72
- self . migration_paths = [ ]
73
69
self . modules_cached = false
74
70
self . modules_caching = false
75
71
@@ -82,17 +78,17 @@ def initialize(framework, opts = {})
82
78
end
83
79
84
80
initialize_database_support
85
-
86
- # have to set migration paths after initialize_database_support as it loads
87
- # MetasploitDataModels.
88
- self . migration_paths << MetasploitDataModels . root . join ( 'db' , 'migrate' ) . to_s
89
81
end
90
82
91
- #
92
- # Add additional migration paths
93
- #
94
- def add_migration_path ( path )
95
- self . migration_paths . push ( path )
83
+ def initialize_metasploit_data_models
84
+ # Provide access to ActiveRecord models shared w/ commercial versions
85
+ require "metasploit_data_models"
86
+
87
+ metasploit_data_model_migrations_pathname = MetasploitDataModels . root . join (
88
+ 'db' ,
89
+ 'migrate'
90
+ )
91
+ ActiveRecord ::Migrator . migrations_paths << metasploit_data_model_migrations_pathname . to_s
96
92
end
97
93
98
94
#
@@ -105,8 +101,7 @@ def initialize_database_support
105
101
106
102
require "active_record"
107
103
108
- # Provide access to ActiveRecord models shared w/ commercial versions
109
- require "metasploit_data_models"
104
+ initialize_metasploit_data_models
110
105
111
106
# Patches issues with ActiveRecord
112
107
require "msf/core/patches/active_record"
@@ -283,45 +278,31 @@ def disconnect
283
278
end
284
279
end
285
280
281
+ # Migrate database to latest schema version.
286
282
#
287
- # Migrate database to latest schema version
283
+ # @param verbose [Boolean] see ActiveRecord::Migration.verbose
284
+ # @return [Array<ActiveRecord::Migration>] List of migrations that ran.
288
285
#
286
+ # @see ActiveRecord::Migrator.migrate
289
287
def migrate ( verbose = false )
288
+ ran = [ ]
289
+ ActiveRecord ::Migration . verbose = verbose
290
290
291
- temp_dir = ::File . expand_path ( ::File . join ( Msf ::Config . config_directory , "schema" , "#{ Time . now . to_i } _#{ $$} " ) )
292
- ::FileUtils . rm_rf ( temp_dir )
293
- ::FileUtils . mkdir_p ( temp_dir )
294
-
295
- self . migration_paths . each do |mpath |
296
- dir = Dir . new ( mpath ) rescue nil
297
- if not dir
298
- elog ( "Could not access migration path #{ mpath } " )
299
- next
300
- end
301
-
302
- dir . entries . each do |ent |
303
- next unless ent =~ /^\d +.*\. rb$/
304
- ::FileUtils . cp ( ::File . join ( mpath , ent ) , ::File . join ( temp_dir , ent ) )
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 " } " )
305
302
end
306
303
end
307
304
308
- success = true
309
- begin
310
-
311
- ::ActiveRecord ::Base . connection_pool . with_connection {
312
- ActiveRecord ::Migration . verbose = verbose
313
- ActiveRecord ::Migrator . migrate ( temp_dir , nil )
314
- }
315
- rescue ::Exception => e
316
- self . error = e
317
- elog ( "DB.migrate threw an exception: #{ e } " )
318
- dlog ( "Call stack:\n #{ e . backtrace . join "\n " } " )
319
- success = false
320
- end
321
-
322
- ::FileUtils . rm_rf ( temp_dir )
323
-
324
- return true
305
+ return ran
325
306
end
326
307
327
308
def workspace = ( workspace )
0 commit comments