Skip to content
Snippets Groups Projects
Commit 45a105b1 authored by Kamil Trzcinski's avatar Kamil Trzcinski
Browse files

Use pure SQL queries to migrate CI tags

parent a6f2caf7
No related branches found
No related tags found
No related merge requests found
Loading
@@ -4,45 +4,37 @@ module Ci
Loading
@@ -4,45 +4,37 @@ module Ci
module Migrate module Migrate
class Tags class Tags
def restore def restore
puts 'Migrating tags for Runners... ' ActiveRecord::Base.transaction do
list_objects('Runner').each do |id| puts 'Inserting tags...'
putc '.' connection.execute(
runner = Ci::Runner.find_by_id(id) 'INSERT INTO tags (name) ' +
if runner 'SELECT ci_tags.name FROM ci_tags ' +
tags = list_tags('Runner', id) 'WHERE (SELECT COUNT(*) FROM tags WHERE tags.name = ci_tags.name)=0'
runner.update_attributes(tag_list: tags) )
end
end puts 'Deleting old records'
puts '' connection.execute "DELETE FROM taggings WHERE context = 'tags' AND taggable_type LIKE 'Ci::%'"
puts 'Inserting tags...'
connection.execute(
'INSERT INTO taggings (taggable_type, taggable_id, tag_id, context) ' +
"SELECT CONCAT('Ci::', ci_taggings.taggable_type), ci_taggings.taggable_id, tags.id, 'tags' FROM ci_taggings " +
'JOIN ci_tags ON ci_tags.id = ci_taggings.tag_id ' +
'JOIN tags ON tags.name = ci_tags.name '
)
   
puts 'Migrating tags for Builds... ' puts 'Resetting counters... '
list_objects('Build').each do |id| connection.execute(
putc '.' 'UPDATE tags SET ' +
build = Ci::Build.find_by_id(id) 'taggings_count = (SELECT COUNT(*) FROM taggings WHERE tags.id = taggings.tag_id)'
if build )
tags = list_tags('Build', id)
build.update_attributes(tag_list: tags)
end
end end
puts ''
end end
   
protected protected
   
def list_objects(type) def connection
ids = ActiveRecord::Base.connection.select_all( ActiveRecord::Base.connection
"select distinct taggable_id from ci_taggings where taggable_type = #{ActiveRecord::Base::sanitize(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 = #{ActiveRecord::Base::sanitize(type)} and taggable_id = #{ActiveRecord::Base::sanitize(id)} and context = 'tags'"
)
tags.map { |tag| tag['name'] }
end end
end end
end end
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment