summaryrefslogtreecommitdiff
path: root/db/migrate/20161025231710_migrate_jira_to_gem.rb
blob: 870b00411d2bf0fa11fa0cda43a3972e2ca879ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class MigrateJiraToGem < ActiveRecord::Migration
  DOWNTIME = true

  DOWNTIME_REASON = <<-HEREDOC
    Refactor all Jira services properties(serialized field) to use new jira-ruby gem.
    There were properties on old Jira service that are not needed anymore after the
    service refactoring: api_url, project_url, new_issue_url, issues_url.
    We extract the new necessary some properties from old keys and delete them:
    taking project_key from project_url and url from api_url
  HEREDOC

  def up
    active_services_query = "SELECT id, properties FROM services WHERE services.type IN ('JiraService') AND services.active = true"

    select_all(active_services_query).each do |service|
      id = service['id']
      properties = JSON.parse(service['properties'])
      properties_was = properties.clone

      # Migrate `project_url` to `project_key`
      # Ignore if `project_url` doesn't have jql project query with project key
      if properties['project_url'].present?
        jql = properties['project_url'].match('project=([A-Za-z]*)')
        properties['project_key'] = jql.captures.first if jql
      end

      # Migrate `api_url` to `url`
      if properties['api_url'].present?
        url = properties['api_url'].match('(.*)\/rest\/api')
        properties['url'] = url.captures.first if url
      end

      # Delete now unnecessary properties
      properties.delete('api_url')
      properties.delete('project_url')
      properties.delete('new_issue_url')
      properties.delete('issues_url')

      # Update changes properties
      if properties != properties_was
        execute("UPDATE services SET properties = '#{quote_string(properties.to_json)}' WHERE id = #{id}")
      end
    end
  end

  def down
    active_services_query = "SELECT id, properties FROM services WHERE services.type IN ('JiraService') AND services.active = true"

    select_all(active_services_query).each do |service|
      id = service['id']
      properties = JSON.parse(service['properties'])
      properties_was = properties.clone

      # Rebuild old properties based on sane defaults
      if properties['url'].present?
        properties['api_url'] = "#{properties['url']}/rest/api/2"
        properties['project_url'] =
          "#{properties['url']}/issues/?jql=project=#{properties['project_key']}"
        properties['issues_url'] = "#{properties['url']}/browse/:id"
        properties['new_issue_url'] = "#{properties['url']}/secure/CreateIssue.jspa"
      end

      # Delete the new properties
      properties.delete('url')
      properties.delete('project_key')

      # Update changes properties
      if properties != properties_was
        execute("UPDATE services SET properties = '#{quote_string(properties.to_json)}' WHERE id = #{id}")
      end
    end
  end
end