2016-11-24 14:07:44 +00:00
|
|
|
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
|
|
|
|
# for more information on how to write migrations for GitLab.
|
|
|
|
|
|
|
|
class MigrateProcessCommitWorkerJobs < ActiveRecord::Migration
|
|
|
|
include Gitlab::Database::MigrationHelpers
|
|
|
|
|
|
|
|
class Project < ActiveRecord::Base
|
|
|
|
def self.find_including_path(id)
|
2017-02-22 22:50:15 +00:00
|
|
|
select("projects.*, CONCAT(namespaces.path, '/', projects.path) AS path_with_namespace").
|
|
|
|
joins('INNER JOIN namespaces ON namespaces.id = projects.namespace_id').
|
|
|
|
find_by(id: id)
|
2016-11-24 14:07:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def repository_storage_path
|
|
|
|
Gitlab.config.repositories.storages[repository_storage]
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository_path
|
|
|
|
File.join(repository_storage_path, read_attribute(:path_with_namespace) + '.git')
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
|
|
|
@repository ||= Rugged::Repository.new(repository_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
DOWNTIME = true
|
2017-02-22 22:34:45 +00:00
|
|
|
DOWNTIME_REASON = 'Existing workers will error until they are using a newer version of the code'
|
2016-11-24 14:07:44 +00:00
|
|
|
|
|
|
|
disable_ddl_transaction!
|
|
|
|
|
|
|
|
def up
|
|
|
|
Sidekiq.redis do |redis|
|
|
|
|
new_jobs = []
|
|
|
|
|
|
|
|
while job = redis.lpop('queue:process_commit')
|
2017-02-22 00:42:22 +00:00
|
|
|
payload = JSON.parse(job)
|
2016-11-24 14:07:44 +00:00
|
|
|
project = Project.find_including_path(payload['args'][0])
|
|
|
|
|
|
|
|
next unless project
|
|
|
|
|
|
|
|
begin
|
|
|
|
commit = project.repository.lookup(payload['args'][2])
|
|
|
|
rescue Rugged::OdbError
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
hash = {
|
|
|
|
id: commit.oid,
|
2016-12-13 15:49:17 +00:00
|
|
|
message: encode(commit.message),
|
2016-11-24 14:07:44 +00:00
|
|
|
parent_ids: commit.parent_ids,
|
|
|
|
authored_date: commit.author[:time],
|
2016-12-13 15:49:17 +00:00
|
|
|
author_name: encode(commit.author[:name]),
|
|
|
|
author_email: encode(commit.author[:email]),
|
2016-11-24 14:07:44 +00:00
|
|
|
committed_date: commit.committer[:time],
|
2016-12-13 15:49:17 +00:00
|
|
|
committer_email: encode(commit.committer[:email]),
|
|
|
|
committer_name: encode(commit.committer[:name])
|
2016-11-24 14:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
payload['args'][2] = hash
|
|
|
|
|
|
|
|
new_jobs << JSON.dump(payload)
|
|
|
|
end
|
|
|
|
|
|
|
|
redis.multi do |multi|
|
|
|
|
new_jobs.each do |j|
|
|
|
|
multi.lpush('queue:process_commit', j)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
Sidekiq.redis do |redis|
|
|
|
|
new_jobs = []
|
|
|
|
|
|
|
|
while job = redis.lpop('queue:process_commit')
|
2017-02-22 00:42:22 +00:00
|
|
|
payload = JSON.parse(job)
|
2016-11-24 14:07:44 +00:00
|
|
|
|
|
|
|
payload['args'][2] = payload['args'][2]['id']
|
|
|
|
|
|
|
|
new_jobs << JSON.dump(payload)
|
|
|
|
end
|
|
|
|
|
|
|
|
redis.multi do |multi|
|
|
|
|
new_jobs.each do |j|
|
|
|
|
multi.lpush('queue:process_commit', j)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-13 15:49:17 +00:00
|
|
|
|
|
|
|
def encode(data)
|
|
|
|
encoding = Encoding::UTF_8
|
|
|
|
|
|
|
|
if data.encoding == encoding
|
|
|
|
data
|
|
|
|
else
|
|
|
|
data.encode(encoding, invalid: :replace, undef: :replace)
|
|
|
|
end
|
|
|
|
end
|
2016-11-24 14:07:44 +00:00
|
|
|
end
|