BigW Consortium Gitlab

20151209145909_migrate_ci_emails.rb 1.76 KB
Newer Older
1
# rubocop:disable all
2 3 4 5
class MigrateCiEmails < ActiveRecord::Migration
  include Gitlab::Database

  def up
6
    # This inserts a new service: BuildsEmailService
7
    # It "manually" constructs the properties (JSON-encoded)
8
    # Migrating all ci_projects e-mail related columns
9 10
    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) ' \
11 12 13 14 15 16
      "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')}, " \
      "'\"}') " \
17 18 19 20 21 22
      '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
23 24 25

  def down
  end
26 27 28

  # This function escapes double-quotes and slash
  def escape_text(name)
29 30 31 32 33
    if Gitlab::Database.postgresql?
      "REPLACE(REPLACE(#{name}, '\\', '\\\\'), '\"', '\\\"')"
    else
      "REPLACE(REPLACE(#{name}, '\\\\', '\\\\\\\\'), '\\\"', '\\\\\\\"')"
    end
34 35 36 37
  end

  # This function returns 0 or 1 for column
  def convert_bool(name)
38
    if Gitlab::Database.postgresql?
39
      # PostgreSQL uses BOOLEAN type
40
      "CASE WHEN #{name} IS TRUE THEN '1' ELSE '0' END"
41 42 43 44 45
    else
      # MySQL uses TINYINT
      "#{name}"
    end
  end
46
end