From f70fa3eb45104304a1ea5a33edb0ea8bea3f0d32 Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Fri, 12 Jun 2015 17:50:55 +0300 Subject: migration jobs to yaml --- db/migrate/20150601043225_migrate_jobs_to_yaml.rb | 65 ------------------ db/migrate/20150601043226_migrate_jobs_to_yaml.rb | 82 +++++++++++++++++++++++ 2 files changed, 82 insertions(+), 65 deletions(-) delete mode 100644 db/migrate/20150601043225_migrate_jobs_to_yaml.rb create mode 100644 db/migrate/20150601043226_migrate_jobs_to_yaml.rb (limited to 'db') diff --git a/db/migrate/20150601043225_migrate_jobs_to_yaml.rb b/db/migrate/20150601043225_migrate_jobs_to_yaml.rb deleted file mode 100644 index aaf10a7..0000000 --- a/db/migrate/20150601043225_migrate_jobs_to_yaml.rb +++ /dev/null @@ -1,65 +0,0 @@ -# Migration tested on MySQL and PostgreSQL. -# Can be performed online without errors. -# This migration will loop through all projects and jobs, so it can take some time. - -class MigrateJobsToYaml < ActiveRecord::Migration - def up - select_all("SELECT * FROM projects").each do |project| - config = {jobs: [], deploy_jobs: []} - - 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" - - # Create Jobs - select_all(sql).each do |job| - config[:jobs] << { - script: job["commands"] && job["commands"].split("\n").map(&:strip), - name: job["name"], - branches: parse_boolean_value(job["build_branches"]), - tags: parse_boolean_value(job["build_tags"]), - runner: job["tags"] - } - end - - # Create Deploy Jobs - select_all(sql.sub("parallel", 'deploy')).each do |job| - config[:deploy_jobs] << { - script: job["commands"] && job["commands"].split("\n").map(&:strip), - name: job["name"], - refs: job["refs"], - runner: job["tags"] - } - end - - config[:skip_refs] = project["skip_refs"] - - 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 - - def down - - end - - private - - def parse_boolean_value(value) - [ true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON' ].include?(value) - end -end diff --git a/db/migrate/20150601043226_migrate_jobs_to_yaml.rb b/db/migrate/20150601043226_migrate_jobs_to_yaml.rb new file mode 100644 index 0000000..ba7a7dc --- /dev/null +++ b/db/migrate/20150601043226_migrate_jobs_to_yaml.rb @@ -0,0 +1,82 @@ +# Migration tested on MySQL and PostgreSQL. +# Can be performed online without errors. +# This migration will loop through all projects and jobs, so it can take some time. + +class MigrateJobsToYaml < ActiveRecord::Migration + 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" + + # Create Jobs + select_all(sql).each do |job| + config[job["name"].to_s] = { + test: job["commands"] && job["commands"].split("\n").map(&:strip), + tags: job["tags"] + } + + except = build_except_param(parse_boolean_value(job["build_branches"]), parse_boolean_value(job["build_tags"])) + + if except + config[job["name"].to_s][:except] = except + end + end + + # Create Deploy Jobs + select_all(sql.sub("parallel", 'deploy')).each do |job| + config[job["name"].to_s] = { + deploy: job["commands"] && job["commands"].split("\n").map(&:strip), + tags: job["tags"] + } + + except = build_except_param(parse_boolean_value(job["build_branches"]), parse_boolean_value(job["build_tags"])) + + if except + config[job["name"].to_s][:except] = except + 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 + + def down + + end + + private + + def parse_boolean_value(value) + [ true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON' ].include?(value) + end + + def build_except_param(branches, tags) + unless branches + return ["branches"] + end + + unless tags + return ["tags"] + end + + false + end +end -- cgit v1.2.1