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
- SCHEMA_FILE_LOCATION =
"/tmp/pger_schema.sql"
- VERSION =
"0.1.12"
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, quote_ident, secondary_source_db_url, source_db_url, subscription_name, target_db_url, test_env?, underscore
Class Method Details
.assert_config(special_user_role: nil, copy_schema: false) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/pg_easy_replicate.rb', line 64
def assert_config(special_user_role: nil, copy_schema: false)
config_hash =
config(special_user_role: special_user_role, copy_schema: copy_schema)
if copy_schema && !config_hash.dig(:pg_dump_exists)
abort_with("pg_dump must exist if copy_schema (-c) is passed")
end
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
|
.assert_wal_level_logical(db_config) ⇒ Object
226
227
228
229
230
|
# File 'lib/pg_easy_replicate.rb', line 226
def assert_wal_level_logical(db_config)
db_config&.find do |r|
r.dig(:name) == "wal_level" && r.dig(:setting) == "logical"
end
end
|
.bootstrap(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
|
# File 'lib/pg_easy_replicate.rb', line 88
def bootstrap(options)
logger.info("Setting up schema")
setup_internal_schema
if options[:copy_schema]
logger.info("Setting up schema on targer database")
copy_schema(
source_conn_string: source_db_url,
target_conn_string: target_db_url,
)
end
logger.info("Setting up replication user on source database")
create_user(
conn_string: source_db_url,
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,
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/pg_easy_replicate.rb', line 119
def cleanup(options)
logger.info("Dropping groups table")
Group.drop
if options[:everything]
logger.info("Dropping schema")
drop_internal_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]
logger.info("Dropping replication user on source database")
drop_user(conn_string: source_db_url)
logger.info("Dropping replication user on target database")
drop_user(conn_string: target_db_url)
end
rescue => e
abort_with("Unable to cleanup: #{e.message}")
end
|
.config(special_user_role: nil, copy_schema: false) ⇒ Object
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
55
56
57
58
59
60
61
62
|
# File 'lib/pg_easy_replicate.rb', line 28
def config(special_user_role: nil, copy_schema: false)
abort_with("SOURCE_DB_URL is missing") if source_db_url.nil?
abort_with("TARGET_DB_URL is missing") if target_db_url.nil?
system("which pg_dump")
pg_dump_exists = $CHILD_STATUS.success?
@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),
),
pg_dump_exists: pg_dump_exists,
}
rescue => e
abort_with("Unable to check config: #{e.message}")
end
end
|
.copy_schema(source_conn_string:, target_conn_string:) ⇒ Object
192
193
194
195
|
# File 'lib/pg_easy_replicate.rb', line 192
def copy_schema(source_conn_string:, target_conn_string:)
export_schema(conn_string: source_conn_string)
import_schema(conn_string: target_conn_string)
end
|
.create_user(conn_string:, special_user_role: nil, grant_permissions_on_schema: false) ⇒ Object
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# File 'lib/pg_easy_replicate.rb', line 262
def create_user(
conn_string:,
special_user_role: nil,
grant_permissions_on_schema: false
)
password = connection_info(conn_string)[:password].gsub("'") { "''" }
drop_user(conn_string: conn_string)
sql = " create role \#{quote_ident(internal_user_name)} with password '\#{password}' login createdb createrole;\n grant all privileges on database \#{quote_ident(db_name(conn_string))} TO \#{quote_ident(internal_user_name)};\n SQL\n\n Query.run(\n query: sql,\n connection_url: conn_string,\n user: db_user(conn_string),\n transaction: false,\n )\n\n sql =\n if special_user_role\n \"grant \#{quote_ident(special_user_role)} to \#{quote_ident(internal_user_name)};\"\n else\n \"alter user \#{quote_ident(internal_user_name)} with superuser;\"\n end\n\n Query.run(\n query: sql,\n connection_url: conn_string,\n user: db_user(conn_string),\n transaction: false,\n )\n\n return unless grant_permissions_on_schema\n Query.run(\n query:\n \"grant all on schema \#{quote_ident(internal_schema_name)} to \#{quote_ident(internal_user_name)}\",\n connection_url: conn_string,\n user: db_user(conn_string),\n transaction: false,\n )\nrescue => e\n raise \"Unable to create user: \#{e.message}\"\nend\n"
|
.drop_internal_schema ⇒ Object
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/pg_easy_replicate.rb', line 152
def drop_internal_schema
Query.run(
query:
"DROP SCHEMA IF EXISTS #{quote_ident(internal_schema_name)} CASCADE",
connection_url: source_db_url,
schema: internal_schema_name,
user: db_user(source_db_url),
)
rescue => e
raise "Unable to drop schema: #{e.message}"
end
|
.drop_user(conn_string:, user: internal_user_name) ⇒ Object
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
# File 'lib/pg_easy_replicate.rb', line 309
def drop_user(conn_string:, user: internal_user_name)
return unless user_exists?(conn_string: conn_string, user: user)
sql = " revoke all privileges on database \#{quote_ident(db_name(conn_string))} from \#{quote_ident(user)};\n SQL\n\n Query.run(\n query: sql,\n connection_url: conn_string,\n user: db_user(conn_string),\n )\n\n sql = <<~SQL\n drop role if exists \#{quote_ident(user)};\n SQL\n\n Query.run(\n query: sql,\n connection_url: conn_string,\n user: db_user(conn_string),\n )\nrescue => e\n raise \"Unable to drop user: \#{e.message}\"\nend\n"
|
.export_schema(conn_string:) ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# File 'lib/pg_easy_replicate.rb', line 197
def export_schema(conn_string:)
logger.info("Exporting schema to #{SCHEMA_FILE_LOCATION}")
_, stderr, status =
Open3.capture3(
"pg_dump",
conn_string,
"-f",
SCHEMA_FILE_LOCATION,
"--schema-only",
)
success = status.success?
raise stderr unless success
rescue => e
raise "Unable to export schema: #{e.message}"
end
|
.import_schema(conn_string:) ⇒ Object
214
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/pg_easy_replicate.rb', line 214
def import_schema(conn_string:)
logger.info("Importing schema from #{SCHEMA_FILE_LOCATION}")
_, stderr, status =
Open3.capture3("psql", "-f", SCHEMA_FILE_LOCATION, conn_string)
success = status.success?
raise stderr unless success
rescue => e
raise "Unable to import schema: #{e.message}"
end
|
.is_super_user?(url, special_user_role = nil) ⇒ Boolean
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
# File 'lib/pg_easy_replicate.rb', line 232
def is_super_user?(url, special_user_role = nil)
if special_user_role
sql = " SELECT r.rolname AS username,\n r1.rolname AS \"role\"\n FROM pg_catalog.pg_roles r\n LEFT JOIN pg_catalog.pg_auth_members m ON (m.member = r.oid)\n LEFT JOIN pg_roles r1 ON (m.roleid=r1.oid)\n WHERE r.rolname = '\#{db_user(url)}'\n ORDER BY 1;\n SQL\n\n r = Query.run(query: sql, connection_url: url, user: db_user(url))\n # If special_user_role is passed just ensure the url in conn_string has been granted\n # the special_user_role\n r.any? { |q| q[:role] == special_user_role }\n else\n r =\n Query.run(\n query:\n \"SELECT rolname, rolsuper FROM pg_roles where rolname = '\#{db_user(url)}';\",\n connection_url: url,\n user: db_user(url),\n )\n r.any? { |q| q[:rolsuper] }\n end\nrescue => e\n raise \"Unable to check superuser conditions: \#{e.message}\"\nend\n"
|
.logger ⇒ Object
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/pg_easy_replicate.rb', line 181
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_internal_schema ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/pg_easy_replicate.rb', line 164
def setup_internal_schema
sql = " create schema if not exists \#{quote_ident(internal_schema_name)};\n grant usage on schema \#{quote_ident(internal_schema_name)} to \#{quote_ident(db_user(source_db_url))};\n grant create on schema \#{quote_ident(internal_schema_name)} to \#{quote_ident(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(source_db_url),\n )\nrescue => e\n raise \"Unable to setup schema: \#{e.message}\"\nend\n"
|
.user_exists?(conn_string:, user: internal_user_name) ⇒ Boolean
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
# File 'lib/pg_easy_replicate.rb', line 335
def user_exists?(conn_string:, user: internal_user_name)
sql = " SELECT r.rolname AS username,\n r1.rolname AS \"role\"\n FROM pg_catalog.pg_roles r\n LEFT JOIN pg_catalog.pg_auth_members m ON (m.member = r.oid)\n LEFT JOIN pg_roles r1 ON (m.roleid=r1.oid)\n WHERE r.rolname = '\#{user}'\n ORDER BY 1;\n SQL\n\n Query\n .run(\n query: sql,\n connection_url: conn_string,\n user: db_user(conn_string),\n )\n .any? { |q| q[:username] == user }\nend\n"
|