diff --git a/src/bosh-director/lib/bosh/director/models/local_dns_record.rb b/src/bosh-director/lib/bosh/director/models/local_dns_record.rb index df9b964e273..3252e7f1527 100644 --- a/src/bosh-director/lib/bosh/director/models/local_dns_record.rb +++ b/src/bosh-director/lib/bosh/director/models/local_dns_record.rb @@ -3,7 +3,9 @@ class LocalDnsRecord < Sequel::Model(Bosh::Director::Config.db) many_to_one :instance def self.insert_tombstone - create(:ip => "#{SecureRandom.uuid}-tombstone") + tombstone_record = create(:ip => "#{SecureRandom.uuid}-tombstone") + where{id < tombstone_record.id}.where(Sequel.like(:ip, '%-tombstone')).delete + tombstone_record end def links=(value) diff --git a/src/bosh-director/spec/unit/models/local_dns_record_spec.rb b/src/bosh-director/spec/unit/models/local_dns_record_spec.rb index 5d3ad972b09..7148ecd71ff 100644 --- a/src/bosh-director/spec/unit/models/local_dns_record_spec.rb +++ b/src/bosh-director/spec/unit/models/local_dns_record_spec.rb @@ -11,5 +11,12 @@ module Bosh::Director::Models expect(Bosh::Director::Models::LocalDnsRecord.first.instance).to be_nil end + + it 'removes old tombstone records' do + previous_record = Bosh::Director::Models::LocalDnsRecord.insert_tombstone + new_record = Bosh::Director::Models::LocalDnsRecord.insert_tombstone + expect(Bosh::Director::Models::LocalDnsRecord.where(id: previous_record.id).first).to be_nil + expect(Bosh::Director::Models::LocalDnsRecord.where(id: new_record.id).first).not_to be_nil + end end end