summaryrefslogtreecommitdiff
path: root/db/post_migrate/20210609125005_drop_non_partitioned_web_hook_logs.rb
blob: 596b643e079e4449e3e4039c592eab6821e33c56 (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
# frozen_string_literal: true

class DropNonPartitionedWebHookLogs < ActiveRecord::Migration[6.1]
  include Gitlab::Database::MigrationHelpers
  include Gitlab::Database::PartitioningMigrationHelpers::TableManagementHelpers

  DOWNTIME = false

  def up
    drop_nonpartitioned_archive_table(:web_hook_logs)
  end

  def down
    execute(<<~SQL)
      CREATE TABLE web_hook_logs_archived (
          id integer NOT NULL,
          web_hook_id integer NOT NULL,
          trigger character varying,
          url character varying,
          request_headers text,
          request_data text,
          response_headers text,
          response_body text,
          response_status character varying,
          execution_duration double precision,
          internal_error_message character varying,
          created_at timestamp without time zone NOT NULL,
          updated_at timestamp without time zone NOT NULL
      );

      ALTER TABLE ONLY web_hook_logs_archived ADD CONSTRAINT web_hook_logs_archived_pkey PRIMARY KEY (id);

      CREATE INDEX index_web_hook_logs_on_created_at_and_web_hook_id ON web_hook_logs_archived USING btree (created_at, web_hook_id);
      CREATE INDEX index_web_hook_logs_on_web_hook_id ON web_hook_logs_archived USING btree (web_hook_id);

      ALTER TABLE ONLY web_hook_logs_archived ADD CONSTRAINT fk_rails_666826e111 FOREIGN KEY (web_hook_id) REFERENCES web_hooks(id) ON DELETE CASCADE;
    SQL

    with_lock_retries do
      create_trigger_to_sync_tables(:web_hook_logs, :web_hook_logs_archived, 'id')
    end
  end
end