summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKamil Trzcinski <ayufan@ayufan.eu>2015-09-16 16:59:54 +0200
committerKamil Trzcinski <ayufan@ayufan.eu>2015-09-16 16:59:54 +0200
commit912f470497129b0d8759b18700299e38535e7bec (patch)
tree47d58838d15fb6de49f2e3a335df43db9e5b0348
parent1b2a1d0ddd39ecc530c64563d0083e1e255f7c1a (diff)
downloadgitlab-ce-912f470497129b0d8759b18700299e38535e7bec.tar.gz
Fix ordering issue
-rw-r--r--app/models/ci/project.rb2
-rw-r--r--db/migrate/20150916145038_add_index_for_committed_at_and_id.rb5
-rw-r--r--db/schema.rb3
-rw-r--r--spec/models/ci/project_spec.rb18
4 files changed, 26 insertions, 2 deletions
diff --git a/app/models/ci/project.rb b/app/models/ci/project.rb
index 2cf1783616f..cd2692246b7 100644
--- a/app/models/ci/project.rb
+++ b/app/models/ci/project.rb
@@ -33,7 +33,7 @@ module Ci
belongs_to :gl_project, class_name: '::Project', foreign_key: :gitlab_id
- has_many :commits, ->() { order(:committed_at) }, dependent: :destroy, class_name: 'Ci::Commit'
+ has_many :commits, ->() { order('CASE WHEN commits.committed_at IS NULL THEN 0 ELSE 1 END', :committed_at, :id) }, dependent: :destroy, class_name: 'Ci::Commit'
has_many :builds, through: :commits, dependent: :destroy, class_name: 'Ci::Build'
has_many :runner_projects, dependent: :destroy, class_name: 'Ci::RunnerProject'
has_many :runners, through: :runner_projects, class_name: 'Ci::Runner'
diff --git a/db/migrate/20150916145038_add_index_for_committed_at_and_id.rb b/db/migrate/20150916145038_add_index_for_committed_at_and_id.rb
new file mode 100644
index 00000000000..78d9e5f61a1
--- /dev/null
+++ b/db/migrate/20150916145038_add_index_for_committed_at_and_id.rb
@@ -0,0 +1,5 @@
+class AddIndexForCommittedAtAndId < ActiveRecord::Migration
+ def change
+ add_index :ci_commits, [:project_id, :committed_at, :id]
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 5fd764bf698..48314b8db6a 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20150914215247) do
+ActiveRecord::Schema.define(version: 20150916145038) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -118,6 +118,7 @@ ActiveRecord::Schema.define(version: 20150914215247) do
t.datetime "committed_at"
end
+ add_index "ci_commits", ["project_id", "committed_at", "id"], name: "index_ci_commits_on_project_id_and_committed_at_and_id", using: :btree
add_index "ci_commits", ["project_id", "committed_at"], name: "index_ci_commits_on_project_id_and_committed_at", using: :btree
add_index "ci_commits", ["project_id", "sha"], name: "index_ci_commits_on_project_id_and_sha", using: :btree
add_index "ci_commits", ["project_id"], name: "index_ci_commits_on_project_id", using: :btree
diff --git a/spec/models/ci/project_spec.rb b/spec/models/ci/project_spec.rb
index 1025868da6e..a6fd6f27942 100644
--- a/spec/models/ci/project_spec.rb
+++ b/spec/models/ci/project_spec.rb
@@ -61,6 +61,24 @@ describe Ci::Project do
end
end
+ describe 'ordered commits' do
+ let (:project) { FactoryGirl.create :ci_project }
+
+ it 'returns ordered list of commits' do
+ commit1 = FactoryGirl.create :ci_commit, committed_at: 1.hour.ago, project: project
+ commit2 = FactoryGirl.create :ci_commit, committed_at: 2.hour.ago, project: project
+ project.commits.should == [commit2, commit1]
+ end
+
+ it 'returns commits ordered by committed_at and id, with nulls last' do
+ commit1 = FactoryGirl.create :ci_commit, committed_at: 1.hour.ago, project: project
+ commit2 = FactoryGirl.create :ci_commit, committed_at: nil, project: project
+ commit3 = FactoryGirl.create :ci_commit, committed_at: 2.hour.ago, project: project
+ commit4 = FactoryGirl.create :ci_commit, committed_at: nil, project: project
+ project.commits.should == [commit2, commit4, commit3, commit1]
+ end
+ end
+
context :valid_project do
let(:project) { FactoryGirl.create :ci_project }