Skip to content

Commit

Permalink
[PostgresAdmin] Use pg-logical_replication
Browse files Browse the repository at this point in the history
Use PG::LogicalReplication::Client to remove subscriptions or
publications on a db prior to doing a database reload.

Doesn't drop the `MiqRegion`, as was previously done as part of
PglogicalSubscription in core:

  https://github.com/ManageIQ/manageiq/blob/8ca93da126aada556685db5d05aaf5e9a0666900/app/models/pglogical_subscription.rb#L70

But since this is a database reload, and the Region will be removed from
the DB anyway, this should not matter, and the necessary code required
to perform that delete will be pointless to include in this gem.
  • Loading branch information
NickLaMuro committed Apr 13, 2021
1 parent 72dfb22 commit 7964013
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/manageiq/appliance_console/postgres_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ class << self
raise message
end

disable_replication(dbname)

if connection_count(backup_type) > 1
message = "Database restore failed. #{connection_count - 1} connections remain to the database."
ManageIQ::ApplianceConsole.logger.error(message)
Expand All @@ -356,6 +358,33 @@ class << self
result[0]["count"] > 0
end

ALWAYS_EXCLUDED_TABLES = %w(ar_internal_metadata schema_migrations repl_events repl_monitor repl_nodes).freeze
private_class_method def self.disable_replication(dbname)
require 'pg/logical_replication'

with_pg_connection do |conn|
pglogical = PG::LogicalReplication::Client.new(conn)

if pglogical.subscriber?
pglogical.subcriptions(dbname).each do |subscriber|
sub_id = subscriber["subscription_name"]
begin
pglogical.drop_subscription(sub_id, true)
rescue PG::InternalError => e
raise unless e.message =~ /could not connect to publisher/
raise unless e.message =~ /replication slot .* does not exist/

pglogical.disable_subscription(sub_id).check
pglogical.alter_subscription_options(sub_id, "slot_name" => "NONE")
pglogical.drop_subscription(sub_id, true)
end
end
elsif pglogical.publishes?('miq')
pglogical.drop_publication(PUBLICATION_NAME)
end
end
end

private_class_method def self.connection_count(backup_type, dbname)
result = nil

Expand Down
1 change: 1 addition & 0 deletions manageiq-appliance_console.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "net-scp", "~> 1.2.1"
spec.add_runtime_dependency "optimist", "~> 3.0"
spec.add_runtime_dependency "pg"
spec.add_runtime_dependency "pg-logical_replication"
spec.add_runtime_dependency "rbnacl", ">= 3.2", "< 5.0"

spec.add_development_dependency "bundler"
Expand Down

0 comments on commit 7964013

Please sign in to comment.