Module: PgEasyReplicate::Helper

Included in:
PgEasyReplicate, Group, IndexManager, Orchestrate, Query, Stats
Defined in:
lib/pg_easy_replicate/helper.rb

Instance Method Summary collapse

Instance Method Details

#abort_with(msg) ⇒ Object



71
72
73
74
# File 'lib/pg_easy_replicate/helper.rb', line 71

def abort_with(msg)
  raise(msg) if test_env?
  abort(msg)
end

#connection_info(conn_string) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/pg_easy_replicate/helper.rb', line 54

def connection_info(conn_string)
  PG::Connection
    .conninfo_parse(conn_string)
    .each_with_object({}) do |obj, hash|
      hash[obj[:keyword].to_sym] = obj[:val]
    end
    .compact
end

#db_name(url) ⇒ Object



67
68
69
# File 'lib/pg_easy_replicate/helper.rb', line 67

def db_name(url)
  connection_info(url)[:dbname]
end

#db_user(url) ⇒ Object



63
64
65
# File 'lib/pg_easy_replicate/helper.rb', line 63

def db_user(url)
  connection_info(url)[:user]
end

#determine_tables(conn_string:, list: "", schema: nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/pg_easy_replicate/helper.rb', line 76

def determine_tables(conn_string:, list: "", schema: nil)
  schema ||= "public"

  tables = list&.split(",") || []
  if tables.size > 0
    tables
  else
    list_all_tables(schema: schema, conn_string: conn_string) - %w[ spatial_ref_sys ]
  end
end

#internal_schema_nameObject



21
22
23
# File 'lib/pg_easy_replicate/helper.rb', line 21

def internal_schema_name
  "pger"
end

#internal_user_nameObject



25
26
27
# File 'lib/pg_easy_replicate/helper.rb', line 25

def internal_user_name
  "pger_su_h1a4fb"
end

#list_all_tables(schema:, conn_string:) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/pg_easy_replicate/helper.rb', line 87

def list_all_tables(schema:, conn_string:)
  Query
    .run(
      query:
        "SELECT c.relname::information_schema.sql_identifier AS table_name
         FROM pg_namespace n
           JOIN pg_class c ON n.oid = c.relnamespace
         WHERE c.relkind = 'r'
           AND c.relpersistence = 'p'
           AND n.nspname::information_schema.sql_identifier = '#{schema}'
         ORDER BY table_name",
      connection_url: conn_string,
      user: db_user(conn_string),
    )
    .map(&:values)
    .flatten
end

#loggerObject



17
18
19
# File 'lib/pg_easy_replicate/helper.rb', line 17

def logger
  PgEasyReplicate.logger
end

#publication_name(group_name) ⇒ Object



29
30
31
# File 'lib/pg_easy_replicate/helper.rb', line 29

def publication_name(group_name)
  "pger_publication_#{underscore(group_name)}"
end

#quote_ident(sql_ident) ⇒ Object



46
47
48
# File 'lib/pg_easy_replicate/helper.rb', line 46

def quote_ident(sql_ident)
  PG::Connection.quote_ident(sql_ident)
end

#secondary_source_db_urlObject



9
10
11
# File 'lib/pg_easy_replicate/helper.rb', line 9

def secondary_source_db_url
  ENV.fetch("SECONDARY_SOURCE_DB_URL", nil)
end

#source_db_urlObject



5
6
7
# File 'lib/pg_easy_replicate/helper.rb', line 5

def source_db_url
  ENV.fetch("SOURCE_DB_URL", nil)
end

#subscription_name(group_name) ⇒ Object



33
34
35
# File 'lib/pg_easy_replicate/helper.rb', line 33

def subscription_name(group_name)
  "pger_subscription_#{underscore(group_name)}"
end

#target_db_urlObject



13
14
15
# File 'lib/pg_easy_replicate/helper.rb', line 13

def target_db_url
  ENV.fetch("TARGET_DB_URL", nil)
end

#test_env?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/pg_easy_replicate/helper.rb', line 50

def test_env?
  ENV.fetch("RACK_ENV", nil) == "test"
end

#underscore(str) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/pg_easy_replicate/helper.rb', line 37

def underscore(str)
  str
    .gsub("::", "/")
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr("-", "_")
    .downcase
end