summaryrefslogtreecommitdiff
path: root/db/migrate/20200123090839_remove_analytics_repository_table_fks_on_projects.rb
blob: 18fd349df2ab611383d7531257120ec959db2c92 (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
# frozen_string_literal: true

class RemoveAnalyticsRepositoryTableFksOnProjects < ActiveRecord::Migration[5.2]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    # Requires ExclusiveLock on all tables. analytics_* tables are empty
    with_lock_retries do
      remove_foreign_key_if_exists(:analytics_repository_files, :projects)
    end

    with_lock_retries do
      remove_foreign_key_if_exists(:analytics_repository_file_edits, :projects) if table_exists?(:analytics_repository_file_edits) # this table might be already dropped on development environment
    end

    with_lock_retries do
      remove_foreign_key_if_exists(:analytics_repository_file_commits, :projects)
    end
  end

  def down
    add_concurrent_foreign_key(:analytics_repository_files, :projects, column: :project_id, on_delete: :cascade)
    add_concurrent_foreign_key(:analytics_repository_file_edits, :projects, column: :project_id, on_delete: :cascade)
    add_concurrent_foreign_key(:analytics_repository_file_commits, :projects, column: :project_id, on_delete: :cascade)
  end
end