summaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
authorJames Lopez <james@jameslopez.es>2016-03-28 16:35:03 +0200
committerJames Lopez <james@jameslopez.es>2016-03-28 16:35:03 +0200
commit459ad34493c57b40fd431b18750fef85884d51e1 (patch)
tree80e4f89cb3db54d2adde3a22840af3a46536559f /db
parentfcb85381cf025da5e8cfecdc1a4afc016b5cb6ae (diff)
downloadgitlab-ce-459ad34493c57b40fd431b18750fef85884d51e1.tar.gz
refactored code based on feedback plus fixed a couple of other issues
Diffstat (limited to 'db')
-rw-r--r--db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb66
1 files changed, 44 insertions, 22 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 a55ab6a42b4..fb5d8591c09 100644
--- a/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb
+++ b/db/migrate/20160302152808_remove_wrong_import_url_from_projects.rb
@@ -16,14 +16,12 @@ class RemoveWrongImportUrlFromProjects < ActiveRecord::Migration
say("Projects and Github projects with a wrong URL. It also migrates Gitlab project credentials.")
in_transaction { process_projects_with_wrong_url }
- say("Migrating bitbucket credentials...")
- in_transaction { process_project(import_type: 'bitbucket') }
+ say("Migrating bitbucket credentials...")# TODO remove last param
+ in_transaction { process_project(import_type: 'bitbucket', unencrypted_data: ['repo', 'user_map']) }
say("Migrating fogbugz credentials...")
- in_transaction { process_project(import_type: 'fogbugz') }
+ in_transaction { process_project(import_type: 'fogbugz', unencrypted_data: ['repo', 'user_map']) }
- say("Migrating google code credentials...")
- in_transaction { process_project(import_type: 'google_code') }
end
def process_projects_with_wrong_url
@@ -35,19 +33,29 @@ class RemoveWrongImportUrlFromProjects < ActiveRecord::Migration
end
end
- def process_project(import_type: )
+ def process_project(import_type: , unencrypted_data: [])
unencrypted_import_data(import_type: import_type).each do |data|
- replace_data_credentials(data)
+ replace_data_credentials(data, unencrypted_data)
end
end
- def replace_data_credentials(data)
- data_hash = YAML::load(data['data']) if data['data']
+ def replace_data_credentials(data, unencrypted_data)
+ data_hash = JSON.load(data['data']) if data['data']
if defined?(data_hash) && !data_hash.blank?
- update_with_encrypted_data(data_hash, data['id'])
+ unencrypted_data_hash = encrypted_data_hash(data_hash, unencrypted_data)
+ update_with_encrypted_data(data_hash, data['id'], unencrypted_data_hash)
end
end
+ def encrypted_data_hash(data_hash, unencrypted_data)
+ return 'NULL' if unencrypted_data.empty?
+ new_data_hash = {}
+ unencrypted_data.each do |key|
+ new_data_hash[key] = data_hash.delete(key) if data_hash[key]
+ end
+ quote(new_data_hash.to_json)
+ end
+
def in_transaction
say_with_time("Processing new transaction...") do
ActiveRecord::Base.transaction do
@@ -59,18 +67,18 @@ class RemoveWrongImportUrlFromProjects < ActiveRecord::Migration
def update_import_data(import_url, project)
fake_import_data = FakeProjectImportData.new
fake_import_data.credentials = import_url.credentials
- project_import_data = project_import_data(project['id'])
- if project_import_data
- execute(update_import_data_sql(project_import_data['id'], fake_import_data))
+ import_data_id = project['import_data_id']
+ if import_data_id
+ execute(update_import_data_sql(import_data_id, fake_import_data))
else
execute(insert_import_data_sql(project['id'], fake_import_data))
end
end
- def update_with_encrypted_data(data_hash, import_data_id)
+ def update_with_encrypted_data(data_hash, import_data_id, data_array = nil)
fake_import_data = FakeProjectImportData.new
fake_import_data.credentials = data_hash
- execute(update_import_data_sql(import_data_id, fake_import_data))
+ execute(update_import_data_sql(import_data_id, fake_import_data, data_array))
end
def update_import_url(import_url, project)
@@ -78,16 +86,34 @@ class RemoveWrongImportUrlFromProjects < ActiveRecord::Migration
end
def insert_import_data_sql(project_id, fake_import_data)
- %( INSERT into project_import_data (encrypted_credentials, project_id, encrypted_credentials_iv, encrypted_credentials_salt) VALUES ( #{quote(fake_import_data.encrypted_credentials)}, '#{project_id}', #{quote(fake_import_data.encrypted_credentials_iv)}, #{quote(fake_import_data.encrypted_credentials_salt)}))
+ %(
+ INSERT INTO project_import_data
+ (encrypted_credentials,
+ project_id,
+ encrypted_credentials_iv,
+ encrypted_credentials_salt)
+ VALUES ( #{quote(fake_import_data.encrypted_credentials)},
+ '#{project_id}',
+ #{quote(fake_import_data.encrypted_credentials_iv)},
+ #{quote(fake_import_data.encrypted_credentials_salt)})
+ ).squish
end
def update_import_data_sql(id, fake_import_data, data = 'NULL')
- %( UPDATE project_import_data SET encrypted_credentials = #{quote(fake_import_data.encrypted_credentials)}, encrypted_credentials_iv = #{quote(fake_import_data.encrypted_credentials_iv)}, encrypted_credentials_salt = #{quote(fake_import_data.encrypted_credentials_salt)}, data = #{data} WHERE id = '#{id}')
+ %(
+ UPDATE project_import_data
+ SET encrypted_credentials = #{quote(fake_import_data.encrypted_credentials)},
+ encrypted_credentials_iv = #{quote(fake_import_data.encrypted_credentials_iv)},
+ encrypted_credentials_salt = #{quote(fake_import_data.encrypted_credentials_salt)},
+ data = #{data}
+ WHERE id = '#{id}'
+ ).squish
end
#Github projects with token, and any user:password@ based URL
+ #TODO: may need to add import_type != list
def projects_with_wrong_import_url
- select_all("SELECT p.id, p.import_url FROM projects p WHERE p.import_url IS NOT NULL AND p.import_url LIKE '%//%@%'")
+ select_all("SELECT p.id, p.import_url, i.id as import_data_id FROM projects p LEFT JOIN project_import_data i on p.id = i.id WHERE p.import_url IS NOT NULL AND p.import_url LIKE '%//%@%'")
end
# All imports with data for import_type
@@ -95,10 +121,6 @@ class RemoveWrongImportUrlFromProjects < ActiveRecord::Migration
select_all("SELECT i.id, p.import_url, i.data FROM projects p INNER JOIN project_import_data i ON p.id = i.project_id WHERE p.import_url IS NOT NULL AND p.import_type = '#{import_type}' ")
end
- def project_import_data(project_id)
- select_one("SELECT id FROM project_import_data WHERE project_id = '#{project_id}'")
- end
-
def quote(value)
ActiveRecord::Base.connection.quote(value)
end