BigW Consortium Gitlab

20151210125232_migrate_ci_slack_service.rb 1.47 KB
Newer Older
1
# rubocop:disable all
2 3 4 5 6 7
class MigrateCiSlackService < ActiveRecord::Migration
  include Gitlab::Database

  def up
    properties_query = 'SELECT properties FROM ci_services ' \
      'JOIN ci_projects ON ci_services.project_id=ci_projects.id ' \
8 9
      "WHERE ci_projects.gitlab_id=services.project_id AND ci_services.type='Ci::SlackService' AND ci_services.active " \
      'LIMIT 1'
10 11 12

    active_query = 'SELECT 1 FROM ci_services ' \
      'JOIN ci_projects ON ci_services.project_id=ci_projects.id ' \
13 14
      "WHERE ci_projects.gitlab_id=services.project_id AND ci_services.type='Ci::SlackService' AND ci_services.active " \
      'LIMIT 1'
15 16 17 18

    # We update the service since services are always generated for project, even if they are inactive
    # Activate service and migrate properties if currently the service is not active
    execute(
19 20 21
      "UPDATE services SET properties=(#{properties_query}), active=#{true_value}, " \
      "push_events=#{false_value}, issues_events=#{false_value}, merge_requests_events=#{false_value}, " \
      "tag_push_events=#{false_value}, note_events=#{false_value}, build_events=#{true_value} " \
22 23 24 25 26 27 28 29 30 31 32 33 34
      "WHERE NOT services.active AND services.type='SlackService' AND (#{active_query}) IS NOT NULL"
    )

    # Tick only build_events if the service is already active
    execute(
      "UPDATE services SET build_events=#{true_value} " \
      "WHERE services.active AND services.type='SlackService' AND (#{active_query}) IS NOT NULL"
    )
  end

  def down
  end
end