From 1e17096f5fb1c107f5a850dd5f64c89ced6c13fb Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Wed, 11 Jan 2017 09:38:30 -0800 Subject: Memoize `get_all_versions` to reduce number of DB queries in tests Every time we access `ApplicationSetting` (e.g. when creating a user), we also run `ActiveRecord::Migrator.needs_migration`: ```ruby def needs_migration?(connection = Base.connection) (migrations(migrations_paths).collect(&:version) - get_all_versions(connection)).size > 0 end ``` During tests, we memoized the `migrations` call, saving a lot of filesystem loads. However, we left `get_all_versions` alone. This patch memoizes that as well to reduce DB and memory loads for that table. See https://gitlab.com/gitlab-org/gitlab-ce/issues/24899#note_21207429 --- config/initializers/ar_speed_up_migration_checking.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config/initializers/ar_speed_up_migration_checking.rb b/config/initializers/ar_speed_up_migration_checking.rb index 1fe5defc01d..d61e2230b54 100644 --- a/config/initializers/ar_speed_up_migration_checking.rb +++ b/config/initializers/ar_speed_up_migration_checking.rb @@ -5,6 +5,7 @@ if Rails.env.test? class Migrator class << self alias_method :migrations_unmemoized, :migrations + alias_method :get_all_versions_unmemoized, :get_all_versions # This method is called a large number of times per rspec example, and # it reads + parses `db/migrate/*` each time. Memoizing it can save 0.5 @@ -12,6 +13,10 @@ if Rails.env.test? def migrations(paths) @migrations ||= migrations_unmemoized(paths) end + + def get_all_versions(connection = Base.connection) + @all_versions ||= get_all_versions_unmemoized(connection) + end end end end -- cgit v1.2.1