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

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

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    pool = create_connection_pool
    threads = []

    threads << Thread.new do
      pool.with_connection do |connection|
        connection.execute <<-SQL.strip_heredoc
        DELETE FROM services
          WHERE type = 'BuildsEmailService'
            AND active IS FALSE
            AND properties = '{"notify_only_broken_builds":true}';
        SQL
      end
    end

    threads << Thread.new do
      pool.with_connection do |connection|
        connection.execute <<-SQL.strip_heredoc
        DELETE FROM services
          WHERE type = 'PipelinesEmailService'
            AND active IS FALSE
            AND properties = '{"notify_only_broken_pipelines":true}';
        SQL
      end
    end

    threads.each(&:join)
  end

  def down
    # Nothing can be done to restore the records
  end

  private

  def create_connection_pool
    # See activerecord-4.2.7.1/lib/active_record/connection_adapters/connection_specification.rb
    env = Rails.env
    original_config = ActiveRecord::Base.configurations
    env_config = original_config[env].merge('pool' => 2)
    config = original_config.merge(env => env_config)

    spec =
      ActiveRecord::
        ConnectionAdapters::
        ConnectionSpecification::Resolver.new(config).spec(env.to_sym)

    ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
  end
end