Module: PgEasyReplicate

Extended by:
Helper
Defined in:
lib/pg_easy_replicate.rb,
lib/pg_easy_replicate/cli.rb,
lib/pg_easy_replicate/group.rb,
lib/pg_easy_replicate/query.rb,
lib/pg_easy_replicate/stats.rb,
lib/pg_easy_replicate/helper.rb,
lib/pg_easy_replicate/version.rb,
lib/pg_easy_replicate/orchestrate.rb

Defined Under Namespace

Modules: Helper Classes: CLI, Error, Group, Orchestrate, Query, Stats

Constant Summary collapse

VERSION =
"0.1.5"

Class Method Summary collapse

Methods included from Helper

abort_with, connection_info, db_name, db_user, internal_schema_name, internal_user_name, logger, publication_name, secondary_source_db_url, source_db_url, subscription_name, target_db_url, test_env?, underscore

Class Method Details

.assert_config(special_user_role: nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pg_easy_replicate.rb', line 56

def assert_config(special_user_role: nil)
  config_hash = config(special_user_role: special_user_role)

  unless assert_wal_level_logical(config_hash.dig(:source_db))
    abort_with("WAL_LEVEL should be LOGICAL on source DB")
  end

  unless assert_wal_level_logical(config_hash.dig(:target_db))
    abort_with("WAL_LEVEL should be LOGICAL on target DB")
  end

  unless config_hash.dig(:source_db_is_super_user)
    abort_with("User on source database does not have super user privilege")
  end

  return if config_hash.dig(:target_db_is_super_user)
  abort_with("User on target database does not have super user privilege")
end

.bootstrap(options) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pg_easy_replicate.rb', line 75

def bootstrap(options)
  logger.info("Setting up schema")
  setup_schema

  logger.info("Setting up replication user on source database")
  create_user(
    conn_string: source_db_url,
    group_name: options[:group_name],
    special_user_role: options[:special_user_role],
    grant_permissions_on_schema: true,
  )

  logger.info("Setting up replication user on target database")
  create_user(
    conn_string: target_db_url,
    group_name: options[:group_name],
    special_user_role: options[:special_user_role],
  )

  logger.info("Setting up groups tables")
  Group.setup
rescue => e
  abort_with("Unable to bootstrap: #{e.message}")
end

.cleanup(options) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/pg_easy_replicate.rb', line 100

def cleanup(options)
  logger.info("Dropping groups table")
  Group.drop

  if options[:everything]
    logger.info("Dropping schema")
    drop_schema
  end

  if options[:everything] || options[:sync]
    Orchestrate.drop_publication(
      group_name: options[:group_name],
      conn_string: source_db_url,
    )

    Orchestrate.drop_subscription(
      group_name: options[:group_name],
      target_conn_string: target_db_url,
    )
  end

  if options[:everything]
    # Drop users at last
    logger.info("Dropping replication user on source database")
    drop_user(conn_string: source_db_url, group_name: options[:group_name])

    logger.info("Dropping replication user on target database")
    drop_user(conn_string: target_db_url, group_name: options[:group_name])
  end
rescue => e
  abort_with("Unable to cleanup: #{e.message}")
end

.config(special_user_role: nil) ⇒ Object



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
# File 'lib/pg_easy_replicate.rb', line 24

def config(special_user_role: nil)
  abort_with("SOURCE_DB_URL is missing") if source_db_url.nil?
  abort_with("TARGET_DB_URL is missing") if target_db_url.nil?

  @config ||=
    begin
      q =
        "select name, setting from pg_settings where name in  ('max_wal_senders', 'max_worker_processes', 'wal_level',  'max_replication_slots', 'max_logical_replication_workers');"

      {
        source_db_is_super_user:
          is_super_user?(source_db_url, special_user_role),
        target_db_is_super_user:
          is_super_user?(target_db_url, special_user_role),
        source_db:
          Query.run(
            query: q,
            connection_url: source_db_url,
            user: db_user(source_db_url),
          ),
        target_db:
          Query.run(
            query: q,
            connection_url: target_db_url,
            user: db_user(target_db_url),
          ),
      }
    rescue => e
      abort_with("Unable to check config: #{e.message}")
    end
end

.drop_schemaObject



133
134
135
136
137
138
139
140
141
142
# File 'lib/pg_easy_replicate.rb', line 133

def drop_schema
  Query.run(
    query: "DROP SCHEMA IF EXISTS #{internal_schema_name} CASCADE",
    connection_url: source_db_url,
    schema: internal_schema_name,
    user: db_user(target_db_url),
  )
rescue => e
  raise "Unable to drop schema: #{e.message}"
end

.loggerObject



161
162
163
164
165
166
167
168
169
170
# File 'lib/pg_easy_replicate.rb', line 161

def logger
  @logger ||=
    begin
      logger = Ougai::Logger.new($stdout)
      logger.level =
        ENV["DEBUG"] ? Ougai::Logger::TRACE : Ougai::Logger::INFO
      logger.with_fields = { version: PgEasyReplicate::VERSION }
      logger
    end
end

.setup_schemaObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/pg_easy_replicate.rb', line 144

def setup_schema
  sql = "    create schema if not exists \#{internal_schema_name};\n    grant usage on schema \#{internal_schema_name} to \#{db_user(source_db_url)};\n    grant create on schema \#{internal_schema_name} to \#{db_user(source_db_url)};\n  SQL\n\n  Query.run(\n    query: sql,\n    connection_url: source_db_url,\n    schema: internal_schema_name,\n    user: db_user(target_db_url),\n  )\nrescue => e\n  raise \"Unable to setup schema: \#{e.message}\"\nend\n"