Added db_strategy (:truncaction) meta helper to switch to DB cleanup via truncation (from transaction) for single examples. We choose to stick to Rails/RSpec transactional cleanup over DatabaseCleaner transaction strategy because of performance reasons. Rails default performs between 30 and 50% better.

This commit is contained in:
Thorsten Eckel 2018-10-02 16:25:04 +02:00
parent 44e89271fe
commit 02ba0e455e
6 changed files with 29 additions and 25 deletions

View file

@ -0,0 +1,13 @@
namespace :zammad do
namespace :db do
desc 'Truncates and reseeds the database, clears the Cache and reloads the Settings'
task reset: :environment do
DatabaseCleaner.clean_with(:truncation)
Rails.application.load_seed
Cache.clear
Setting.reload
end
end
end

View file

@ -3,13 +3,12 @@ require 'rails_helper'
RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do
context 'no online_notifications foreign key' do context 'no online_notifications foreign key' do
self.use_transactional_tests = false
let(:existing_user_id) { User.first.id } let(:existing_user_id) { User.first.id }
context 'invalid User foreign key columns' do context 'invalid User foreign key columns' do
it 'cleans up OnlineNotification#user_id' do it 'cleans up OnlineNotification#user_id', db_strategy: :truncation do
witout_foreign_key(:online_notifications, column: :user_id) witout_foreign_key(:online_notifications, column: :user_id)
create(:online_notification, user_id: 1337) create(:online_notification, user_id: 1337)
@ -20,13 +19,9 @@ RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do
end.to change { end.to change {
OnlineNotification.count OnlineNotification.count
}.by(-1) }.by(-1)
# cleanup since we disabled
# transactions for this tests
valid.destroy
end end
it 'cleans up RecentView#created_by_id' do it 'cleans up RecentView#created_by_id', db_strategy: :truncation do
witout_foreign_key(:online_notifications, column: :user_id) witout_foreign_key(:online_notifications, column: :user_id)
witout_foreign_key(:recent_views, column: :created_by_id) witout_foreign_key(:recent_views, column: :created_by_id)
@ -38,13 +33,9 @@ RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do
end.to change { end.to change {
RecentView.count RecentView.count
}.by(-1) }.by(-1)
# cleanup since we disabled
# transactions for this tests
valid.destroy
end end
it 'cleans up Avatar#o_id' do it 'cleans up Avatar#o_id', db_strategy: :truncation do
witout_foreign_key(:online_notifications, column: :user_id) witout_foreign_key(:online_notifications, column: :user_id)
create(:avatar, object_lookup_id: ObjectLookup.by_name('User'), o_id: 1337) create(:avatar, object_lookup_id: ObjectLookup.by_name('User'), o_id: 1337)
@ -56,11 +47,6 @@ RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do
end.to change { end.to change {
Avatar.count Avatar.count
}.by(-1) }.by(-1)
# cleanup since we disabled
# transactions for this tests
valid_ticket.destroy
valid_user.destroy
end end
end end

View file

@ -0,0 +1,8 @@
RSpec.configure do |config|
config.around(:each, db_strategy: :truncation) do |example|
self.use_transactional_tests = false
example.run
Rake::Task['zammad:db:reset'].execute
end
end

4
spec/support/rake.rb Normal file
View file

@ -0,0 +1,4 @@
require 'rake'
Rake::Task.clear
Zammad::Application.load_tasks

View file

@ -1,8 +1,5 @@
RSpec.configure do |config| RSpec.configure do |config|
config.before(:suite) do config.before(:suite) do
DatabaseCleaner.clean_with(:truncation) Rake::Task['zammad:db:reset'].invoke
Rails.application.load_seed
Cache.clear
Setting.reload
end end
end end

View file

@ -1,5 +1,3 @@
require 'rake'
module SearchindexBackendHelper module SearchindexBackendHelper
def configure_elasticsearch(required: false) def configure_elasticsearch(required: false)
@ -32,8 +30,6 @@ module SearchindexBackendHelper
end end
def rebuild_searchindex def rebuild_searchindex
Rake::Task.clear
Zammad::Application.load_tasks
Rake::Task['searchindex:rebuild'].execute Rake::Task['searchindex:rebuild'].execute
end end