summaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
authorJames Lopez <james@jameslopez.es>2016-03-07 12:50:35 +0100
committerJames Lopez <james@jameslopez.es>2016-03-07 12:50:35 +0100
commit735563329d1f86ee4d72b37cd22eed1168935e8e (patch)
tree71f2354eeab407a06101dbf96696a72475d35727 /db
parent7085850c50a6dd7072bd2c80f092b0c20f74d1dc (diff)
downloadgitlab-ce-735563329d1f86ee4d72b37cd22eed1168935e8e.tar.gz
refactored a bunch of stuff based on MR feedback
Diffstat (limited to 'db')
-rw-r--r--db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb38
1 files changed, 22 insertions, 16 deletions
diff --git a/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb b/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb
index dfa9f2d4dee..881af783c61 100644
--- a/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb
+++ b/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb
@@ -2,43 +2,49 @@ class RemoveWrongImportUrlFromProjects < ActiveRecord::Migration
class ImportUrlSanitizer
def initialize(url)
- @url = url
+ @url = URI.parse(url)
end
def sanitized_url
- @sanitized_url ||= @url[regex_extractor, 1] + @url[regex_extractor, 3]
+ @sanitized_url ||= safe_url
end
def credentials
- @credentials ||= @url[regex_extractor, 2]
+ @credentials ||= { user: @url.user, password: @url.password }
end
private
- # Regex matches 1 <first part of URL>, 2 <token or to be encrypted stuff>,
- # 3 <last part of URL>
- def regex_extractor
- /(.*\/\/)(.*)(\@.*)/
+ def safe_url
+ safe_url = @url.dup
+ safe_url.password = nil
+ safe_url.user = nil
+ safe_url
end
+
+ end
+
+ class FakeProjectImportData
+ extend AttrEncrypted
+ attr_accessor :credentials
+ attr_encrypted :credentials, key: Gitlab::Application.secrets.db_key_base, marshal: true, encode: true
end
def up
- projects_with_wrong_import_url.each do |project_id|
- project = Project.find(project_id["id"])
- sanitizer = ImportUrlSanitizer.new(project.import_url)
+ projects_with_wrong_import_url.each do |project|
+ sanitizer = ImportUrlSanitizer.new(project["import_url"])
ActiveRecord::Base.transaction do
- project.update_columns(import_url: sanitizer.sanitized_url)
- if project.import_data
- project.import_data.credentials = sanitizer.credentials
- project.save!
- end
+ execute("UPDATE projects SET import_url = '#{sanitizer.sanitized_url}' WHERE id = #{project['id']}")
+ fake_import_data = FakeProjectImportData.new
+ fake_import_data.credentials = sanitizer.credentials
+ execute("UPDATE project_import_data SET encrypted_credentials = '#{fake_import_data.encrypted_credentials}' WHERE project_id = #{project['id']}")
end
end
end
def projects_with_wrong_import_url
# TODO Check live with #operations for possible false positives. Also, consider regex? But may have issues MySQL/PSQL
- select_all("SELECT p.id from projects p WHERE p.import_url LIKE '%//%:%@%' or p.import_url like '#{"_"*40}@github.com%'")
+ select_all("SELECT p.id, p.import_url from projects p WHERE p.import_url LIKE '%//%:%@%' or p.import_url like '#{"_"*40}@github.com%'")
end
end