summaryrefslogtreecommitdiff
path: root/db/migrate/20201208210209_create_incident_management_oncall_shifts.rb
blob: 49cb32ffee060af8c20516b05d498dcd941d41d6 (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
# frozen_string_literal: true

class CreateIncidentManagementOncallShifts < ActiveRecord::Migration[6.0]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  def up
    unless table_exists?(:incident_management_oncall_shifts)
      with_lock_retries do
        create_table :incident_management_oncall_shifts do |t|
          t.references :rotation, null: false, foreign_key: { to_table: :incident_management_oncall_rotations, on_delete: :cascade }
          t.references :participant, null: false, foreign_key: { to_table: :incident_management_oncall_participants, on_delete: :cascade }
          t.datetime_with_timezone :starts_at, null: false
          t.datetime_with_timezone :ends_at, null: false
        end

        execute <<~SQL
          ALTER TABLE incident_management_oncall_shifts
            ADD CONSTRAINT inc_mgmnt_no_overlapping_oncall_shifts
            EXCLUDE USING gist
            ( rotation_id WITH =,
              tstzrange(starts_at, ends_at, '[)') WITH &&
            )
        SQL
      end
    end
  end

  def down
    with_lock_retries do
      drop_table :incident_management_oncall_shifts
    end
  end
end