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.2"

Class Method Summary collapse

Methods included from Helper

abort_with, connection_info, 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_configObject



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

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

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

  unless config.dig(:source_db_is_superuser)
    abort_with("User on source database should be a superuser")
  end

  return if config.dig(:target_db_is_superuser)
  abort_with("User on target database should be a superuser")
end

.bootstrap(options) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pg_easy_replicate.rb', line 71

def bootstrap(options)
  assert_config
  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])

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

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

.cleanup(options) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pg_easy_replicate.rb', line 88

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

.configObject



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

def config
  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_superuser: is_super_user?(source_db_url),
        target_db_is_superuser: is_super_user?(target_db_url),
        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



121
122
123
124
125
126
127
# File 'lib/pg_easy_replicate.rb', line 121

def drop_schema
  Query.run(
    query: "DROP SCHEMA IF EXISTS #{internal_schema_name} CASCADE",
    connection_url: source_db_url,
    schema: internal_schema_name,
  )
end

.loggerObject



144
145
146
147
148
149
150
151
152
153
# File 'lib/pg_easy_replicate.rb', line 144

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



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

def setup_schema
  sql = <<~SQL
    create schema if not exists #{internal_schema_name};
    grant usage on schema #{internal_schema_name} to #{db_user(source_db_url)};
    grant create on schema #{internal_schema_name} to #{db_user(source_db_url)};
  SQL

  Query.run(
    query: sql,
    connection_url: source_db_url,
    schema: internal_schema_name,
    user: db_user(target_db_url),
  )
end