Commit 9a3d0f1d authored by Kamil Trzcinski's avatar Kamil Trzcinski

Add rake task to migrate CI tags

parent ebd06d7e
class AddCiTags < ActiveRecord::Migration
def change
create_table "ci_taggings", force: true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context", limit: 128
t.datetime "created_at"
end
add_index "ci_taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "ci_taggings_idx", unique: true, using: :btree
add_index "ci_taggings", ["taggable_id", "taggable_type", "context"], name: "index_ci_taggings_on_taggable_id_and_taggable_type_and_context", using: :btree
create_table "ci_tags", force: true do |t|
t.string "name"
t.integer "taggings_count", default: 0
end
add_index "ci_tags", ["name"], name: "index_ci_tags_on_name", unique: true, using: :btree
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150902001023) do
ActiveRecord::Schema.define(version: 20150914215247) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -223,6 +223,26 @@ ActiveRecord::Schema.define(version: 20150902001023) do
add_index "ci_sessions", ["session_id"], name: "index_ci_sessions_on_session_id", using: :btree
add_index "ci_sessions", ["updated_at"], name: "index_ci_sessions_on_updated_at", using: :btree
create_table "ci_taggings", force: true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context", limit: 128
t.datetime "created_at"
end
add_index "ci_taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "ci_taggings_idx", unique: true, using: :btree
add_index "ci_taggings", ["taggable_id", "taggable_type", "context"], name: "index_ci_taggings_on_taggable_id_and_taggable_type_and_context", using: :btree
create_table "ci_tags", force: true do |t|
t.string "name"
t.integer "taggings_count", default: 0
end
add_index "ci_tags", ["name"], name: "index_ci_tags_on_name", unique: true, using: :btree
create_table "ci_trigger_requests", force: true do |t|
t.integer "trigger_id", null: false
t.text "variables"
......
namespace :ci do
namespace :migrate do
def list_objects(type)
ids = ActiveRecord::Base.connection.select_all(
'select distinct taggable_id from ci_taggings where taggable_type = $1',
nil, [[nil, type]]
)
ids.map { |id| id['taggable_id'] }
end
def list_tags(type, id)
tags = ActiveRecord::Base.connection.select_all(
'select ci_tags.name from ci_tags ' +
'join ci_taggings on ci_tags.id = ci_taggings.tag_id ' +
'where taggable_type = $1 and taggable_id = $2 and context = $3',
nil, [[nil, type], [nil, id], [nil, 'tags']]
)
tags.map { |tag| tag['name'] }
end
desc 'GITLAB | Migrate CI tags'
task tags: :environment do
list_objects('Runner').each do |id|
runner = Ci::Runner.find_by_id(id)
if runner
tags = list_tags('Runner', id)
runner.update_attributes(tag_list: tags)
end
end
list_objects('Build').each do |id|
build = Ci::Build.find_by_id(id)
if build
tags = list_tags('Build', id)
build.update_attributes(tag_list: tags)
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment