summaryrefslogtreecommitdiff
path: root/db/post_migrate/20180223124427_build_user_interacted_projects_table.rb
blob: 5e729b1aa53cb84764efa2d3e4ba01dc0082b1a4 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class BuildUserInteractedProjectsTable < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  # Set this constant to true if this migration requires downtime.
  DOWNTIME = false

  disable_ddl_transaction!

  def up
    if Gitlab::Database.postgresql?
      PostgresStrategy.new
    else
      MysqlStrategy.new
    end.up

    unless index_exists?(:user_interacted_projects, [:project_id, :user_id])
      add_concurrent_index :user_interacted_projects, [:project_id, :user_id], unique: true
    end

    unless foreign_key_exists?(:user_interacted_projects, :user_id)
      add_concurrent_foreign_key :user_interacted_projects, :users, column: :user_id, on_delete: :cascade
    end

    unless foreign_key_exists?(:user_interacted_projects, :project_id)
      add_concurrent_foreign_key :user_interacted_projects, :projects, column: :project_id, on_delete: :cascade
    end
  end

  def down
    execute "TRUNCATE user_interacted_projects"

    if foreign_key_exists?(:user_interacted_projects, :user_id)
      remove_foreign_key :user_interacted_projects, :users
    end

    if foreign_key_exists?(:user_interacted_projects, :project_id)
      remove_foreign_key :user_interacted_projects, :projects
    end

    if index_exists_by_name?(:user_interacted_projects, 'index_user_interacted_projects_on_project_id_and_user_id')
      remove_concurrent_index_by_name :user_interacted_projects, 'index_user_interacted_projects_on_project_id_and_user_id'
    end
  end

  private

  # Rails' index_exists? doesn't work when you only give it a table and index
  # name. As such we have to use some extra code to check if an index exists for
  # a given name.
  def index_exists_by_name?(table, index)
    indexes_for_table[table].include?(index)
  end

  def indexes_for_table
    @indexes_for_table ||= Hash.new do |hash, table_name|
      hash[table_name] = indexes(table_name).map(&:name)
    end
  end

  def foreign_key_exists?(table, column)
    foreign_keys(table).any? do |key|
      key.options[:column] == column.to_s
    end
  end

  class PostgresStrategy < ActiveRecord::Migration
    include Gitlab::Database::MigrationHelpers

    BATCH_SIZE = 100_000
    SLEEP_TIME = 5

    def up
      with_index(:events, [:author_id, :project_id], name: 'events_user_interactions_temp', where: 'project_id IS NOT NULL') do
        iteration = 0
        records = 0
        begin
          Rails.logger.info "Building user_interacted_projects table, batch ##{iteration}"
          result = execute <<~SQL
            INSERT INTO user_interacted_projects (user_id, project_id)
            SELECT e.user_id, e.project_id
            FROM (SELECT DISTINCT author_id AS user_id, project_id FROM events WHERE project_id IS NOT NULL) AS e
            LEFT JOIN user_interacted_projects ucp USING (user_id, project_id)
            WHERE ucp.user_id IS NULL
            LIMIT #{BATCH_SIZE}
          SQL
          iteration += 1
          records += result.cmd_tuples
          Rails.logger.info "Building user_interacted_projects table, batch ##{iteration} complete, created #{records} overall"
          Kernel.sleep(SLEEP_TIME) if result.cmd_tuples > 0
        rescue ActiveRecord::InvalidForeignKey => e
          Rails.logger.info "Retry on InvalidForeignKey: #{e}"
          retry
        end while result.cmd_tuples > 0
      end

      execute "ANALYZE user_interacted_projects"

    end

    private

    def with_index(*args)
      add_concurrent_index(*args) unless index_exists?(*args)
      yield
    ensure
      remove_concurrent_index(*args) if index_exists?(*args)
    end
  end

  class MysqlStrategy < ActiveRecord::Migration
    include Gitlab::Database::MigrationHelpers

    def up
      execute <<~SQL
        INSERT INTO user_interacted_projects (user_id, project_id)
        SELECT e.user_id, e.project_id
        FROM (SELECT DISTINCT author_id AS user_id, project_id FROM events WHERE project_id IS NOT NULL) AS e
        LEFT JOIN user_interacted_projects ucp USING (user_id, project_id)
        WHERE ucp.user_id IS NULL
      SQL
    end
  end

end