Commit e3657501 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Enhance migrate CI emails

parent dc4e2744
...@@ -3,13 +3,16 @@ class MigrateCiEmails < ActiveRecord::Migration ...@@ -3,13 +3,16 @@ class MigrateCiEmails < ActiveRecord::Migration
def up def up
# This inserts a new service: BuildsEmailService # This inserts a new service: BuildsEmailService
# It also "manually" constructs the properties (JSON-encoded) # It "manually" constructs the properties (JSON-encoded)
# Migrating all ci_projects e-mail related columns # Migrating all ci_projects e-mail related columns
execute( execute(
'INSERT INTO services (project_id, type, created_at, updated_at, active, push_events, issues_events, merge_requests_events, tag_push_events, note_events, build_events, properties) ' \ 'INSERT INTO services (project_id, type, created_at, updated_at, active, push_events, issues_events, merge_requests_events, tag_push_events, note_events, build_events, properties) ' \
"SELECT projects.id, 'BuildsEmailService', ci_services.created_at, ci_services.updated_at, #{true_value}, #{false_value}, #{false_value}, #{false_value}, #{false_value}, #{false_value}, #{true_value}, " \ "SELECT projects.id, 'BuildsEmailService', ci_services.created_at, ci_services.updated_at, " \
"CONCAT('{\"notify_only_broken_builds\":\"', ci_projects.email_only_broken_builds, " \ "#{true_value}, #{false_value}, #{false_value}, #{false_value}, #{false_value}, #{false_value}, #{true_value}, " \
"'\",\"add_pusher\":\"', ci_projects.email_add_pusher, '\",\"recipients\":\"', ci_projects.email_recipients, '\"}') " \ "CONCAT('{\"notify_only_broken_builds\":\"', #{convert_bool('ci_projects.email_only_broken_builds')}, " \
"'\",\"add_pusher\":\"', #{convert_bool('ci_projects.email_add_pusher')}, " \
"'\",\"recipients\":\"', #{escape_text('ci_projects.email_recipients')}, " \
"'\"}') " \
'FROM ci_services ' \ 'FROM ci_services ' \
'JOIN ci_projects ON ci_services.project_id = ci_projects.id ' \ 'JOIN ci_projects ON ci_services.project_id = ci_projects.id ' \
'JOIN projects ON ci_projects.gitlab_id = projects.id ' \ 'JOIN projects ON ci_projects.gitlab_id = projects.id ' \
...@@ -19,4 +22,20 @@ class MigrateCiEmails < ActiveRecord::Migration ...@@ -19,4 +22,20 @@ class MigrateCiEmails < ActiveRecord::Migration
def down def down
end end
# This function escapes double-quotes and slash
def escape_text(name)
"REPLACE(REPLACE(#{name}, '\\', '\\\\'), '\"', '\\\"')"
end
# This function returns 0 or 1 for column
def convert_bool(name)
if self.postgresql?
# PostgreSQL uses BOOLEAN type
"CASE WHEN #{name} IS TRUE THEN '1' ELSE '0' END;"
else
# MySQL uses TINYINT
"#{name}"
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