pg-logical_replication 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +30 -0
- data/.gitignore +9 -0
- data/.rspec +4 -0
- data/.rubocop.yml +3 -0
- data/.rubocop_cc.yml +4 -0
- data/.rubocop_local.yml +0 -0
- data/.travis.yml +21 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +202 -0
- data/README.md +38 -0
- data/Rakefile +35 -0
- data/ci/before_install.sh +7 -0
- data/lib/pg-logical_replication.rb +1 -0
- data/lib/pg/logical_replication.rb +10 -0
- data/lib/pg/logical_replication/client.rb +321 -0
- data/lib/pg/logical_replication/command_builder.rb +62 -0
- data/lib/pg/logical_replication/version.rb +5 -0
- data/pg-logical_replication.gemspec +30 -0
- metadata +136 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 995a68b802609a1681e2329f88b1dc0e1cea9148
|
4
|
+
data.tar.gz: cb8c1d5435ca90c6c50a30d09a4afcc141f07d3a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d2f847732697fd297746cc19fa10a2733de22a020be2b165eacaa258c30cfaa39857ca0a0467860e77ee4f672ea4599b3508aa5883f22c4704ee417371171efb
|
7
|
+
data.tar.gz: 52c714f97cd3ccb81be3a6725fa52a49229b8ef43e96ff21691519fb27f9e41904603d8bba7005df6bb933b945d9230f5136d9c90cc7d1ccfaef29d44e34e25f
|
data/.codeclimate.yml
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
---
|
2
|
+
version: '2'
|
3
|
+
checks:
|
4
|
+
argument-count:
|
5
|
+
enabled: false
|
6
|
+
complex-logic:
|
7
|
+
enabled: false
|
8
|
+
file-lines:
|
9
|
+
enabled: false
|
10
|
+
method-complexity:
|
11
|
+
config:
|
12
|
+
threshold: 11
|
13
|
+
method-count:
|
14
|
+
enabled: false
|
15
|
+
method-lines:
|
16
|
+
enabled: false
|
17
|
+
nested-control-flow:
|
18
|
+
enabled: false
|
19
|
+
return-statements:
|
20
|
+
enabled: false
|
21
|
+
prepare:
|
22
|
+
fetch:
|
23
|
+
- url: https://raw.githubusercontent.com/ManageIQ/guides/master/.rubocop_base.yml
|
24
|
+
path: ".rubocop_base.yml"
|
25
|
+
- url: https://raw.githubusercontent.com/ManageIQ/guides/master/.rubocop_cc_base.yml
|
26
|
+
path: ".rubocop_cc_base.yml"
|
27
|
+
plugins:
|
28
|
+
rubocop:
|
29
|
+
enabled: true
|
30
|
+
config: ".rubocop_cc.yml"
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.rubocop_cc.yml
ADDED
data/.rubocop_local.yml
ADDED
File without changes
|
data/.travis.yml
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
---
|
2
|
+
dist: xenial
|
3
|
+
language: ruby
|
4
|
+
cache: bundler
|
5
|
+
rvm:
|
6
|
+
- 2.4.5
|
7
|
+
- 2.5.3
|
8
|
+
before_install:
|
9
|
+
- 'echo ''gem: --no-ri --no-rdoc --no-document'' > ~/.gemrc'
|
10
|
+
- gem install bundler
|
11
|
+
- source ${TRAVIS_BUILD_DIR}/ci/before_install.sh
|
12
|
+
before_script:
|
13
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64
|
14
|
+
> ./cc-test-reporter
|
15
|
+
- chmod +x ./cc-test-reporter
|
16
|
+
- "./cc-test-reporter before-build"
|
17
|
+
- bundle exec rake spec:setup
|
18
|
+
after_script:
|
19
|
+
- "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
|
20
|
+
addons:
|
21
|
+
postgresql: '10'
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
This project adheres to [Semantic Versioning](https://semver.org/).
|
5
|
+
|
6
|
+
## [1.0.0] - 2019-05-08
|
7
|
+
|
8
|
+
[Unreleased]: https://github.com/ManageIQ/pg-logical_replication/compare/v1.0.0...master
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# PG::LogicalReplication
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/pg-logical_replication.svg)](http://badge.fury.io/rb/pg-logical_replication)
|
4
|
+
[![Build Status](https://travis-ci.org/ManageIQ/pg-logical_replication.svg?branch=master)](https://travis-ci.org/ManageIQ/pg-logical_replication)
|
5
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/9967445630b9e8ff2fe5/maintainability)](https://codeclimate.com/github/ManageIQ/pg-logical_replication/maintainability)
|
6
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/9967445630b9e8ff2fe5/test_coverage)](https://codeclimate.com/github/ManageIQ/pg-logical_replication/test_coverage)
|
7
|
+
[![Security](https://hakiri.io/github/ManageIQ/pg-logical_replication/master.svg)](https://hakiri.io/github/ManageIQ/pg-logical_replication/master)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'pg-logical_replication'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install pg-logical_replication
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec:setup spec` to run the tests. `rake spec:teardown` will remove the databases created for test purposes.
|
28
|
+
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ManageIQ/pg-logical_replication.
|
34
|
+
|
35
|
+
## License
|
36
|
+
|
37
|
+
This project is available as open source under the terms of the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
|
38
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
def create_database(dbname)
|
5
|
+
require "pg"
|
6
|
+
c = PG::Connection.new(:dbname => "postgres")
|
7
|
+
c.async_exec("CREATE DATABASE #{dbname}")
|
8
|
+
rescue PG::DuplicateDatabase => err
|
9
|
+
raise unless err.message =~ /already exists/
|
10
|
+
end
|
11
|
+
|
12
|
+
def drop_database(dbname)
|
13
|
+
require "pg"
|
14
|
+
c = PG::Connection.new(:dbname => "postgres")
|
15
|
+
c.async_exec("DROP DATABASE #{dbname}")
|
16
|
+
rescue PG::InvalidCatalogName => err
|
17
|
+
raise unless err.message =~ /does not exist/
|
18
|
+
end
|
19
|
+
|
20
|
+
namespace :spec do
|
21
|
+
desc "Setup the test databases"
|
22
|
+
task :setup => :teardown do
|
23
|
+
create_database("logical_test")
|
24
|
+
create_database("logical_test_target")
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "Teardown the test databases"
|
28
|
+
task :teardown do
|
29
|
+
drop_database("logical_test")
|
30
|
+
drop_database("logical_test_target")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:spec)
|
35
|
+
task :default => :spec
|
@@ -0,0 +1,7 @@
|
|
1
|
+
set -v
|
2
|
+
|
3
|
+
echo -e "wal_level = 'logical'\nmax_worker_processes = 10\nmax_replication_slots = 10\nmax_wal_senders = 10" | sudo tee -a /etc/postgresql/10/main/postgresql.conf
|
4
|
+
echo -e "local replication all trust" | sudo tee -a /etc/postgresql/10/main/pg_hba.conf
|
5
|
+
sudo service postgresql restart 10
|
6
|
+
|
7
|
+
set +v
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'pg/logical_replication'
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require "pg/logical_replication/version"
|
2
|
+
require "pg/logical_replication/client"
|
3
|
+
require "pg/logical_replication/command_builder"
|
4
|
+
|
5
|
+
module PG
|
6
|
+
module LogicalReplication
|
7
|
+
class Error < StandardError; end
|
8
|
+
class CommandBuilderError < PG::LogicalReplication::Error; end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,321 @@
|
|
1
|
+
require "pg"
|
2
|
+
|
3
|
+
module PG
|
4
|
+
module LogicalReplication
|
5
|
+
class Client
|
6
|
+
attr_reader :connection
|
7
|
+
attr_reader :command_builder
|
8
|
+
|
9
|
+
# @param connection [PG::Connection] Database Connection
|
10
|
+
def initialize(connection)
|
11
|
+
@connection = connection
|
12
|
+
@command_builder = PG::LogicalReplication::CommandBuilder.new(connection)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Reports on replication lag from publisher to subscriber nodes
|
16
|
+
# This method must be run on the publisher node
|
17
|
+
#
|
18
|
+
# @return [Array<Hash<String,String>>] List of returned lag and application names,
|
19
|
+
# one for each replication process
|
20
|
+
def lag_bytes
|
21
|
+
typed_exec(<<-SQL).to_a
|
22
|
+
SELECT
|
23
|
+
pg_wal_lsn_diff(pg_current_wal_insert_lsn(), flush_lsn) AS lag_bytes,
|
24
|
+
application_name
|
25
|
+
FROM
|
26
|
+
pg_stat_replication
|
27
|
+
SQL
|
28
|
+
end
|
29
|
+
|
30
|
+
# Reports on replication bytes of WAL being retained for each replication slot
|
31
|
+
# This method must be run on the publisher node
|
32
|
+
#
|
33
|
+
# @return [Array<Hash<String,String>>] List of returned WAL bytes and replication slot names,
|
34
|
+
# one for each replication process
|
35
|
+
def wal_retained_bytes
|
36
|
+
typed_exec(<<-SQL).to_a
|
37
|
+
SELECT
|
38
|
+
pg_wal_lsn_diff(pg_current_wal_insert_lsn(), restart_lsn) AS retained_bytes,
|
39
|
+
slot_name::TEXT
|
40
|
+
FROM
|
41
|
+
pg_replication_slots
|
42
|
+
WHERE
|
43
|
+
plugin = 'pgoutput'
|
44
|
+
SQL
|
45
|
+
end
|
46
|
+
|
47
|
+
# Creates a subscription to a publisher node
|
48
|
+
#
|
49
|
+
# @param name [String] subscription name
|
50
|
+
# @param conninfo_hash [Hash] publisher node connection info
|
51
|
+
# @param publications [Array<String>] publication names to subscribe to
|
52
|
+
# @param options [Hash] optional parameters for CREATE SUBSCRIPTION
|
53
|
+
def create_subscription(name, conninfo_hash, publications, options = {})
|
54
|
+
connection_string = connection.escape_string(PG::Connection.parse_connect_args(conninfo_hash))
|
55
|
+
base_command = <<-SQL
|
56
|
+
CREATE SUBSCRIPTION #{connection.quote_ident(name)}
|
57
|
+
CONNECTION '#{connection_string}'
|
58
|
+
PUBLICATION #{safe_list(publications)}
|
59
|
+
SQL
|
60
|
+
typed_exec(command_builder.command_with_options(base_command, "WITH", options))
|
61
|
+
end
|
62
|
+
|
63
|
+
# Disconnects the subscription and removes it
|
64
|
+
#
|
65
|
+
# @param name [String] subscription name
|
66
|
+
# @param ifexists [Boolean] if true an error is not thrown when the subscription does not exist
|
67
|
+
def drop_subscription(name, ifexists = false)
|
68
|
+
typed_exec("DROP SUBSCRIPTION#{" IF EXISTS" if ifexists} #{connection.quote_ident(name)}")
|
69
|
+
end
|
70
|
+
|
71
|
+
# Updates a subscription connection string
|
72
|
+
#
|
73
|
+
# @param name [String] subscription name
|
74
|
+
# @param conninfo_hash [Hash] new external connection hash to the publisher node
|
75
|
+
def set_subscription_conninfo(name, conninfo_hash)
|
76
|
+
connection_string = connection.escape_string(PG::Connection.parse_connect_args(conninfo_hash))
|
77
|
+
typed_exec("ALTER SUBSCRIPTION #{connection.quote_ident(name)} CONNECTION '#{connection_string}'")
|
78
|
+
end
|
79
|
+
|
80
|
+
# Changes list of subscribed publications
|
81
|
+
#
|
82
|
+
# @param name [String] subscription name
|
83
|
+
# @param publications [Array<String>] publication names to subscribe to
|
84
|
+
# @param options [Hash] optional parameters
|
85
|
+
def set_subscription_publications(name, publications, options = {})
|
86
|
+
base_command = <<-SQL
|
87
|
+
ALTER SUBSCRIPTION #{connection.quote_ident(name)}
|
88
|
+
SET PUBLICATION #{safe_list(publications)}
|
89
|
+
SQL
|
90
|
+
typed_exec(@command_builder.command_with_options(base_command, "WITH", options))
|
91
|
+
end
|
92
|
+
|
93
|
+
# Fetch missing table information from publisher
|
94
|
+
#
|
95
|
+
# @param name [String] subscription name
|
96
|
+
# @param options [Hash] optional parameters
|
97
|
+
def sync_subscription(name, options = {})
|
98
|
+
base_command = <<-SQL
|
99
|
+
ALTER SUBSCRIPTION #{connection.quote_ident(name)} REFRESH PUBLICATION
|
100
|
+
SQL
|
101
|
+
typed_exec(@command_builder.command_with_options(base_command, "WITH", options))
|
102
|
+
end
|
103
|
+
|
104
|
+
# Enables the previously disabled subscription
|
105
|
+
#
|
106
|
+
# @param name [String] subscription name
|
107
|
+
def enable_subscription(name)
|
108
|
+
typed_exec("ALTER SUBSCRIPTION #{connection.quote_ident(name)} ENABLE")
|
109
|
+
end
|
110
|
+
|
111
|
+
# Disables the running subscription
|
112
|
+
#
|
113
|
+
# @param name [String] subscription name
|
114
|
+
def disable_subscription(name)
|
115
|
+
typed_exec("ALTER SUBSCRIPTION #{connection.quote_ident(name)} DISABLE")
|
116
|
+
end
|
117
|
+
|
118
|
+
# Alters parameters originally set by CREATE SUBSCRIPTION
|
119
|
+
#
|
120
|
+
# @param name [String] subscription name
|
121
|
+
# @param options [Hash] parameters to set
|
122
|
+
def alter_subscription_options(name, options)
|
123
|
+
base_command = "ALTER SUBSCRIPTION #{connection.quote_ident(name)}"
|
124
|
+
typed_exec(command_builder.command_with_options(base_command, "SET", options))
|
125
|
+
end
|
126
|
+
|
127
|
+
# Sets the owner of the subscription
|
128
|
+
#
|
129
|
+
# @param name [String] subscription name
|
130
|
+
# @param owner [String] new owner user name
|
131
|
+
def set_subscription_owner(name, owner)
|
132
|
+
typed_exec("ALTER SUBSCRIPTION #{connection.quote_ident(name)} OWNER TO #{connection.quote_ident(owner)}")
|
133
|
+
end
|
134
|
+
|
135
|
+
# Renames the subscription
|
136
|
+
#
|
137
|
+
# @param name [String] current subscription name
|
138
|
+
# @param new_name [String] new subscription name
|
139
|
+
def rename_subscription(name, new_name)
|
140
|
+
typed_exec("ALTER SUBSCRIPTION #{connection.quote_ident(name)} RENAME TO #{connection.quote_ident(new_name)}")
|
141
|
+
end
|
142
|
+
|
143
|
+
# Shows status and basic information about all subscriptions
|
144
|
+
#
|
145
|
+
# @return [Array<Hash>] a list of subscriptions
|
146
|
+
# keys:
|
147
|
+
# subscription_name
|
148
|
+
# database_name
|
149
|
+
# owner
|
150
|
+
# worker_count
|
151
|
+
# enabled
|
152
|
+
# subscription_dsn
|
153
|
+
# slot_name
|
154
|
+
# publications
|
155
|
+
# remote_replication_lsn
|
156
|
+
# local_replication_lsn
|
157
|
+
def subscriptions(dbname = nil)
|
158
|
+
subscriptions = typed_exec(<<-SQL).to_a
|
159
|
+
SELECT
|
160
|
+
sub.subname::TEXT AS subscription_name,
|
161
|
+
pg_database.datname::TEXT AS database_name,
|
162
|
+
pg_user.usename::TEXT AS owner,
|
163
|
+
COUNT(sub_stat.pid) AS worker_count,
|
164
|
+
sub.subenabled AS enabled,
|
165
|
+
sub.subconninfo AS subscription_dsn,
|
166
|
+
sub.subslotname::TEXT AS slot_name,
|
167
|
+
sub.subpublications AS publications,
|
168
|
+
stat.remote_lsn::TEXT AS remote_replication_lsn,
|
169
|
+
stat.local_lsn::TEXT AS local_replication_lsn
|
170
|
+
FROM
|
171
|
+
pg_subscription AS sub
|
172
|
+
JOIN pg_user
|
173
|
+
ON sub.subowner = usesysid
|
174
|
+
JOIN pg_database
|
175
|
+
ON sub.subdbid = pg_database.oid
|
176
|
+
LEFT JOIN pg_replication_origin_status stat
|
177
|
+
ON concat('pg_', sub.oid) = stat.external_id
|
178
|
+
LEFT JOIN pg_stat_subscription sub_stat
|
179
|
+
ON sub_stat.subid = sub.oid AND sub_stat.pid IS NOT NULL
|
180
|
+
GROUP BY
|
181
|
+
sub.subname,
|
182
|
+
pg_database.datname,
|
183
|
+
pg_user.usename,
|
184
|
+
sub.subenabled,
|
185
|
+
sub.subconninfo,
|
186
|
+
sub.subslotname,
|
187
|
+
sub.subpublications,
|
188
|
+
stat.remote_lsn,
|
189
|
+
stat.local_lsn
|
190
|
+
SQL
|
191
|
+
|
192
|
+
dbname ? subscriptions.select { |s| s["database_name"] == dbname } : subscriptions
|
193
|
+
end
|
194
|
+
|
195
|
+
# Returns if this database is subscribing to any publications
|
196
|
+
#
|
197
|
+
# @return [Boolean] true if there are any subscriptions, false otherwise
|
198
|
+
def subscriber?
|
199
|
+
subscriptions.any?
|
200
|
+
end
|
201
|
+
|
202
|
+
# Lists the current publications
|
203
|
+
#
|
204
|
+
# @return [Array<String>] publication names
|
205
|
+
def publications
|
206
|
+
typed_exec(<<-SQL)
|
207
|
+
SELECT
|
208
|
+
pubname::TEXT AS name,
|
209
|
+
usename::TEXT AS owner,
|
210
|
+
puballtables,
|
211
|
+
pubinsert,
|
212
|
+
pubupdate,
|
213
|
+
pubdelete
|
214
|
+
FROM
|
215
|
+
pg_publication
|
216
|
+
JOIN pg_user ON pubowner = usesysid
|
217
|
+
SQL
|
218
|
+
end
|
219
|
+
|
220
|
+
def publishes?(publication_name)
|
221
|
+
publications.any? { |p| p["name"] == publication_name }
|
222
|
+
end
|
223
|
+
|
224
|
+
# Creates a new publication
|
225
|
+
#
|
226
|
+
# @param name [String] publication name
|
227
|
+
# @param all_tables [Boolean] replicate changes for all tables, including ones created in the future
|
228
|
+
# @param tables [Array<String>] tables to be added to the publication, ignored if all_tables is true
|
229
|
+
# @param options [Hash] optional parameters
|
230
|
+
def create_publication(name, all_tables = false, tables = [], options = {})
|
231
|
+
base_command = "CREATE PUBLICATION #{connection.quote_ident(name)}"
|
232
|
+
if all_tables
|
233
|
+
base_command << " FOR ALL TABLES"
|
234
|
+
elsif !tables.empty?
|
235
|
+
base_command << " FOR TABLE #{safe_list(tables)}"
|
236
|
+
end
|
237
|
+
typed_exec(@command_builder.command_with_options(base_command, "WITH", options))
|
238
|
+
end
|
239
|
+
|
240
|
+
# Adds tables to a publication
|
241
|
+
#
|
242
|
+
# @param name [String] publication name
|
243
|
+
# @param tables [Array<String>] table names to add
|
244
|
+
def add_tables_to_publication(name, tables)
|
245
|
+
typed_exec("ALTER PUBLICATION #{connection.quote_ident(name)} ADD TABLE #{safe_list(tables)}")
|
246
|
+
end
|
247
|
+
|
248
|
+
# Sets the tables included in a publication
|
249
|
+
#
|
250
|
+
# @param name [String] publication name
|
251
|
+
# @param tables [Array<String>] table names
|
252
|
+
def set_publication_tables(name, tables)
|
253
|
+
typed_exec("ALTER PUBLICATION #{connection.quote_ident(name)} SET TABLE #{safe_list(tables)}")
|
254
|
+
end
|
255
|
+
|
256
|
+
# Removes tables from a publication
|
257
|
+
#
|
258
|
+
# @param name [String] publication name
|
259
|
+
# @param tables [Array<String>] table names to remove
|
260
|
+
def remove_tables_from_publication(name, tables)
|
261
|
+
typed_exec("ALTER PUBLICATION #{connection.quote_ident(name)} DROP TABLE #{safe_list(tables)}")
|
262
|
+
end
|
263
|
+
|
264
|
+
# Alters parameters originally set by CREATE PUBLICATION
|
265
|
+
#
|
266
|
+
# @param name [String] publication name
|
267
|
+
# @param options [Hash] parameters to set
|
268
|
+
def alter_publication_options(name, options)
|
269
|
+
base_command = "ALTER PUBLICATION #{connection.quote_ident(name)}"
|
270
|
+
typed_exec(command_builder.command_with_options(base_command, "SET", options))
|
271
|
+
end
|
272
|
+
|
273
|
+
# Sets the owner of a publication
|
274
|
+
#
|
275
|
+
# @param name [String] publication name
|
276
|
+
# @param owner [String] new owner user name
|
277
|
+
def set_publication_owner(name, owner)
|
278
|
+
typed_exec("ALTER PUBLICATION #{connection.quote_ident(name)} OWNER TO #{connection.quote_ident(owner)}")
|
279
|
+
end
|
280
|
+
|
281
|
+
# Renames a publication
|
282
|
+
#
|
283
|
+
# @param name [String] current publication name
|
284
|
+
# @param new_name [String] new publication name
|
285
|
+
def rename_publication(name, new_name)
|
286
|
+
typed_exec("ALTER PUBLICATION #{connection.quote_ident(name)} RENAME TO #{connection.quote_ident(new_name)}")
|
287
|
+
end
|
288
|
+
|
289
|
+
# Remove a publication
|
290
|
+
#
|
291
|
+
# @param name [String] publication name
|
292
|
+
# @param ifexists [Boolean] if true an error is not thrown when the publication does not exist
|
293
|
+
def drop_publication(name, ifexists = false)
|
294
|
+
typed_exec("DROP PUBLICATION#{" IF EXISTS" if ifexists} #{connection.quote_ident(name)}")
|
295
|
+
end
|
296
|
+
|
297
|
+
# Lists the tables currently in the publication
|
298
|
+
#
|
299
|
+
# @param set_name [String] publication name
|
300
|
+
# @return [Array<String>] table names
|
301
|
+
def tables_in_publication(name)
|
302
|
+
typed_exec(<<-SQL, name).values.flatten
|
303
|
+
SELECT tablename::TEXT
|
304
|
+
FROM pg_publication_tables
|
305
|
+
WHERE pubname = $1
|
306
|
+
SQL
|
307
|
+
end
|
308
|
+
|
309
|
+
private
|
310
|
+
|
311
|
+
def safe_list(list)
|
312
|
+
list.map { |e| connection.quote_ident(e) }.join(", ")
|
313
|
+
end
|
314
|
+
|
315
|
+
def typed_exec(sql, *params)
|
316
|
+
result = connection.async_exec(sql, params, nil, PG::BasicTypeMapForQueries.new(connection))
|
317
|
+
result.map_types!(PG::BasicTypeMapForResults.new(connection))
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require "pg"
|
2
|
+
|
3
|
+
module PG
|
4
|
+
module LogicalReplication
|
5
|
+
class CommandBuilder
|
6
|
+
attr_accessor :connection
|
7
|
+
|
8
|
+
def initialize(connection)
|
9
|
+
@connection = connection
|
10
|
+
end
|
11
|
+
|
12
|
+
def command_with_options(sql, keyword, options)
|
13
|
+
raise CommandBuilderError, "Unrecognized keyword #{keyword}" unless ["WITH", "SET"].include?(keyword)
|
14
|
+
if options.empty?
|
15
|
+
case keyword
|
16
|
+
when "WITH"
|
17
|
+
return sql
|
18
|
+
when "SET"
|
19
|
+
raise CommandBuilderError, "Keyword SET requires options"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
"#{sql} #{keyword} (#{parameters_list(options)})"
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def parameters_list(options)
|
28
|
+
options.to_a.map do |k, v|
|
29
|
+
"#{connection.quote_ident(k)} = #{safe_value(k, v)}"
|
30
|
+
end.join(", ")
|
31
|
+
end
|
32
|
+
|
33
|
+
VALUE_TYPES_BY_KEY = {
|
34
|
+
# subscription options
|
35
|
+
"connect" => "bool",
|
36
|
+
"copy_data" => "bool",
|
37
|
+
"create_slot" => "bool",
|
38
|
+
"enabled" => "bool",
|
39
|
+
"refresh" => "bool",
|
40
|
+
"slot_name" => "string",
|
41
|
+
"synchronous_commit" => "string",
|
42
|
+
# publication options
|
43
|
+
"publish" => "string"
|
44
|
+
}.freeze
|
45
|
+
|
46
|
+
def safe_value(key, value)
|
47
|
+
case VALUE_TYPES_BY_KEY[key]
|
48
|
+
when "string"
|
49
|
+
value == "NONE" ? "NONE" : quote_string(value)
|
50
|
+
when "bool"
|
51
|
+
value ? "true" : "false"
|
52
|
+
else
|
53
|
+
raise PG::LogicalReplication::CommandBuilderError, "Option type for key '#{key}' not defined"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def quote_string(s)
|
58
|
+
"#{connection.escape_literal(s)}"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pg/logical_replication/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "pg-logical_replication"
|
8
|
+
spec.version = PG::LogicalReplication::VERSION
|
9
|
+
spec.authors = ["Nick Carboni"]
|
10
|
+
spec.email = ["ncarboni@redhat.com"]
|
11
|
+
|
12
|
+
spec.summary = "A ruby gem for configuring and using postgresql logical replication"
|
13
|
+
spec.description = <<-EOS
|
14
|
+
This gem provides a class with methods which map directly to the PostgreSQL DSL for logical replication configuration
|
15
|
+
EOS
|
16
|
+
spec.homepage = "https://github.com/ManageIQ/pg-logical_replication"
|
17
|
+
spec.license = "Apache-2.0"
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "pg"
|
25
|
+
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
spec.add_development_dependency "rubocop", "~> 0.52"
|
29
|
+
spec.add_development_dependency "simplecov"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg-logical_replication
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nick Carboni
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pg
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.52'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.52'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: 'This gem provides a class with methods which map directly to the PostgreSQL
|
84
|
+
DSL for logical replication configuration
|
85
|
+
|
86
|
+
'
|
87
|
+
email:
|
88
|
+
- ncarboni@redhat.com
|
89
|
+
executables: []
|
90
|
+
extensions: []
|
91
|
+
extra_rdoc_files: []
|
92
|
+
files:
|
93
|
+
- ".codeclimate.yml"
|
94
|
+
- ".gitignore"
|
95
|
+
- ".rspec"
|
96
|
+
- ".rubocop.yml"
|
97
|
+
- ".rubocop_cc.yml"
|
98
|
+
- ".rubocop_local.yml"
|
99
|
+
- ".travis.yml"
|
100
|
+
- CHANGELOG.md
|
101
|
+
- Gemfile
|
102
|
+
- LICENSE.txt
|
103
|
+
- README.md
|
104
|
+
- Rakefile
|
105
|
+
- ci/before_install.sh
|
106
|
+
- lib/pg-logical_replication.rb
|
107
|
+
- lib/pg/logical_replication.rb
|
108
|
+
- lib/pg/logical_replication/client.rb
|
109
|
+
- lib/pg/logical_replication/command_builder.rb
|
110
|
+
- lib/pg/logical_replication/version.rb
|
111
|
+
- pg-logical_replication.gemspec
|
112
|
+
homepage: https://github.com/ManageIQ/pg-logical_replication
|
113
|
+
licenses:
|
114
|
+
- Apache-2.0
|
115
|
+
metadata: {}
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 2.6.14
|
133
|
+
signing_key:
|
134
|
+
specification_version: 4
|
135
|
+
summary: A ruby gem for configuring and using postgresql logical replication
|
136
|
+
test_files: []
|