From 4c212d9ab1410eb7d849a01f24175167931c41b2 Mon Sep 17 00:00:00 2001 From: Varik Matevosyan Date: Wed, 1 May 2024 18:46:12 -0700 Subject: [PATCH] Add dissociate fork functionality for lantern resource --- model/lantern/lantern_resource.rb | 8 ++++++ routes/api/project/location/lantern.rb | 17 +++++++++--- spec/model/lantern/lantern_resource_spec.rb | 12 +++++++++ .../api/project/location/lantern_spec.rb | 26 ++++++++++++++++++- 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/model/lantern/lantern_resource.rb b/model/lantern/lantern_resource.rb index ab4426fd1..cfe14edf9 100644 --- a/model/lantern/lantern_resource.rb +++ b/model/lantern/lantern_resource.rb @@ -6,6 +6,7 @@ class LanternResource < Sequel::Model one_to_one :strand, key: :id many_to_one :project many_to_one :parent, key: :parent_id, class: self + one_to_many :forks, key: :parent_id, class: self one_to_many :servers, class: LanternServer, key: :resource_id one_to_one :representative_server, class: LanternServer, key: :resource_id, conditions: Sequel.~(representative_at: nil) one_through_one :timeline, class: LanternTimeline, join_table: :lantern_server, left_key: :resource_id, right_key: :timeline_id @@ -51,6 +52,13 @@ def required_standby_count required_standby_count_map[ha_type] end + def dissociate_forks + forks.each { + _1.update(parent_id: nil) + _1.timeline.update(parent_id: nil) + } + end + module HaType NONE = "none" ASYNC = "async" diff --git a/routes/api/project/location/lantern.rb b/routes/api/project/location/lantern.rb index a08973934..8ed4d0b52 100644 --- a/routes/api/project/location/lantern.rb +++ b/routes/api/project/location/lantern.rb @@ -20,6 +20,10 @@ class CloverApi r.delete true do Authorization.authorize(@current_user.id, "Postgres:delete", pg.id) + if pg.forks.size > 0 + response.status = 409 + return {"error" => "Can not delete resource which has active forks"} + end pg.incr_destroy response.status = 200 r.halt @@ -136,12 +140,17 @@ class CloverApi r.halt rescue => e Clog.emit("Error while pushing backup") { {error: e} } - if e.message.include? "Another backup" - response.status = 409 - response.write e.message + response.status = if e.message.include? "Another backup" + 409 else - response.status = 400 + 400 end + return {"error" => e.message} + end + + r.post "dissociate-forks" do + pg.dissociate_forks + response.status = 200 r.halt end end diff --git a/spec/model/lantern/lantern_resource_spec.rb b/spec/model/lantern/lantern_resource_spec.rb index 084d56621..c54356da4 100644 --- a/spec/model/lantern/lantern_resource_spec.rb +++ b/spec/model/lantern/lantern_resource_spec.rb @@ -65,4 +65,16 @@ expect(lantern_resource.required_standby_count).to eq(2) end end + + describe "#dissociate_forks" do + it "removes parents from forks" do + child = instance_double(described_class, parent_id: lantern_resource.id, timeline: instance_double(LanternTimeline, parent_id: "test")) + forks = [child] + expect(lantern_resource).to receive(:forks).and_return(forks) + expect(child).to receive(:update).with(parent_id: nil) + expect(child.timeline).to receive(:update).with(parent_id: nil) + + expect { lantern_resource.dissociate_forks }.not_to raise_error + end + end end diff --git a/spec/routes/api/project/location/lantern_spec.rb b/spec/routes/api/project/location/lantern_spec.rb index 7c5190ccd..9708e9b42 100644 --- a/spec/routes/api/project/location/lantern_spec.rb +++ b/spec/routes/api/project/location/lantern_spec.rb @@ -175,6 +175,18 @@ end describe "delete" do + it "does not delete instance if has forks" do + expect(Project).to receive(:from_ubid).and_return(project).at_least(:once) + query_res = class_double(LanternResource, first: pg) + allow(query_res).to receive(:where).and_return(query_res) + expect(project).to receive(:lantern_resources_dataset).and_return(query_res) + expect(pg).to receive(:forks).and_return([instance_double(LanternResource)]) + + delete "/api/project/#{project.ubid}/location/#{pg.location}/lantern/instance-1" + expect(last_response.status).to eq(409) + expect(JSON.parse(last_response.body)).to eq({"error" => "Can not delete resource which has active forks"}) + end + it "deletes instance" do delete "/api/project/#{project.ubid}/location/#{pg.location}/lantern/instance-1" expect(last_response.status).to eq(200) @@ -282,7 +294,7 @@ post "/api/project/#{project.ubid}/location/#{pg.location}/lantern/instance-1/push-backup" expect(last_response.status).to eq(409) - expect(last_response.body).to eq("Another backup is in progress please try again later") + expect(JSON.parse(last_response.body)).to eq({"error" => "Another backup is in progress please try again later"}) end it "fails to create new backup with unknown" do @@ -296,5 +308,17 @@ expect(last_response.status).to eq(400) end end + + describe "dissociate-forks" do + it "dissociate-forkses" do + expect(Project).to receive(:from_ubid).and_return(project).at_least(:once) + query_res = class_double(LanternResource, first: pg) + allow(query_res).to receive(:where).and_return(query_res) + expect(project).to receive(:lantern_resources_dataset).and_return(query_res) + expect(pg).to receive(:dissociate_forks) + post "/api/project/#{project.ubid}/location/#{pg.location}/lantern/instance-1/dissociate-forks" + expect(last_response.status).to eq(200) + end + end end end