Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20180305095250_create_internal_ids_table.rb « migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d2e79a12c0a7bcdd4bbc16f7ffb6238a00f45363 (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
class CreateInternalIdsTable < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    create_table :internal_ids, id: :bigserial do |t|
      t.references :project
      t.integer :usage, null: false
      t.integer :last_value, null: false
    end

    unless index_exists?(:internal_ids, [:usage, :project_id])
      add_index :internal_ids, [:usage, :project_id], unique: true
    end

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

  def down
    drop_table :internal_ids
  end

  private

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