Fixed issue #1649 - Tickets are deleted but database is still the same size.

This commit is contained in:
Martin Edenhofer 2017-11-17 11:41:44 +01:00
parent f627df1447
commit eb5ad7d9fa
4 changed files with 117 additions and 27 deletions

View file

@ -47,7 +47,7 @@ returns
data.delete('object') data.delete('object')
# store meta data # store meta data
store = Store.create(data) store = Store.create!(data)
store store
end end
@ -123,17 +123,18 @@ remove one attachment from storage
=end =end
def self.remove_item(store_id) def self.remove_item(store_id)
store = Store.find(store_id) store = Store.find(store_id)
file_id = store.store_file_id file_id = store.store_file_id
store.destroy
# check backend for references # check backend for references
files = Store.where(store_file_id: file_id) files = Store.where(store_file_id: file_id)
return if files.count != 1 if files.count > 1 || files.first.id != store.id
return if files.first.id != store.id store.destroy!
return true
end
Store::File.find(file_id).destroy store.destroy!
Store::File.find(file_id).destroy!
end end
=begin =begin

View file

@ -18,6 +18,7 @@ class Ticket::Article < ApplicationModel
store :preferences store :preferences
before_create :check_subject, :check_body, :check_message_id_md5 before_create :check_subject, :check_body, :check_message_id_md5
before_update :check_subject, :check_body, :check_message_id_md5 before_update :check_subject, :check_body, :check_message_id_md5
after_destroy :store_delete
sanitized_html :body sanitized_html :body
@ -316,6 +317,18 @@ returns
} }
end end
# delete attachments and mails of article
def store_delete
Store.remove(
object: 'Ticket::Article',
o_id: id,
)
Store.remove(
object: 'Ticket::Article::Mail',
o_id: id,
)
end
class Flag < ApplicationModel class Flag < ApplicationModel
end end

View file

