Skip to content

Commit d05a81a

Browse files
committed
Do not mark switchover complete if subscription drop fails
1 parent d3de2a2 commit d05a81a

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

lib/pg_easy_replicate/orchestrate.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ def switchover(
239239
revoke_connections_on_source_db(group_name)
240240
wait_for_remaining_catchup(group_name)
241241
refresh_sequences(conn_string: target_conn, schema: group[:schema_name])
242+
243+
drop_subscription(
244+
group_name: group_name,
245+
target_conn_string: target_conn,
246+
)
242247
mark_switchover_complete(group_name)
243248

244249
unless skip_vacuum_analyze
@@ -248,11 +253,6 @@ def switchover(
248253
schema: group[:schema_name],
249254
)
250255
end
251-
252-
drop_subscription(
253-
group_name: group_name,
254-
target_conn_string: target_conn,
255-
)
256256
rescue => e
257257
restore_connections_on_source_db(group_name)
258258
abort_with("Switchover failed: #{e.message}")

spec/pg_easy_replicate/orchestrate_spec.rb

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,7 @@
209209
end
210210

211211
describe ".drop_subscription" do
212-
before do
213-
PgEasyReplicate.bootstrap({ group_name: "cluster1" })
214-
end
212+
before { PgEasyReplicate.bootstrap({ group_name: "cluster1" }) }
215213

216214
after do
217215
PgEasyReplicate.cleanup({ everything: true, group_name: "cluster1" })
@@ -234,7 +232,8 @@
234232
it "raises an error when user does not have sufficient privileges" do
235233
# Use a limited user for this test case
236234
restricted_user = "no_sup"
237-
restricted_user_target_connection_url = target_connection_url(restricted_user)
235+
restricted_user_target_connection_url =
236+
target_connection_url(restricted_user)
238237

239238
described_class.create_subscription(
240239
group_name: "cluster1",
@@ -250,7 +249,9 @@
250249
end.to raise_error(RuntimeError) { |e|
251250
expect(e.message).to include("Unable to drop subscription")
252251
}
253-
expect(pg_subscriptions(connection_url: target_connection_url)).not_to eq([])
252+
expect(pg_subscriptions(connection_url: target_connection_url)).not_to eq(
253+
[],
254+
)
254255
end
255256
end
256257

@@ -704,6 +705,51 @@
704705
{ last_analyze: nil, last_vacuum: nil, relname: "spatial_ref_sys" },
705706
)
706707
end
708+
709+
it "does not mark switchover complete if subscription drop fails" do
710+
conn1 =
711+
PgEasyReplicate::Query.connect(
712+
connection_url: connection_url,
713+
schema: test_schema,
714+
)
715+
conn1[:items].insert(name: "Foo1")
716+
expect(conn1[:items].first[:name]).to eq("Foo1")
717+
718+
conn2 =
719+
PgEasyReplicate::Query.connect(
720+
connection_url: target_connection_url,
721+
schema: test_schema,
722+
)
723+
expect(conn2[:items].first).to be_nil
724+
725+
ENV["SECONDARY_SOURCE_DB_URL"] = docker_compose_source_connection_url
726+
described_class.start_sync(
727+
group_name: "cluster1",
728+
schema_name: test_schema,
729+
recreate_indices_post_copy: true,
730+
)
731+
732+
conn1[:items].insert(name: "Foo2")
733+
734+
allow(described_class).to receive(:drop_subscription).and_raise(
735+
"Subscription drop failed",
736+
)
737+
738+
expect do
739+
described_class.switchover(
740+
group_name: "cluster1",
741+
source_conn_string: connection_url,
742+
target_conn_string: target_connection_url,
743+
skip_vacuum_analyze: true,
744+
)
745+
end.to raise_error("Switchover failed: Subscription drop failed")
746+
747+
described_class.restore_connections_on_source_db("cluster1")
748+
749+
expect(PgEasyReplicate::Group.find("cluster1")).to include(
750+
switchover_completed_at: nil,
751+
)
752+
end
707753
end
708754

709755
# Note: Hard to test for special roles that act as superuser which aren't superuser, like rds_superuser
@@ -769,17 +815,9 @@
769815
)
770816
end.to raise_error(/Starting sync failed: PG::InsufficientPrivilege/)
771817

772-
# expect(PgEasyReplicate::Group.find("cluster1")).to include(
773-
# switchover_completed_at: nil,
774-
# created_at: kind_of(Time),
775-
# name: "cluster1",
776-
# schema_name: "pger_test",
777-
# id: kind_of(Integer),
778-
# started_at: kind_of(Time),
779-
# updated_at: kind_of(Time),
780-
# failed_at: nil,
781-
# table_names: nil,
782-
# )
818+
expect(PgEasyReplicate::Group.find("cluster1")).to include(
819+
switchover_completed_at: nil,
820+
)
783821

784822
# conn1[:items].insert(name: "Foo2")
785823

0 commit comments

Comments
 (0)