2016-06-09 08:39:16 -04:00
|
|
|
# rubocop:disable all
|
2015-12-09 10:31:42 -05:00
|
|
|
class MigrateCiEmails < ActiveRecord::Migration
|
2017-04-10 06:23:28 -04:00
|
|
|
include Gitlab::Database::MigrationHelpers
|
2015-12-09 10:31:42 -05:00
|
|
|
|
|
|
|
def up
|
2015-12-10 08:08:09 -05:00
|
|
|
# This inserts a new service: BuildsEmailService
|
2015-12-11 07:24:38 -05:00
|
|
|
# It "manually" constructs the properties (JSON-encoded)
|
2015-12-10 08:08:09 -05:00
|
|
|
# Migrating all ci_projects e-mail related columns
|
2015-12-09 10:31:42 -05:00
|
|
|
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) ' \
|
2015-12-11 07:24:38 -05:00
|
|
|
"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}, " \
|
|
|
|
"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')}, " \
|
|
|
|
"'\"}') " \
|
2015-12-09 10:31:42 -05:00
|
|
|
'FROM ci_services ' \
|
|
|
|
'JOIN ci_projects ON ci_services.project_id = ci_projects.id ' \
|
|
|
|
'JOIN projects ON ci_projects.gitlab_id = projects.id ' \
|
|
|
|
"WHERE ci_services.type = 'Ci::MailService' AND ci_services.active"
|
|
|
|
)
|
|
|
|
end
|
2015-12-10 08:08:09 -05:00
|
|
|
|
|
|
|
def down
|
|
|
|
end
|
2015-12-11 07:24:38 -05:00
|
|
|
|
|
|
|
# This function escapes double-quotes and slash
|
|
|
|
def escape_text(name)
|
2015-12-14 06:03:28 -05:00
|
|
|
if Gitlab::Database.postgresql?
|
|
|
|
"REPLACE(REPLACE(#{name}, '\\', '\\\\'), '\"', '\\\"')"
|
|
|
|
else
|
|
|
|
"REPLACE(REPLACE(#{name}, '\\\\', '\\\\\\\\'), '\\\"', '\\\\\\\"')"
|
|
|
|
end
|
2015-12-11 07:24:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# This function returns 0 or 1 for column
|
|
|
|
def convert_bool(name)
|
2015-12-14 05:04:47 -05:00
|
|
|
if Gitlab::Database.postgresql?
|
2015-12-11 07:24:38 -05:00
|
|
|
# PostgreSQL uses BOOLEAN type
|
2015-12-14 05:04:47 -05:00
|
|
|
"CASE WHEN #{name} IS TRUE THEN '1' ELSE '0' END"
|
2015-12-11 07:24:38 -05:00
|
|
|
else
|
|
|
|
# MySQL uses TINYINT
|
|
|
|
"#{name}"
|
|
|
|
end
|
|
|
|
end
|
2015-12-09 10:31:42 -05:00
|
|
|
end
|