2018-11-13 02:27:31 -05:00
|
|
|
class MigrateOldArtifacts < ActiveRecord::Migration[4.2]
|
2017-05-04 17:02:51 -04:00
|
|
|
include Gitlab::Database::MigrationHelpers
|
|
|
|
|
|
|
|
DOWNTIME = false
|
|
|
|
|
|
|
|
disable_ddl_transaction!
|
|
|
|
|
|
|
|
# This uses special heuristic to find potential candidates for data migration
|
|
|
|
# Read more about this here: https://gitlab.com/gitlab-org/gitlab-ce/issues/32036#note_30422345
|
2017-08-15 13:44:37 -04:00
|
|
|
|
2017-05-04 17:02:51 -04:00
|
|
|
def up
|
|
|
|
builds_with_artifacts.find_each do |build|
|
|
|
|
build.migrate_artifacts!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def builds_with_artifacts
|
|
|
|
Build.with_artifacts
|
|
|
|
.joins('JOIN projects ON projects.id = ci_builds.project_id')
|
2017-06-01 03:52:19 -04:00
|
|
|
.where('ci_builds.id < ?', min_id)
|
2017-05-04 17:02:51 -04:00
|
|
|
.where('projects.ci_id IS NOT NULL')
|
|
|
|
.select('id', 'created_at', 'project_id', 'projects.ci_id AS ci_id')
|
|
|
|
end
|
|
|
|
|
|
|
|
def min_id
|
|
|
|
Build.joins('JOIN projects ON projects.id = ci_builds.project_id')
|
|
|
|
.where('projects.ci_id IS NULL')
|
2017-06-01 03:52:19 -04:00
|
|
|
.pluck('coalesce(min(ci_builds.id), 0)')
|
2017-05-04 17:02:51 -04:00
|
|
|
.first
|
|
|
|
end
|
|
|
|
|
|
|
|
class Build < ActiveRecord::Base
|
|
|
|
self.table_name = 'ci_builds'
|
|
|
|
|
2017-06-01 03:52:19 -04:00
|
|
|
scope :with_artifacts, -> { where.not(artifacts_file: [nil, '']) }
|
2017-05-04 17:02:51 -04:00
|
|
|
|
|
|
|
def migrate_artifacts!
|
2017-06-01 03:52:19 -04:00
|
|
|
return unless File.exist?(source_artifacts_path)
|
|
|
|
return if File.exist?(target_artifacts_path)
|
2017-05-04 17:02:51 -04:00
|
|
|
|
|
|
|
ensure_target_path
|
|
|
|
|
|
|
|
FileUtils.move(source_artifacts_path, target_artifacts_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def source_artifacts_path
|
2017-08-15 13:44:37 -04:00
|
|
|
@source_artifacts_path ||=
|
2017-05-04 17:02:51 -04:00
|
|
|
File.join(Gitlab.config.artifacts.path,
|
|
|
|
created_at.utc.strftime('%Y_%m'),
|
|
|
|
ci_id.to_s, id.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_artifacts_path
|
2017-08-15 13:44:37 -04:00
|
|
|
@target_artifacts_path ||=
|
2017-05-04 17:02:51 -04:00
|
|
|
File.join(Gitlab.config.artifacts.path,
|
|
|
|
created_at.utc.strftime('%Y_%m'),
|
|
|
|
project_id.to_s, id.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_target_path
|
|
|
|
directory = File.dirname(target_artifacts_path)
|
|
|
|
FileUtils.mkdir_p(directory) unless Dir.exist?(directory)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|