pg-pglogical 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: da05087f43ad43beb2667fe42141438063b2132d
4
+ data.tar.gz: ef46f2b1fc9527374b9421d30c2fc4e2698911c3
5
+ SHA512:
6
+ metadata.gz: 1e6956ce6c83426d3baddf094f5142d7bc777a3aec892aafd6e1e74b9f6382b75888fd2c3142f6748fd0b7e48a4d4dbe577d523f6a02ef9466d2f25fb3973038
7
+ data.tar.gz: 848e14a6cbc2cd559660958009e545c83cbd7ef1113c5b0c29c11522dd8e669c7e29e85cc1f9609e30775d246c98358ecdcbb58b78ff4297687d17ed95aff5e1
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --format documentation
3
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ sudo: required
2
+ dist: trusty
3
+ language: ruby
4
+ rvm:
5
+ - 2.3.1
6
+ before_install:
7
+ - gem install bundler -v 1.13.6
8
+ - source ${TRAVIS_BUILD_DIR}/ci/before_install.sh
9
+ script:
10
+ - bundle exec rake spec:setup spec
11
+ addons:
12
+ postgresql: "9.5"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pg-pglogical.gemspec
4
+ gemspec
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,30 @@
1
+ # PG::Pglogical
2
+
3
+ This gem extends the ActiveRecord connection object to include methods which map directly to the SQL stored procedure APIs provided by pglogical.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pg-pglogical'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pg-pglogical
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec:setup spec` to run the tests. `rake spec:teardown` will remove the databases created for test purposes. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`.
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ManageIQ/pg-pglogical.
30
+
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("pg_pglogical_test")
24
+ create_database("pg_pglogical_test_target")
25
+ end
26
+
27
+ desc "Teardown the test databases"
28
+ task :teardown do
29
+ drop_database("pg_pglogical_test")
30
+ drop_database("pg_pglogical_test_target")
31
+ end
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:spec)
35
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pg/pglogical"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,13 @@
1
+ set -v
2
+
3
+ sudo sh -c 'echo "deb http://packages.2ndquadrant.com/pglogical/apt/ $(lsb_release -cs)-2ndquadrant main" > /etc/apt/sources.list.d/2ndquadrant.list'
4
+
5
+ wget --quiet -O - http://packages.2ndquadrant.com/pglogical/apt/AA7A6805.asc | sudo apt-key add -
6
+ sudo apt-get -y update
7
+ sudo apt-get -y install postgresql-9.5-pglogical
8
+
9
+ echo -e "wal_level = 'logical'\nmax_worker_processes = 10\nmax_replication_slots = 10\nmax_wal_senders = 10\nshared_preload_libraries = 'pglogical'" | sudo tee -a /etc/postgresql/9.5/main/postgresql.conf
10
+ echo -e "local replication all trust" | sudo tee -a /etc/postgresql/9.5/main/pg_hba.conf
11
+ sudo service postgresql restart 9.5
12
+
13
+ set +v
@@ -0,0 +1,15 @@
1
+ require "pg/pglogical"
2
+ require "active_record"
3
+ require "active_record/connection_adapters/postgresql_adapter"
4
+
5
+ module PG
6
+ module Pglogical
7
+ module ActiveRecordExtension
8
+ def pglogical
9
+ PG::Pglogical::Client.new(self)
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include PG::Pglogical::ActiveRecordExtension
@@ -0,0 +1,328 @@
1
+ require "pg"
2
+
3
+ module PG
4
+ module Pglogical
5
+ class Client
6
+ attr_accessor :connection
7
+
8
+ # @param connection [PostgreSQLAdapter] ActiveRecord database connection
9
+ def initialize(connection)
10
+ @connection = connection
11
+ end
12
+
13
+ def installed?
14
+ connection.select_value("SELECT EXISTS(SELECT * FROM pg_available_extensions WHERE name = 'pglogical')")
15
+ end
16
+
17
+ # Returns whether pglogical is currently enabled or not
18
+ #
19
+ # @return [Boolean]
20
+ def enabled?
21
+ return false unless installed? && connection.extension_enabled?("pglogical")
22
+ return true if connection.postgresql_version >= 90_500
23
+ connection.extension_enabled?("pglogical_origin")
24
+ end
25
+
26
+ # Enables pglogical postgres extensions
27
+ def enable
28
+ connection.enable_extension("pglogical_origin") if connection.postgresql_version < 90_500
29
+ connection.enable_extension("pglogical")
30
+ end
31
+
32
+ def disable
33
+ connection.disable_extension("pglogical")
34
+ connection.disable_extension("pglogical_origin") if connection.postgresql_version < 90_500
35
+ end
36
+
37
+ # Monitoring
38
+ #
39
+
40
+ # Reports on replication lag from provider to subscriber nodes
41
+ # This method must be run on the provider node
42
+ #
43
+ # @return [Array<Hash<String,String>>] List of returned lag and application names,
44
+ # one for each replication process
45
+ def lag_bytes
46
+ typed_exec(<<-SQL).to_a
47
+ SELECT
48
+ pg_xlog_location_diff(pg_current_xlog_insert_location(), flush_location) AS lag_bytes,
49
+ application_name
50
+ FROM pg_stat_replication
51
+ SQL
52
+ end
53
+
54
+ # Reports on replication bytes of WAL being retained for each replication slot
55
+ # This method must be run on the provider node
56
+ #
57
+ # @return [Array<Hash<String,String>>] List of returned WAL bytes and replication slot names,
58
+ # one for each replication process
59
+ def wal_retained_bytes
60
+ typed_exec(<<-SQL).to_a
61
+ SELECT
62
+ pg_xlog_location_diff(pg_current_xlog_insert_location(), restart_lsn) AS retained_bytes,
63
+ slot_name
64
+ FROM pg_replication_slots
65
+ WHERE plugin = 'pglogical_output'
66
+ SQL
67
+ end
68
+
69
+ # Node Management
70
+ #
71
+
72
+ # Creates a node
73
+ #
74
+ # @param name [String]
75
+ # @param dsn [String] external connection string to the node
76
+ def node_create(name, dsn)
77
+ typed_exec("SELECT pglogical.create_node($1, $2)", name, dsn)
78
+ end
79
+
80
+ # Updates a node connection string
81
+ #
82
+ # @param name [String]
83
+ # @param dsn [String] new external connection string to the node
84
+ # @return [Boolean] true if the dsn was updated, false otherwise
85
+ #
86
+ # NOTE: This method relies on the internals of the pglogical tables
87
+ # rather than a published API.
88
+ # NOTE: Disable subscriptions involving the node before
89
+ # calling this method for a provider node in a subscriber
90
+ # database.
91
+ def node_dsn_update(name, dsn)
92
+ res = typed_exec(<<-SQL, name, dsn)
93
+ UPDATE pglogical.node_interface
94
+ SET if_dsn = $2
95
+ WHERE if_nodeid = (
96
+ SELECT node_id
97
+ FROM pglogical.node
98
+ WHERE node_name = $1
99
+ )
100
+ SQL
101
+
102
+ res.cmd_tuples == 1
103
+ end
104
+
105
+ # Drops the node
106
+ #
107
+ # @param name [String]
108
+ # @param ifexists [Boolean]
109
+ def node_drop(name, ifexists = false)
110
+ typed_exec("SELECT pglogical.drop_node($1, $2)", name, ifexists)
111
+ end
112
+
113
+ def nodes
114
+ typed_exec(<<-SQL)
115
+ SELECT node_name AS name, if_dsn AS conn_string
116
+ FROM pglogical.node join pglogical.node_interface
117
+ ON if_nodeid = node_id
118
+ SQL
119
+ end
120
+
121
+ # Subscription Management
122
+ #
123
+
124
+ # Creates a subscription to a provider node
125
+ #
126
+ # @param name [String] subscription name
127
+ # @param dsn [String] provider node connection string
128
+ # @param replication_sets [Array<String>] replication set names to subscribe to
129
+ # @param sync_structure [Boolean] sync the schema structure when subscribing
130
+ # @param sync_data [Boolean] sync the data when subscribing
131
+ # @param forward_origins [Array<String>] names of non-provider nodes to replicate changes from (cascading replication)
132
+ def subscription_create(name, dsn, replication_sets = %w(default default_insert_only),
133
+ sync_structure = true, sync_data = true, forward_origins = ["all"])
134
+ command = "SELECT pglogical.create_subscription($1, $2, $3, $4, $5, $6)"
135
+ typed_exec(command, name, dsn, replication_sets, sync_structure, sync_data, forward_origins)
136
+ end
137
+
138
+ # Disconnects the subscription and removes it
139
+ #
140
+ # @param name [String] subscription name
141
+ # @param ifexists [Boolean] if true an error is not thrown when the subscription does not exist
142
+ def subscription_drop(name, ifexists = false)
143
+ typed_exec("SELECT pglogical.drop_subscription($1, $2)", name, ifexists)
144
+ end
145
+
146
+ # Disables a subscription and disconnects it from the provider
147
+ #
148
+ # @param name [String] subscription name
149
+ # @param immediate [Boolean] do not wait for the current transaction before stopping
150
+ def subscription_disable(name, immediate = false)
151
+ typed_exec("SELECT pglogical.alter_subscription_disable($1, $2)", name, immediate)
152
+ end
153
+
154
+ # Enables a previously disabled subscription
155
+ #
156
+ # @param name [String] subscription name
157
+ # @param immediate [Boolean] do not wait for the current transaction before starting
158
+ def subscription_enable(name, immediate = false)
159
+ typed_exec("SELECT pglogical.alter_subscription_enable($1, $2)", name, immediate)
160
+ end
161
+
162
+ # Syncs all unsynchronized tables in all sets in a single operation.
163
+ # Command does not block
164
+ #
165
+ # @param name [String] subscription name
166
+ # @param truncate [Boolean] truncate the tables before syncing
167
+ def subscription_sync(name, truncate = false)
168
+ typed_exec("SELECT pglogical.alter_subscription_synchronize($1, $2)", name, truncate)
169
+ end
170
+
171
+ # Resyncs one existing table
172
+ # Table will be truncated before the sync
173
+ #
174
+ # @param name [String] subscription name
175
+ # @param table [String] name of the table to resync
176
+ def subscription_resync_table(name, table)
177
+ typed_exec("SELECT pglogical.alter_subscription_resynchronize_table($1, $2)", name, table)
178
+ end
179
+
180
+ # Adds a replication set to a subscription
181
+ # Does not sync, only activates event consumption
182
+ #
183
+ # @param name [String] subscription name
184
+ # @param set_name [String] replication set name
185
+ def subscription_add_replication_set(name, set_name)
186
+ typed_exec("SELECT pglogical.alter_subscription_add_replication_set($1, $2)", name, set_name)
187
+ end
188
+
189
+ # Removes a replication set from a subscription
190
+ #
191
+ # @param name [String] subscription name
192
+ # @param set_name [String] replication set name
193
+ def subscription_remove_replication_set(name, set_name)
194
+ typed_exec("SELECT pglogical.alter_subscription_remove_replication_set($1, $2)", name, set_name)
195
+ end
196
+
197
+ # Shows status and basic information about a subscription
198
+ #
199
+ # @prarm name [String] subscription name
200
+ # @return a hash with the subscription information
201
+ # keys:
202
+ # subscription_name
203
+ # status
204
+ # provider_node
205
+ # provider_dsn
206
+ # slot_name
207
+ # replication_sets
208
+ # forward_origins
209
+ def subscription_show_status(name)
210
+ res = typed_exec("SELECT * FROM pglogical.show_subscription_status($1)", name).first
211
+ res["replication_sets"] = res["replication_sets"][1..-2].split(",")
212
+ res["forward_origins"] = res["forward_origins"][1..-2].split(",")
213
+ res
214
+ end
215
+
216
+ # Shows the status of all configured subscriptions
217
+ #
218
+ # @return Array<Hash> list of results from #subscription_show_status
219
+ def subscriptions
220
+ ret = []
221
+ connection.select_values("SELECT sub_name FROM pglogical.subscription").each do |s|
222
+ ret << subscription_show_status(s)
223
+ end
224
+ ret
225
+ end
226
+
227
+ # Replication Sets
228
+ #
229
+
230
+ # Lists the current replication sets
231
+ #
232
+ # @return [Array<String>] the replication sets
233
+ def replication_sets
234
+ typed_exec("SELECT set_name FROM pglogical.replication_set").values.flatten
235
+ end
236
+
237
+ # Creates a new replication set
238
+ #
239
+ # @param set_name [String] new replication set name
240
+ # @param insert [Boolean] replicate INSERT events
241
+ # @param update [Boolean] replicate UPDATE events
242
+ # @param delete [Boolean] replicate DELETE events
243
+ # @param truncate [Boolean] replicate TRUNCATE events
244
+ def replication_set_create(set_name, insert = true, update = true, delete = true, truncate = true)
245
+ typed_exec("SELECT pglogical.create_replication_set($1, $2, $3, $4, $5)",
246
+ set_name, insert, update, delete, truncate)
247
+ end
248
+
249
+ # Alters an existing replication set
250
+ #
251
+ # @param set_name [String] replication set name
252
+ # @param insert [Boolean] replicate INSERT events
253
+ # @param update [Boolean] replicate UPDATE events
254
+ # @param delete [Boolean] replicate DELETE events
255
+ # @param truncate [Boolean] replicate TRUNCATE events
256
+ def replication_set_alter(set_name, insert = true, update = true, delete = true, truncate = true)
257
+ typed_exec("SELECT pglogical.alter_replication_set($1, $2, $3, $4, $5)",
258
+ set_name, insert, update, delete, truncate)
259
+ end
260
+
261
+ # Removes a replication set
262
+ #
263
+ # @param set_name [string] replication set name
264
+ def replication_set_drop(set_name)
265
+ typed_exec("SELECT pglogical.drop_replication_set($1)", set_name)
266
+ end
267
+
268
+ # Adds a table to a replication set
269
+ #
270
+ # @param set_name [String] replication set name
271
+ # @param table_name [String] table name to add
272
+ # @param sync [Boolean] sync the table on all subscribers to the given replication set
273
+ def replication_set_add_table(set_name, table_name, sync = false)
274
+ typed_exec("SELECT pglogical.replication_set_add_table($1, $2, $3)", set_name, table_name, sync)
275
+ end
276
+
277
+ # Adds all tables in the given schemas to the replication set
278
+ #
279
+ # @param set_name [String] replication set name
280
+ # @param schema_names [Array<String>] list of schema names
281
+ # @param sync [Boolean] sync table data to all the subscribers to the replication set
282
+ def replication_set_add_all_tables(set_name, schema_names, sync = false)
283
+ typed_exec("SELECT pglogical.replication_set_add_all_tables($1, $2, $3)",
284
+ set_name, schema_names, sync)
285
+ end
286
+
287
+ # Removes a table from a replication set
288
+ #
289
+ # @param set_name [String] replication set name
290
+ # @param table_name [String] table to remove
291
+ def replication_set_remove_table(set_name, table_name)
292
+ typed_exec("SELECT pglogical.replication_set_remove_table($1, $2)", set_name, table_name)
293
+ end
294
+
295
+ # Lists the tables currently in the replication set
296
+ #
297
+ # @param set_name [String] replication set name
298
+ # @return [Array<String>] names of the tables in the given set
299
+ def tables_in_replication_set(set_name)
300
+ typed_exec(<<-SQL, set_name).values.flatten
301
+ SELECT set_reloid
302
+ FROM pglogical.replication_set_relation
303
+ JOIN pglogical.replication_set
304
+ USING (set_id)
305
+ WHERE set_name = $1
306
+ SQL
307
+ end
308
+
309
+ def with_replication_set_lock(set_name)
310
+ connection.transaction(:requires_new => true) do
311
+ typed_exec(<<-SQL, set_name)
312
+ SELECT *
313
+ FROM pglogical.replication_set
314
+ WHERE set_name = $1
315
+ FOR UPDATE
316
+ SQL
317
+ yield
318
+ end
319
+ end
320
+
321
+ private
322
+
323
+ def typed_exec(sql, *params)
324
+ connection.raw_connection.async_exec(sql, params, nil, PG::BasicTypeMapForQueries.new(connection.raw_connection))
325
+ end
326
+ end
327
+ end
328
+ end
@@ -0,0 +1,5 @@
1
+ module PG
2
+ module Pglogical
3
+ VERSION = "1.0.0".freeze
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require "pg/pglogical/version"
2
+ require "pg/pglogical/client"
@@ -0,0 +1 @@
1
+ require 'pg/pglogical'
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pg/pglogical/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pg-pglogical"
8
+ spec.version = PG::Pglogical::VERSION
9
+ spec.authors = ["Nick Carboni"]
10
+ spec.email = ["ncarboni@redhat.com"]
11
+
12
+ spec.summary = %q{A ruby gem for configuring and using pglogical}
13
+ spec.description = %q{This gem provides a class with methods which map directly to the SQL stored procedure APIs provided by pglogical. It also provides a way to mix these methods directly into the ActiveRecord connection object.}
14
+ spec.homepage = "https://github.com/ManageIQ/pg-pglogical"
15
+ spec.license = "Apache-2.0"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "activerecord"
23
+ spec.add_dependency "pg"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.13"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg-pglogical
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: 2016-12-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
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: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: This gem provides a class with methods which map directly to the SQL
84
+ stored procedure APIs provided by pglogical. It also provides a way to mix these
85
+ methods directly into the ActiveRecord connection object.
86
+ email:
87
+ - ncarboni@redhat.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".travis.yml"
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - ci/before_install.sh
102
+ - lib/pg-pglogical.rb
103
+ - lib/pg/pglogical.rb
104
+ - lib/pg/pglogical/active_record_extension.rb
105
+ - lib/pg/pglogical/client.rb
106
+ - lib/pg/pglogical/version.rb
107
+ - pg-pglogical.gemspec
108
+ homepage: https://github.com/ManageIQ/pg-pglogical
109
+ licenses:
110
+ - Apache-2.0
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.5.2
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: A ruby gem for configuring and using pglogical
132
+ test_files: []