Commit ad665709 authored by Robert Speicher's avatar Robert Speicher

Merge branch '2274-store-last-repository-updated-at' into 'master'

Store the last time a repository was updated

See merge request !1788
parents 3fddf612 df9976f3
...@@ -30,6 +30,7 @@ class Event < ActiveRecord::Base ...@@ -30,6 +30,7 @@ class Event < ActiveRecord::Base
# Callbacks # Callbacks
after_create :reset_project_activity after_create :reset_project_activity
after_create :set_last_repository_updated_at, if: :push?
# Scopes # Scopes
scope :recent, -> { reorder(id: :desc) } scope :recent, -> { reorder(id: :desc) }
...@@ -363,4 +364,9 @@ class Event < ActiveRecord::Base ...@@ -363,4 +364,9 @@ class Event < ActiveRecord::Base
def recent_update? def recent_update?
project.last_activity_at > RESET_PROJECT_ACTIVITY_INTERVAL.ago project.last_activity_at > RESET_PROJECT_ACTIVITY_INTERVAL.ago
end end
def set_last_repository_updated_at
Project.unscoped.where(id: project_id).
update_all(last_repository_updated_at: created_at)
end
end end
...@@ -56,6 +56,11 @@ class Project < ActiveRecord::Base ...@@ -56,6 +56,11 @@ class Project < ActiveRecord::Base
update_column(:last_activity_at, self.created_at) update_column(:last_activity_at, self.created_at)
end end
after_create :set_last_repository_updated_at
def set_last_repository_updated_at
update_column(:last_repository_updated_at, self.created_at)
end
after_destroy :remove_pages after_destroy :remove_pages
# update visibility_level of forks # update visibility_level of forks
......
...@@ -196,7 +196,7 @@ class ProjectWiki ...@@ -196,7 +196,7 @@ class ProjectWiki
end end
def update_project_activity def update_project_activity
@project.touch(:last_activity_at) @project.touch(:last_activity_at, :last_repository_updated_at)
end end
def update_elastic_index def update_elastic_index
......
class AddLastRepositoryUpdatedAtToProjects < ActiveRecord::Migration
DOWNTIME = false
def change
add_column :projects, :last_repository_updated_at, :datetime
end
end
class AddIndexToLastRepositoryUpdatedAtOnProjects < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index(:projects, :last_repository_updated_at)
end
def down
remove_concurrent_index(:projects, :last_repository_updated_at) if index_exists?(:projects, :last_repository_updated_at)
end
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170502091007) do ActiveRecord::Schema.define(version: 20170503004425) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
...@@ -1126,6 +1126,7 @@ ActiveRecord::Schema.define(version: 20170502091007) do ...@@ -1126,6 +1126,7 @@ ActiveRecord::Schema.define(version: 20170502091007) do
t.boolean "service_desk_enabled" t.boolean "service_desk_enabled"
t.string "import_jid" t.string "import_jid"
t.integer "cached_markdown_version" t.integer "cached_markdown_version"
t.datetime "last_repository_updated_at"
end end
add_index "projects", ["ci_id"], name: "index_projects_on_ci_id", using: :btree add_index "projects", ["ci_id"], name: "index_projects_on_ci_id", using: :btree
...@@ -1134,6 +1135,7 @@ ActiveRecord::Schema.define(version: 20170502091007) do ...@@ -1134,6 +1135,7 @@ ActiveRecord::Schema.define(version: 20170502091007) do
add_index "projects", ["description"], name: "index_projects_on_description_trigram", using: :gin, opclasses: {"description"=>"gin_trgm_ops"} add_index "projects", ["description"], name: "index_projects_on_description_trigram", using: :gin, opclasses: {"description"=>"gin_trgm_ops"}
add_index "projects", ["last_activity_at"], name: "index_projects_on_last_activity_at", using: :btree add_index "projects", ["last_activity_at"], name: "index_projects_on_last_activity_at", using: :btree
add_index "projects", ["last_repository_check_failed"], name: "index_projects_on_last_repository_check_failed", using: :btree add_index "projects", ["last_repository_check_failed"], name: "index_projects_on_last_repository_check_failed", using: :btree
add_index "projects", ["last_repository_updated_at"], name: "index_projects_on_last_repository_updated_at", using: :btree
add_index "projects", ["mirror_last_successful_update_at"], name: "index_projects_on_mirror_last_successful_update_at", using: :btree add_index "projects", ["mirror_last_successful_update_at"], name: "index_projects_on_mirror_last_successful_update_at", using: :btree
add_index "projects", ["name"], name: "index_projects_on_name_trigram", using: :gin, opclasses: {"name"=>"gin_trgm_ops"} add_index "projects", ["name"], name: "index_projects_on_name_trigram", using: :gin, opclasses: {"name"=>"gin_trgm_ops"}
add_index "projects", ["namespace_id"], name: "index_projects_on_namespace_id", using: :btree add_index "projects", ["namespace_id"], name: "index_projects_on_namespace_id", using: :btree
......
...@@ -364,6 +364,7 @@ Project: ...@@ -364,6 +364,7 @@ Project:
- repository_size_limit - repository_size_limit
- sync_time - sync_time
- service_desk_enabled - service_desk_enabled
- last_repository_updated_at
Author: Author:
- name - name
ProjectFeature: ProjectFeature:
......
...@@ -15,13 +15,39 @@ describe Event, models: true do ...@@ -15,13 +15,39 @@ describe Event, models: true do
end end
describe 'Callbacks' do describe 'Callbacks' do
describe 'after_create :reset_project_activity' do let(:project) { create(:empty_project) }
let(:project) { create(:empty_project) }
describe 'after_create :reset_project_activity' do
it 'calls the reset_project_activity method' do it 'calls the reset_project_activity method' do
expect_any_instance_of(described_class).to receive(:reset_project_activity) expect_any_instance_of(described_class).to receive(:reset_project_activity)
create_event(project, project.owner) create_push_event(project, project.owner)
end
end
describe 'after_create :set_last_repository_updated_at' do
context 'with a push event' do
it 'updates the project last_repository_updated_at' do
project.update(last_repository_updated_at: 1.year.ago)
create_push_event(project, project.owner)
project.reload
expect(project.last_repository_updated_at).to be_within(1.minute).of(Time.now)
end
end
context 'without a push event' do
it 'does not update the project last_repository_updated_at' do
project.update(last_repository_updated_at: 1.year.ago)
create(:closed_issue_event, project: project, author: project.owner)
project.reload
expect(project.last_repository_updated_at).to be_within(1.minute).of(1.year.ago)
end
end end
end end
end end
...@@ -29,7 +55,7 @@ describe Event, models: true do ...@@ -29,7 +55,7 @@ describe Event, models: true do
describe "Push event" do describe "Push event" do
let(:project) { create(:empty_project, :private) } let(:project) { create(:empty_project, :private) }
let(:user) { project.owner } let(:user) { project.owner }
let(:event) { create_event(project, user) } let(:event) { create_push_event(project, user) }
it do it do
expect(event.push?).to be_truthy expect(event.push?).to be_truthy
...@@ -243,7 +269,7 @@ describe Event, models: true do ...@@ -243,7 +269,7 @@ describe Event, models: true do
expect(project).not_to receive(:update_column). expect(project).not_to receive(:update_column).
with(:last_activity_at, a_kind_of(Time)) with(:last_activity_at, a_kind_of(Time))
create_event(project, project.owner) create_push_event(project, project.owner)
end end
end end
...@@ -251,11 +277,11 @@ describe Event, models: true do ...@@ -251,11 +277,11 @@ describe Event, models: true do
it 'updates the project' do it 'updates the project' do
project.update(last_activity_at: 1.year.ago) project.update(last_activity_at: 1.year.ago)
create_event(project, project.owner) create_push_event(project, project.owner)
project.reload project.reload
project.last_activity_at <= 1.minute.ago expect(project.last_activity_at).to be_within(1.minute).of(Time.now)
end end
end end
end end
...@@ -278,7 +304,7 @@ describe Event, models: true do ...@@ -278,7 +304,7 @@ describe Event, models: true do
end end
end end
def create_event(project, user, attrs = {}) def create_push_event(project, user, attrs = {})
data = { data = {
before: Gitlab::Git::BLANK_SHA, before: Gitlab::Git::BLANK_SHA,
after: "0220c11b9a3e6c69dc8fd35321254ca9a7b98f7e", after: "0220c11b9a3e6c69dc8fd35321254ca9a7b98f7e",
......
...@@ -2340,4 +2340,12 @@ describe Project, models: true do ...@@ -2340,4 +2340,12 @@ describe Project, models: true do
not_to raise_error not_to raise_error
end end
end end
describe '#last_repository_updated_at' do
it 'sets to created_at upon creation' do
project = create(:empty_project, created_at: 2.hours.ago)
expect(project.last_repository_updated_at.to_i).to eq(project.created_at.to_i)
end
end
end end
...@@ -221,9 +221,12 @@ describe ProjectWiki, models: true do ...@@ -221,9 +221,12 @@ describe ProjectWiki, models: true do
end end
it 'updates project activity' do it 'updates project activity' do
expect(subject).to receive(:update_project_activity)
subject.create_page('Test Page', 'This is content') subject.create_page('Test Page', 'This is content')
project.reload
expect(project.last_activity_at).to be_within(1.minute).of(Time.now)
expect(project.last_repository_updated_at).to be_within(1.minute).of(Time.now)
end end
end end
...@@ -248,9 +251,12 @@ describe ProjectWiki, models: true do ...@@ -248,9 +251,12 @@ describe ProjectWiki, models: true do
end end
it 'updates project activity' do it 'updates project activity' do
expect(subject).to receive(:update_project_activity)
subject.update_page(@gollum_page, 'Yet more content', :markdown, 'Updated page again') subject.update_page(@gollum_page, 'Yet more content', :markdown, 'Updated page again')
project.reload
expect(project.last_activity_at).to be_within(1.minute).of(Time.now)
expect(project.last_repository_updated_at).to be_within(1.minute).of(Time.now)
end end
end end
...@@ -266,9 +272,12 @@ describe ProjectWiki, models: true do ...@@ -266,9 +272,12 @@ describe ProjectWiki, models: true do
end end
it 'updates project activity' do it 'updates project activity' do
expect(subject).to receive(:update_project_activity)
subject.delete_page(@page) subject.delete_page(@page)
project.reload
expect(project.last_activity_at).to be_within(1.minute).of(Time.now)
expect(project.last_repository_updated_at).to be_within(1.minute).of(Time.now)
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment