Module: Msf::DBManager::Migration

Included in:
Msf::DBManager
Defined in:
lib/msf/core/db_manager/migration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#migratedBoolean?

Flag to indicate database migration has completed

Returns:

  • (Boolean, nil)


76
77
78
# File 'lib/msf/core/db_manager/migration.rb', line 76

def migrated
  @migrated
end

Instance Method Details

#add_rails_engine_migration_pathsObject

Loads Metasploit Data Models and adds gathers migration paths.



6
7
8
9
10
11
12
13
14
# File 'lib/msf/core/db_manager/migration.rb', line 6

def add_rails_engine_migration_paths
  unless defined? ActiveRecord
    fail "Bundle installed '--without #{Bundler.settings.without.join(' ')}'.  To clear the without option do " \
         "`bundle install --without ''` (the --without flag with an empty string) or `rm -rf .bundle` to remove " \
         "the .bundle/config manually and then `bundle install`"
  end

  gather_engine_migration_paths
end

#migrate(config = nil, verbose = false) ⇒ Array<ActiveRecord::MigrationProxy] List of migrations that ran.

Migrate database to latest schema version.

Parameters:

  • config (Hash) (defaults to: nil)

    see ActiveRecord::Base.establish_connection

  • verbose (Boolean) (defaults to: false)

    see ActiveRecord::Migration.verbose

Returns:

  • (Array<ActiveRecord::MigrationProxy] List of migrations that ran.)

    Array<ActiveRecord::MigrationProxy] List of migrations that ran.

See Also:

  • ActiveRecord::MigrationContext.migrate


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/msf/core/db_manager/migration.rb', line 24

def migrate(config=nil, verbose=false)
  ran = []
  # Rails 5 changes ActiveRecord parents means to migrate outside
  # the `rake` task framework has to dig a little lower into ActiveRecord
  # to set up the DB connection capable of interacting with migration.
  previouslyConnected = ActiveRecord::Base.connected?
  unless previouslyConnected
    ApplicationRecord.remove_connection
    ActiveRecord::Base.establish_connection(config)
  end
  ActiveRecord::Migration.verbose = verbose
  ActiveRecord::Base.connection_pool.with_connection do
    begin
      context = default_migration_context
      if needs_migration?(context)
        ran = context.migrate
      end
        # ActiveRecord::Migrator#migrate rescues all errors and re-raises them
        # as StandardError
    rescue StandardError => error
      self.error = error
      elog('DB.migrate threw an exception', error: error)
    end
  end

  unless previouslyConnected
    ActiveRecord::Base.remove_connection
    ApplicationRecord.establish_connection(config)
  end
  # Since the connections that existed before the migrations ran could
  # have outdated column information, reset column information for all
  # ApplicationRecord descendents to prevent missing method errors for
  # column methods for columns created in migrations after the column
  # information was cached.
  reset_column_information

  return ran
end

#needs_migration?(context = default_migration_context) ⇒ Boolean

Determine if the currently established database connection needs migration

Parameters:

  • context (ActiveRecord::MigrationContext, snil) (defaults to: default_migration_context)

    The migration context to check. Will default if not supplied

Returns:

  • (Boolean)

    True if migration is required, false otherwise



67
68
69
70
71
# File 'lib/msf/core/db_manager/migration.rb', line 67

def needs_migration?(context = default_migration_context)
  ActiveRecord::Base.connection_pool.with_connection do
    return context.needs_migration?
  end
end