Module: Switchman::TestHelper

Defined in:
lib/switchman/test_helper.rb

Class Method Summary collapse

Class Method Details

.recreate_persistent_test_shards(dont_create: false) ⇒ Object



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
58
59
60
# File 'lib/switchman/test_helper.rb', line 6

def recreate_persistent_test_shards(dont_create: false)
  # recreate the default shard (it got buhleted)
  ::GuardRail.activate(:deploy) { Switchman.cache.clear }
  if Shard.default(reload: true).is_a?(DefaultShard)
    begin
      Shard.create!(default: true)
    rescue
      raise unless dont_create

      # database doesn't exist yet, presumably cause we're creating it right now
      return [nil, nil]
    end
    Shard.default(reload: true)
  end

  server1 = Shard.default.database_server
  server2 = DatabaseServer.create(Shard.default.database_server.config.merge(server2: true, schema_dump: false))

  if server1 == Shard.default.database_server && server1.config[:shard1] && server1.config[:shard2]
    # look for the shards in the db already
    shard1 = find_existing_test_shard(server1, server1.config[:shard1])
    shard2 = find_existing_test_shard(server2, server1.config[:shard2])

    shard1 ||= server1.shards.build
    shard1.name = server1.config[:shard1]
    shard1.save! if shard1.changed?
    shard2 ||= server2.shards.build
    shard2.name = server1.config[:shard2]
    shard2.save! if shard2.changed?

    recreate_shards = begin
      shard1.activate { ::ActiveRecord::Base.connection.tables == [] }
    rescue ::ActiveRecord::StatementInvalid # e.g. `ERROR:  invalid value for parameter "search_path"`
      true
    end

    if recreate_shards
      if dont_create
        shard1.destroy
        shard2.destroy
        return [nil, nil]
      end

      shard1.drop_database rescue nil
      shard1.destroy
      shard2.drop_database rescue nil
      shard2.destroy
      shard1 = server1.create_new_shard(name: server1.config[:shard1])
      shard2 = server2.create_new_shard(name: server1.config[:shard2])
    end
    [shard1, shard2]
  else
    [server1, server2]
  end
end