rapid7/metasploit_data_models — pull request #32 by jvennix-r7 (2013-07-16 18:55:04+00:00)
Title: Tweak migration to merge duplicate hosts in the same workspace

### PT #51335651
RM #7988

Verification steps:

1. First let's test against the scenario where the user has duplicate hosts in the same workspace:
$ rake db:drop; rake db:create; rake db:migrate VERSION=20130430151353
$ psql metasploit_data_models_development < ~/Downloads/test_uniq_migration.sql
2. Now verify that the correct data is in the database:
$ cd spec/dummy
$ rails c
> Mdm::Host.count #=> 6
> Mdm::Host.where(:address => '10.6.201.174', :workspace_id => 1).count #=> 3
> Mdm::Service.count #=> 3
> exit
3. Now run the rest of the migrations:
$ cd ../..
$ rake db:migrate:up VERSION=20130430162145
4. You should see the message "Duplicate hosts in workspace found. Merging host references." in the console log.
5. Verify that the duped hosts had their data merged:
$ cd spec/dummy
$ rails c
> Mdm::Host.count #=> 4
> Mdm::Host.where(:address => '10.6.201.174', :workspace_id => 1).count #=> 1
> Mdm::Client.where(:host_id => Mdm::Host.find_by_address('10.6.201.174').id).count #=> 2
> Mdm::Event.find_all_by_host_id(Mdm::Host.find_by_address('10.6.201.174').id).length #=> 2
> Mdm::ExploitAttempt.where(:host_id => Mdm::Host.find_by_address('10.6.201.174').id).count #=> 4
> Mdm::ExploitedHost.where(:host_id => Mdm::Host.find_by_address('10.6.201.174').id).count #=> 1
> Mdm::Host.find_by_address('10.6.201.174').host_details.length #=> 1
> Mdm::Host.find_by_address('10.6.201.174').loots.count #=> 6
> Mdm::Host.find_by_address('10.6.201.174').notes.count #=> 5
> Mdm::Host.find_by_address('10.6.201.174').sessions.count #=> 3
> Mdm::Host.find_by_address('10.6.201.174').services.count #=> 3
> Mdm::Host.find_by_address('10.6.201.174').tags.count #=> 3
> Mdm::Host.find_by_address('10.6.201.174').vulns.count #=> 4

6. Now let's ensure that a db with no duped records is not affected by our
$ rake db:drop; rake db:create; rake db:migrate VERSION=20130430151353
$ psql metasploit_data_models_development < ~/Downloads/normal_db.sql
$ rake db:migrate:up VERSION=20130430162145
7. You should *not* see "Duplicate hosts in workspace found. Merging host references." in the console log