Commit 79dc8171 authored by Robert Speicher's avatar Robert Speicher

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

Port of "2274-store-last-repository-updated-at" to CE

See merge request !11085
parents 91e8f770 6911baa1
...@@ -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) }
...@@ -357,4 +358,9 @@ class Event < ActiveRecord::Base ...@@ -357,4 +358,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
...@@ -53,6 +53,11 @@ class Project < ActiveRecord::Base ...@@ -53,6 +53,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
......
...@@ -183,6 +183,6 @@ class ProjectWiki ...@@ -183,6 +183,6 @@ 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
end end
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"
...@@ -971,6 +971,7 @@ ActiveRecord::Schema.define(version: 20170502091007) do ...@@ -971,6 +971,7 @@ ActiveRecord::Schema.define(version: 20170502091007) do
t.boolean "printing_merge_request_link_enabled", default: true, null: false t.boolean "printing_merge_request_link_enabled", default: true, null: false
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
...@@ -979,6 +980,7 @@ ActiveRecord::Schema.define(version: 20170502091007) do ...@@ -979,6 +980,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", ["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
add_index "projects", ["path"], name: "index_projects_on_path", using: :btree add_index "projects", ["path"], name: "index_projects_on_path", using: :btree
......
...@@ -351,6 +351,7 @@ Project: ...@@ -351,6 +351,7 @@ Project:
- auto_cancel_pending_pipelines - auto_cancel_pending_pipelines
- printing_merge_request_link_enabled - printing_merge_request_link_enabled
- build_allow_git_fetch - build_allow_git_fetch
- 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",
......
...@@ -1925,4 +1925,12 @@ describe Project, models: true do ...@@ -1925,4 +1925,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
...@@ -213,9 +213,12 @@ describe ProjectWiki, models: true do ...@@ -213,9 +213,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
...@@ -240,9 +243,12 @@ describe ProjectWiki, models: true do ...@@ -240,9 +243,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
...@@ -258,9 +264,12 @@ describe ProjectWiki, models: true do ...@@ -258,9 +264,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