summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Mazetto <brodock@gmail.com>2018-08-04 01:02:38 +0200
committerGabriel Mazetto <brodock@gmail.com>2018-08-16 19:57:27 +0200
commitfd9377fa1a4e1eaef280200430c47036d0e58d71 (patch)
tree1271f871d45664ef64c0b60f958a719fb2c72b82
parent76cd1dd68198bb464b12de7525ab067c6d4dc025 (diff)
downloadgitlab-ce-fd9377fa1a4e1eaef280200430c47036d0e58d71.tar.gz
Added method to re-populating SiteStatitiscs counter
-rw-r--r--app/models/site_statistic.rb16
-rw-r--r--spec/models/site_statistic_spec.rb12
2 files changed, 27 insertions, 1 deletions
diff --git a/app/models/site_statistic.rb b/app/models/site_statistic.rb
index daac1c57db9..2530a9d8b8e 100644
--- a/app/models/site_statistic.rb
+++ b/app/models/site_statistic.rb
@@ -49,7 +49,7 @@ class SiteStatistic < ActiveRecord::Base
#
# @return [SiteStatistic] record with tracked information
def self.fetch
- SiteStatistic.transaction(requires_new: true) do
+ transaction(requires_new: true) do
SiteStatistic.first_or_create!
end
rescue ActiveRecord::RecordNotUnique
@@ -73,4 +73,18 @@ class SiteStatistic < ActiveRecord::Base
super
end
+
+ def self.recalculate_counters!
+ transaction do
+ # see https://gitlab.com/gitlab-org/gitlab-ce/issues/48967
+ ActiveRecord::Base.connection.execute('SET LOCAL statement_timeout TO 0') if Gitlab::Database.postgresql?
+ self.update_all('repositories_count = (SELECT COUNT(*) FROM projects)')
+ end
+
+ transaction do
+ # see https://gitlab.com/gitlab-org/gitlab-ce/issues/48967
+ ActiveRecord::Base.connection.execute('SET LOCAL statement_timeout TO 0') if Gitlab::Database.postgresql?
+ self.update_all('wikis_count = (SELECT COUNT(*) FROM project_features WHERE wiki_access_level != 0)')
+ end
+ end
end
diff --git a/spec/models/site_statistic_spec.rb b/spec/models/site_statistic_spec.rb
index 9b056fbf332..49b54fb6994 100644
--- a/spec/models/site_statistic_spec.rb
+++ b/spec/models/site_statistic_spec.rb
@@ -80,4 +80,16 @@ describe SiteStatistic do
end
end
end
+
+ describe '.recalculate_counters!' do
+ it 'recalculates existing counters' do
+ create(:project)
+ described_class.fetch.update(repositories_count: 0, wikis_count: 0)
+
+ described_class.recalculate_counters!
+
+ expect(described_class.fetch.repositories_count).to eq(1)
+ expect(described_class.fetch.wikis_count).to eq(1)
+ end
+ end
end