Import URL migration doesn't rescue exceptions - fails migration in some cases

A user on IRC reported issues upgrading from 8.5.0 to 8.8.2.

If there is a bad URL in some project's import_url field, the migration will fail. For example, a bad URL might be https://git@gitlab.example.com:group/project.git. In cases like this, the migration should be smart enough to rescue the exception, throw out the bad value (or leave it as-is), and continue on the migration.

The migration this is triggered by is the RemoveWrongImportUrlFromProjects one. More specifically, the problem occurs in the Gitlab::UrlSanitizer intializer when Addressable::URI.parse is called.

Migration stack trace:

StandardError: An error has occurred, this and all later migrations canceled:
             
              Invalid port number: "group"/opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/addressable-2.3.8/lib/addressable/uri.rb:1283:in `port='
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/addressable-2.3.8/lib/addressable/uri.rb:803:in `block in initialize'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/addressable-2.3.8/lib/addressable/uri.rb:2248:in `call'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/addressable-2.3.8/lib/addressable/uri.rb:2248:in `defer_validation'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/addressable-2.3.8/lib/addressable/uri.rb:795:in `initialize'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/addressable-2.3.8/lib/addressable/uri.rb:138:in `new'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/addressable-2.3.8/lib/addressable/uri.rb:138:in `parse'
              /opt/gitlab/embedded/service/gitlab-rails/lib/gitlab/url_sanitizer.rb:10:in `initialize'
              /opt/gitlab/embedded/service/gitlab-rails/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb:27:in `new'
              /opt/gitlab/embedded/service/gitlab-rails/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb:27:in `block in process_projects_with_wrong_url'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/result.rb:51:in `block in each'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/result.rb:51:in `each'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/result.rb:51:in `each'
              /opt/gitlab/embedded/service/gitlab-rails/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb:25:in `process_projects_with_wrong_url'
              /opt/gitlab/embedded/service/gitlab-rails/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb:16:in `block in up'
              /opt/gitlab/embedded/service/gitlab-rails/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb:63:in `block (2 levels) in in_transaction'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:220:in `transaction'
              /opt/gitlab/embedded/service/gitlab-rails/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb:62:in `block in in_transaction'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:634:in `block in say_with_time'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:634:in `say_with_time'
              /opt/gitlab/embedded/service/gitlab-rails/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb:61:in `in_transaction'
              /opt/gitlab/embedded/service/gitlab-rails/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb:16:in `up'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:611:in `exec_migration'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:591:in `block in migrate'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:590:in `migrate'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:768:in `migrate'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:1044:in `block in ddl_transaction'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:220:in `transaction'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:1044:in `ddl_transaction'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:959:in `block in migrate'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:955:in `each'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:955:in `migrate'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:823:in `up'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:801:in `migrate'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
              /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/activerecord-4.2.6/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'

cc/ @rdavila @jameslopez Looks like both of you have looked at this migration before. Do you mind taking a look, please? It would be nice to make it more robust for the future since many customers will hit this migration when they upgrade over the next few months.