Module: PgEventstore::TestHelpers

Defined in:
lib/pg_eventstore/rspec/test_helpers.rb

Class Method Summary collapse

Class Method Details

.clean_up_data(config) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pg_eventstore/rspec/test_helpers.rb', line 21

def clean_up_data(config)
  tables_to_purge = PgEventstore.connection(config).with do |conn|
    conn.exec("      SELECT tablename\n      FROM pg_catalog.pg_tables\n      WHERE schemaname NOT IN ('pg_catalog', 'information_schema') AND tablename != 'migrations'\n    SQL\n  end\n  tables_to_purge = tables_to_purge.map { |attrs| attrs['tablename'] }\n  tables_to_purge.each do |table_name|\n    PgEventstore.connection(config).with { |c| c.exec(\"DELETE FROM \#{table_name}\") }\n  end\nend\n")

.clean_up_db(config = :default) ⇒ Object



6
7
8
9
10
# File 'lib/pg_eventstore/rspec/test_helpers.rb', line 6

def clean_up_db(config = :default)
  clean_up_data(config)
  clean_up_partitions(config)
  clean_up_subscription_functions(config)
end

.clean_up_partitions(config) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/pg_eventstore/rspec/test_helpers.rb', line 12

def clean_up_partitions(config)
  PgEventstore.connection(config).with do |conn|
    # Dropping parent partition also drops all child partitions
    conn.exec("select tablename from pg_tables where tablename like 'contexts_%'").each do |attrs|
      conn.exec("drop table #{attrs['tablename']}")
    end
  end
end

.clean_up_subscription_functions(config) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pg_eventstore/rspec/test_helpers.rb', line 35

def clean_up_subscription_functions(config)
  functions_to_purge = PgEventstore.connection(config).with do |conn|
    conn.exec("      SELECT proname FROM pg_proc WHERE proname LIKE 'subscription_%'\n    SQL\n  end\n  functions_to_purge = functions_to_purge.map { _1['proname'] }\n  functions_to_purge.each do |function_name|\n    PgEventstore.connection(config).with { |c| c.exec(\"drop function \#{function_name}\") }\n  end\nend\n")