Module: PgEasyReplicate::Helper
- Included in:
- PgEasyReplicate, DDLAudit, DDLManager, Group, IndexManager, Orchestrate, Query, Stats
- Defined in:
- lib/pg_easy_replicate/helper.rb
Instance Method Summary collapse
- #abort_with(msg) ⇒ Object
- #connection_info(conn_string) ⇒ Object
- #convert_to_array(input) ⇒ Object
- #db_name(url) ⇒ Object
- #db_user(url) ⇒ Object
- #determine_tables(conn_string:, list: "", exclude_list: "", schema: nil) ⇒ Object
- #internal_schema_name ⇒ Object
- #internal_user_name ⇒ Object
- #list_all_tables(schema:, conn_string:) ⇒ Object
- #logger ⇒ Object
- #publication_name(group_name) ⇒ Object
- #quote_ident(sql_ident) ⇒ Object
- #restore_connections_on_source_db ⇒ Object
- #secondary_source_db_url ⇒ Object
- #source_db_url ⇒ Object
- #subscription_name(group_name) ⇒ Object
- #target_db_url ⇒ Object
- #test_env? ⇒ Boolean
- #underscore(str) ⇒ Object
- #validate_table_lists(tables, exclude_tables, schema_name) ⇒ Object
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 |
#convert_to_array(input) ⇒ Object
109 110 111 |
# File 'lib/pg_easy_replicate/helper.rb', line 109 def convert_to_array(input) input.is_a?(Array) ? input : input&.split(",") || [] 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: "", exclude_list: "", schema: nil) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/pg_easy_replicate/helper.rb', line 76 def determine_tables(conn_string:, list: "", exclude_list: "", schema: nil) schema ||= "public" tables = convert_to_array(list) exclude_tables = convert_to_array(exclude_list) validate_table_lists(tables, exclude_tables, schema) if tables.empty? all_tables = list_all_tables(schema: schema, conn_string: conn_string) all_tables - (exclude_tables + %w[spatial_ref_sys]) else tables end end |
#internal_schema_name ⇒ Object
21 22 23 |
# File 'lib/pg_easy_replicate/helper.rb', line 21 def internal_schema_name "pger" end |
#internal_user_name ⇒ Object
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
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/pg_easy_replicate/helper.rb', line 91 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 |
#logger ⇒ Object
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 |
#restore_connections_on_source_db ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/pg_easy_replicate/helper.rb', line 131 def restore_connections_on_source_db logger.info("Restoring connections") alter_sql = "ALTER USER #{quote_ident(db_user(source_db_url))} set default_transaction_read_only = false" Query.run(query: alter_sql, connection_url: source_db_url) end |
#secondary_source_db_url ⇒ Object
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_url ⇒ Object
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_url ⇒ Object
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
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 |
#validate_table_lists(tables, exclude_tables, schema_name) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/pg_easy_replicate/helper.rb', line 113 def validate_table_lists(tables, exclude_tables, schema_name) table_list = convert_to_array(tables) exclude_table_list = convert_to_array(exclude_tables) if !table_list.empty? && !exclude_table_list.empty? abort_with( "Options --tables(-t) and --exclude-tables(-e) cannot be used together.", ) elsif !table_list.empty? if table_list.size > 0 && (schema_name.nil? || schema_name == "") abort_with("Schema name is required if tables are passed") end elsif exclude_table_list.size > 0 && (schema_name.nil? || schema_name == "") abort_with("Schema name is required if exclude tables are passed") end end |