up

in db/migrate/20150601043231_migrate_jobs_to_yaml.rb [5:74]


  def up
    select_all("SELECT * FROM projects").each do |project|
      config = {}

      concatenate_expression = if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
                                 "string_agg(tags.name, ',')"
                               else
                                 "GROUP_CONCAT(tags.name SEPARATOR ',')"
                               end

      sql = "SELECT j.*, #{concatenate_expression} tags
        FROM jobs j
          LEFT JOIN taggings tgs ON j.id = tgs.taggable_id AND tgs.taggable_type = 'Job'
          LEFT JOIN tags ON tgs.tag_id = tags.id
        WHERE project_id = #{project['id']}
          AND active = true
          AND job_type = 'parallel'
        GROUP BY j.id"

      
      skip_refs = []

      if project["skip_refs"].present?
        skip_refs = project["skip_refs"].split(",").map(&:strip).select{|ref| ref =~  /^[\w-]*\Z/ }
      end


      
      select_all(sql).each do |job|
        config[job["name"].to_s] = {
          script: job["commands"] && job["commands"].split("\n").map(&:strip),
          tags: job["tags"] && job["tags"].split(",").map(&:strip)
        }

        except = build_except_param(parse_boolean_value(job["build_branches"]), parse_boolean_value(job["build_tags"]))
        except = except + skip_refs

        if except.any?
          config[job["name"].to_s][:except] = except
        end
      end

      
      select_all(sql.sub("parallel", 'deploy')).each do |job|
        config[job["name"].to_s] = {
          script: job["commands"] && job["commands"].split("\n").map(&:strip),
          type: "deploy",
          tags: job["tags"] && job["tags"].split(",").map(&:strip)
        }

        if job["refs"].present?
          config[job["name"].to_s][:only] = job["refs"].split(",").map(&:strip)
        else
          except = build_except_param(parse_boolean_value(job["build_branches"]), parse_boolean_value(job["build_tags"]))
          except = except + skip_refs

          if except.any?
            config[job["name"].to_s][:except] = except
          end
        end
      end

      yaml_config = YAML.dump(config.deep_stringify_keys)

      yaml_config.sub!("---", "# This file is generated by GitLab CI")

      execute("UPDATE projects SET generated_yaml_config = '#{quote_string(yaml_config)}' WHERE projects.id = #{project["id"]}")
    end
  end