@ -29,7 +29,7 @@ class StoreTest < ActiveSupport::TestCase
assert(exists) assert(exists)
end end
test 'store attachment' do test 'store attachment and move it between backends' do
files = [ files = [
{ {
data: 'hello world', data: 'hello world',
@ -54,7 +54,7 @@ class StoreTest < ActiveSupport::TestCase
] ]
files.each do |file| files.each do |file|
sha = Digest::SHA256.hexdigest( file[:data] ) sha = Digest::SHA256.hexdigest(file[:data])
# add attachments # add attachments
store = Store.add( store = Store.add(
@ -75,23 +75,23 @@ class StoreTest < ActiveSupport::TestCase
assert attachments assert attachments
# sha check # sha check
sha_new = Digest::SHA256.hexdigest( attachments[0].content ) sha_new = Digest::SHA256.hexdigest(attachments[0].content)
assert_equal( sha, sha_new, "check file #{file[:filename]}") assert_equal(sha, sha_new, "check file #{file[:filename]}")
# filename check # filename check
assert_equal( file[:filename], attachments[0].filename ) assert_equal(file[:filename], attachments[0].filename)
# provider check # provider check
assert_equal( 'DB', attachments[0].provider ) assert_equal('DB', attachments[0].provider)
end end
success = Store::File.verify success = Store::File.verify
assert success, 'verify ok' assert success, 'verify ok'
Store::File.move( 'DB', 'File' ) Store::File.move('DB', 'File')
files.each do |file| files.each do |file|
sha = Digest::SHA256.hexdigest( file[:data] ) sha = Digest::SHA256.hexdigest(file[:data])
# get list of attachments # get list of attachments
attachments = Store.list( attachments = Store.list(
@ -101,54 +101,55 @@ class StoreTest < ActiveSupport::TestCase
assert attachments assert attachments
# sha check # sha check
sha_new = Digest::SHA256.hexdigest( attachments[0].content ) sha_new = Digest::SHA256.hexdigest(attachments[0].content)
assert_equal( sha, sha_new, "check file #{file[:filename]}") assert_equal(sha, sha_new, "check file #{file[:filename]}")
# filename check # filename check
assert_equal( file[:filename], attachments[0].filename ) assert_equal(file[:filename], attachments[0].filename)
# provider check # provider check
assert_equal( 'File', attachments[0].provider ) assert_equal('File', attachments[0].provider)
end end
success = Store::File.verify success = Store::File.verify
assert success, 'verify ok' assert success, 'verify ok'
Store::File.move( 'File', 'DB' ) Store::File.move('File', 'DB')
files.each do |file| files.each do |file|
sha = Digest::SHA256.hexdigest( file[:data] ) sha = Digest::SHA256.hexdigest(file[:data])
# get list of attachments # get list of attachments
attachments = Store.list( attachments = Store.list(
object: 'Test', object: 'Test',
o_id: file[:o_id], o_id: file[:o_id],
) )
assert attachments assert(attachments)
assert_equal(attachments.count, 1)
# sha check # sha check
sha_new = Digest::SHA256.hexdigest( attachments[0].content ) sha_new = Digest::SHA256.hexdigest(attachments[0].content)
assert_equal( sha, sha_new, "check file #{file[:filename]}") assert_equal(sha, sha_new, "check file #{file[:filename]}")
# filename check # filename check
assert_equal( file[:filename], attachments[0].filename ) assert_equal(file[:filename], attachments[0].filename)
# provider check # provider check
assert_equal( 'DB', attachments[0].provider ) assert_equal('DB', attachments[0].provider)
# delete attachments # delete attachments
success = Store.remove( success = Store.remove(
object: 'Test', object: 'Test',
o_id: file[:o_id], o_id: file[:o_id],
) )
assert success assert(success)
# check attachments again # check attachments again
attachments = Store.list( attachments = Store.list(
object: 'Test', object: 'Test',
o_id: file[:o_id], o_id: file[:o_id],
) )
assert !attachments[0] assert_not(attachments[0])
end end
end end
end end

View file

@ -0,0 +1,75 @@
# encoding: utf-8
require 'test_helper'
class TicketArticleStoreEmpty < ActiveSupport::TestCase
test 'check if attachments are deleted after ticket is deleted' do
current_count = Store.count
current_file_count = Store::File.count
current_backend_count = Store::Provider::DB.count
email_raw_string = IO.binread('test/fixtures/mail1.box')
ticket, article, user, mail = Channel::EmailParser.new.process({}, email_raw_string)
next_count = Store.count
next_file_count = Store::File.count
next_backend_count = Store::Provider::DB.count
assert_equal(current_count, next_count - 2)
assert_equal(current_file_count, next_file_count - 2)
assert_equal(current_backend_count, next_backend_count - 2)
ticket.destroy!
after_count = Store.count
after_file_count = Store::File.count
after_backend_count = Store::Provider::DB.count
assert_equal(current_count, after_count)
assert_equal(current_file_count, after_file_count)
assert_equal(current_backend_count, after_backend_count)
end
test 'check if attachments are deleted after ticket same ticket 2 times is deleted' do
current_count = Store.count
current_file_count = Store::File.count
current_backend_count = Store::Provider::DB.count
email_raw_string = IO.binread('test/fixtures/mail1.box')
ticket1, article1, user1, mail1 = Channel::EmailParser.new.process({}, email_raw_string)
ticket2, article2, user2, mail2 = Channel::EmailParser.new.process({}, email_raw_string)
next_count = Store.count
next_file_count = Store::File.count
next_backend_count = Store::Provider::DB.count
assert_equal(current_count, next_count - 4)
assert_equal(current_file_count, next_file_count - 2)
assert_equal(current_backend_count, next_backend_count - 2)
ticket1.destroy!
next_count = Store.count
next_file_count = Store::File.count
next_backend_count = Store::Provider::DB.count
assert_equal(current_count, next_count - 2)
assert_equal(current_file_count, next_file_count - 2)
assert_equal(current_backend_count, next_backend_count - 2)
ticket2.destroy!
after_count = Store.count
after_file_count = Store::File.count
after_backend_count = Store::Provider::DB.count
assert_equal(current_count, after_count)
assert_equal(current_file_count, after_file_count)
assert_equal(current_backend_count, after_backend_count)
end
end