Commit c0160d008c0e39ba92ab3494f1513fd10fcd7b7e

Authored by Dmitriy Zaporozhets
2 parents aa8d4d9f 69c8231b

Merge pull request #2304 from gitlabhq/fix_postgres_notes

Split commit_id and noteable_id for Note
@@ -19,8 +19,7 @@ services: @@ -19,8 +19,7 @@ services:
19 before_script: 19 before_script:
20 - "cp config/database.yml.$DB config/database.yml" 20 - "cp config/database.yml.$DB config/database.yml"
21 - "cp config/gitlab.yml.example config/gitlab.yml" 21 - "cp config/gitlab.yml.example config/gitlab.yml"
22 - - "bundle exec rake db:create RAILS_ENV=test"  
23 - - "bundle exec rake db:migrate RAILS_ENV=test" 22 + - "bundle exec rake db:setup RAILS_ENV=test"
24 - "bundle exec rake db:seed_fu RAILS_ENV=test" 23 - "bundle exec rake db:seed_fu RAILS_ENV=test"
25 - "sh -e /etc/init.d/xvfb start" 24 - "sh -e /etc/init.d/xvfb start"
26 script: "bundle exec rake travis --trace" 25 script: "bundle exec rake travis --trace"
@@ -124,7 +124,7 @@ group :development, :test do @@ -124,7 +124,7 @@ group :development, :test do
124 gem "capybara" 124 gem "capybara"
125 gem "pry" 125 gem "pry"
126 gem "awesome_print" 126 gem "awesome_print"
127 - gem "database_cleaner" 127 + gem "database_cleaner", ref: "f89c34300e114be99532f14c115b2799a3380ac6", git: "https://github.com/bmabey/database_cleaner.git"
128 gem "launchy" 128 gem "launchy"
129 gem 'factory_girl_rails' 129 gem 'factory_girl_rails'
130 130
1 GIT 1 GIT
  2 + remote: https://github.com/bmabey/database_cleaner.git
  3 + revision: f89c34300e114be99532f14c115b2799a3380ac6
  4 + ref: f89c34300e114be99532f14c115b2799a3380ac6
  5 + specs:
  6 + database_cleaner (0.9.1)
  7 +
  8 +GIT
2 remote: https://github.com/ctran/annotate_models.git 9 remote: https://github.com/ctran/annotate_models.git
3 revision: be4e26825b521f0b2d86b181e2dff89901aa9b1e 10 revision: be4e26825b521f0b2d86b181e2dff89901aa9b1e
4 specs: 11 specs:
@@ -140,7 +147,6 @@ GEM @@ -140,7 +147,6 @@ GEM
140 colorize (0.5.8) 147 colorize (0.5.8)
141 crack (0.3.1) 148 crack (0.3.1)
142 daemons (1.1.9) 149 daemons (1.1.9)
143 - database_cleaner (0.9.1)  
144 devise (2.1.2) 150 devise (2.1.2)
145 bcrypt-ruby (~> 3.0) 151 bcrypt-ruby (~> 3.0)
146 orm_adapter (~> 0.1) 152 orm_adapter (~> 0.1)
@@ -458,7 +464,7 @@ DEPENDENCIES @@ -458,7 +464,7 @@ DEPENDENCIES
458 chosen-rails (= 0.9.8) 464 chosen-rails (= 0.9.8)
459 coffee-rails (~> 3.2.2) 465 coffee-rails (~> 3.2.2)
460 colored 466 colored
461 - database_cleaner 467 + database_cleaner!
462 devise (~> 2.1.0) 468 devise (~> 2.1.0)
463 draper (~> 0.18.0) 469 draper (~> 0.18.0)
464 email_spec 470 email_spec
app/models/merge_request.rb
@@ -204,7 +204,7 @@ class MergeRequest < ActiveRecord::Base @@ -204,7 +204,7 @@ class MergeRequest < ActiveRecord::Base
204 204
205 def mr_and_commit_notes 205 def mr_and_commit_notes
206 commit_ids = commits.map(&:id) 206 commit_ids = commits.map(&:id)
207 - Note.where("(noteable_type = 'MergeRequest' AND noteable_id = :mr_id) OR (noteable_type = 'Commit' AND noteable_id IN (:commit_ids))", mr_id: id, commit_ids: commit_ids) 207 + Note.where("(noteable_type = 'MergeRequest' AND noteable_id = :mr_id) OR (noteable_type = 'Commit' AND commit_id IN (:commit_ids))", mr_id: id, commit_ids: commit_ids)
208 end 208 end
209 209
210 # Returns the raw diff for this merge request 210 # Returns the raw diff for this merge request
app/models/note.rb
@@ -20,7 +20,7 @@ require 'file_size_validator' @@ -20,7 +20,7 @@ require 'file_size_validator'
20 class Note < ActiveRecord::Base 20 class Note < ActiveRecord::Base
21 21
22 attr_accessible :note, :noteable, :noteable_id, :noteable_type, :project_id, 22 attr_accessible :note, :noteable, :noteable_id, :noteable_type, :project_id,
23 - :attachment, :line_code 23 + :attachment, :line_code, :commit_id
24 24
25 attr_accessor :notify 25 attr_accessor :notify
26 attr_accessor :notify_author 26 attr_accessor :notify_author
@@ -35,10 +35,14 @@ class Note &lt; ActiveRecord::Base @@ -35,10 +35,14 @@ class Note &lt; ActiveRecord::Base
35 validates :note, :project, presence: true 35 validates :note, :project, presence: true
36 validates :attachment, file_size: { maximum: 10.megabytes.to_i } 36 validates :attachment, file_size: { maximum: 10.megabytes.to_i }
37 37
  38 + validates :noteable_id, presence: true, if: ->(n) { n.noteable_type.present? && n.noteable_type != 'Commit' }
  39 + validates :commit_id, presence: true, if: ->(n) { n.noteable_type == 'Commit' }
  40 +
38 mount_uploader :attachment, AttachmentUploader 41 mount_uploader :attachment, AttachmentUploader
39 42
40 # Scopes 43 # Scopes
41 - scope :common, ->{ where(noteable_id: nil) } 44 + scope :for_commits, ->{ where(noteable_type: "Commit") }
  45 + scope :common, ->{ where(noteable_id: nil, commit_id: nil) }
42 scope :today, ->{ where("created_at >= :date", date: Date.today) } 46 scope :today, ->{ where("created_at >= :date", date: Date.today) }
43 scope :last_week, ->{ where("created_at >= :date", date: (Date.today - 7.days)) } 47 scope :last_week, ->{ where("created_at >= :date", date: (Date.today - 7.days)) }
44 scope :since, ->(day) { where("created_at >= :date", date: (day)) } 48 scope :since, ->(day) { where("created_at >= :date", date: (day)) }
@@ -66,7 +70,7 @@ class Note &lt; ActiveRecord::Base @@ -66,7 +70,7 @@ class Note &lt; ActiveRecord::Base
66 # override to return commits, which are not active record 70 # override to return commits, which are not active record
67 def noteable 71 def noteable
68 if for_commit? 72 if for_commit?
69 - project.commit(noteable_id) 73 + project.commit(commit_id)
70 else 74 else
71 super 75 super
72 end 76 end
app/models/project.rb
@@ -203,15 +203,15 @@ class Project &lt; ActiveRecord::Base @@ -203,15 +203,15 @@ class Project &lt; ActiveRecord::Base
203 end 203 end
204 204
205 def build_commit_note(commit) 205 def build_commit_note(commit)
206 - notes.new(noteable_id: commit.id, noteable_type: "Commit") 206 + notes.new(commit_id: commit.id, noteable_type: "Commit")
207 end 207 end
208 208
209 def commit_notes(commit) 209 def commit_notes(commit)
210 - notes.where(noteable_id: commit.id, noteable_type: "Commit", line_code: nil) 210 + notes.where(commit_id: commit.id, noteable_type: "Commit", line_code: nil)
211 end 211 end
212 212
213 def commit_line_notes(commit) 213 def commit_line_notes(commit)
214 - notes.where(noteable_id: commit.id, noteable_type: "Commit").where("line_code IS NOT NULL") 214 + notes.where(commit_id: commit.id, noteable_type: "Commit").where("line_code IS NOT NULL")
215 end 215 end
216 216
217 def public? 217 def public?
app/roles/note_event.rb
1 module NoteEvent 1 module NoteEvent
2 def note_commit_id 2 def note_commit_id
3 - target.noteable_id 3 + target.commit_id
4 end 4 end
5 5
6 def note_short_commit_id 6 def note_short_commit_id
@@ -16,7 +16,11 @@ module NoteEvent @@ -16,7 +16,11 @@ module NoteEvent
16 end 16 end
17 17
18 def note_target_id 18 def note_target_id
19 - target.noteable_id 19 + if note_commit?
  20 + target.commit_id
  21 + else
  22 + target.noteable_id.to_s
  23 + end
20 end 24 end
21 25
22 def wall_note? 26 def wall_note?
app/views/notes/_common_form.html.haml
@@ -7,6 +7,7 @@ @@ -7,6 +7,7 @@
7 %div= msg 7 %div= msg
8 8
9 = f.hidden_field :noteable_id 9 = f.hidden_field :noteable_id
  10 + = f.hidden_field :commit_id
10 = f.hidden_field :noteable_type 11 = f.hidden_field :noteable_type
11 = f.text_area :note, size: 255, class: 'note-text js-gfm-input' 12 = f.text_area :note, size: 255, class: 'note-text js-gfm-input'
12 #preview-note.preview_note.hide 13 #preview-note.preview_note.hide
app/views/notes/_per_line_form.html.haml
@@ -11,6 +11,7 @@ @@ -11,6 +11,7 @@
11 %div= msg 11 %div= msg
12 12
13 = f.hidden_field :noteable_id 13 = f.hidden_field :noteable_id
  14 + = f.hidden_field :commit_id
14 = f.hidden_field :noteable_type 15 = f.hidden_field :noteable_type
15 = f.hidden_field :line_code 16 = f.hidden_field :line_code
16 = f.text_area :note, size: 255, class: 'line-note-text js-gfm-input' 17 = f.text_area :note, size: 255, class: 'line-note-text js-gfm-input'
config/database.yml.postgresql
@@ -9,6 +9,7 @@ production: @@ -9,6 +9,7 @@ production:
9 username: postgres 9 username: postgres
10 password: 10 password:
11 # host: localhost 11 # host: localhost
  12 + # port: 5432
12 # socket: /tmp/postgresql.sock 13 # socket: /tmp/postgresql.sock
13 14
14 # 15 #
db/migrate/20121218164840_move_noteable_commit_to_own_field.rb 0 → 100644
@@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
  1 +class MoveNoteableCommitToOwnField < ActiveRecord::Migration
  2 + def up
  3 + add_column :notes, :commit_id, :string, null: true
  4 + add_column :notes, :new_noteable_id, :integer, null: true
  5 + Note.where(noteable_type: 'Commit').update_all('commit_id = noteable_id')
  6 +
  7 + if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
  8 + Note.where("noteable_type != 'Commit'").update_all('new_noteable_id = CAST (noteable_id AS INTEGER)')
  9 + else
  10 + Note.where("noteable_type != 'Commit'").update_all('new_noteable_id = noteable_id')
  11 + end
  12 +
  13 + remove_column :notes, :noteable_id
  14 + rename_column :notes, :new_noteable_id, :noteable_id
  15 + end
  16 +
  17 + def down
  18 + raise ActiveRecord::IrreversibleMigration
  19 + end
  20 +end
db/migrate/20121219095402_indices_for_notes.rb 0 → 100644
@@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
  1 +class IndicesForNotes < ActiveRecord::Migration
  2 + def change
  3 + add_index :notes, :commit_id
  4 + add_index :notes, [:project_id, :noteable_type]
  5 + end
  6 +end
@@ -11,7 +11,7 @@ @@ -11,7 +11,7 @@
11 # 11 #
12 # It's strongly recommended to check this file into your version control system. 12 # It's strongly recommended to check this file into your version control system.
13 13
14 -ActiveRecord::Schema.define(:version => 20121205201726) do 14 +ActiveRecord::Schema.define(:version => 20121219095402) do
15 15
16 create_table "events", :force => true do |t| 16 create_table "events", :force => true do |t|
17 t.string "target_type" 17 t.string "target_type"
@@ -124,7 +124,6 @@ ActiveRecord::Schema.define(:version =&gt; 20121205201726) do @@ -124,7 +124,6 @@ ActiveRecord::Schema.define(:version =&gt; 20121205201726) do
124 124
125 create_table "notes", :force => true do |t| 125 create_table "notes", :force => true do |t|
126 t.text "note" 126 t.text "note"
127 - t.string "noteable_id"  
128 t.string "noteable_type" 127 t.string "noteable_type"
129 t.integer "author_id" 128 t.integer "author_id"
130 t.datetime "created_at", :null => false 129 t.datetime "created_at", :null => false
@@ -132,11 +131,14 @@ ActiveRecord::Schema.define(:version =&gt; 20121205201726) do @@ -132,11 +131,14 @@ ActiveRecord::Schema.define(:version =&gt; 20121205201726) do
132 t.integer "project_id" 131 t.integer "project_id"
133 t.string "attachment" 132 t.string "attachment"
134 t.string "line_code" 133 t.string "line_code"
  134 + t.string "commit_id"
  135 + t.integer "noteable_id"
135 end 136 end
136 137
  138 + add_index "notes", ["commit_id"], :name => "index_notes_on_commit_id"
137 add_index "notes", ["created_at"], :name => "index_notes_on_created_at" 139 add_index "notes", ["created_at"], :name => "index_notes_on_created_at"
138 - add_index "notes", ["noteable_id"], :name => "index_notes_on_noteable_id"  
139 add_index "notes", ["noteable_type"], :name => "index_notes_on_noteable_type" 140 add_index "notes", ["noteable_type"], :name => "index_notes_on_noteable_type"
  141 + add_index "notes", ["project_id", "noteable_type"], :name => "index_notes_on_project_id_and_noteable_type"
140 add_index "notes", ["project_id"], :name => "index_notes_on_project_id" 142 add_index "notes", ["project_id"], :name => "index_notes_on_project_id"
141 143
142 create_table "projects", :force => true do |t| 144 create_table "projects", :force => true do |t|
features/support/env.rb
@@ -36,8 +36,6 @@ Spinach.hooks.before_scenario do @@ -36,8 +36,6 @@ Spinach.hooks.before_scenario do
36 Gitlab.config.stub(git_base_path: Rails.root.join('tmp', 'test-git-base-path')) 36 Gitlab.config.stub(git_base_path: Rails.root.join('tmp', 'test-git-base-path'))
37 FileUtils.rm_rf Gitlab.config.git_base_path 37 FileUtils.rm_rf Gitlab.config.git_base_path
38 FileUtils.mkdir_p Gitlab.config.git_base_path 38 FileUtils.mkdir_p Gitlab.config.git_base_path
39 -  
40 - DatabaseCleaner.start  
41 end 39 end
42 40
43 Spinach.hooks.after_scenario do 41 Spinach.hooks.after_scenario do
spec/models/merge_request_spec.rb
@@ -42,7 +42,7 @@ describe MergeRequest do @@ -42,7 +42,7 @@ describe MergeRequest do
42 42
43 before do 43 before do
44 merge_request.stub(:commits) { [merge_request.project.commit] } 44 merge_request.stub(:commits) { [merge_request.project.commit] }
45 - create(:note, noteable: merge_request.commits.first) 45 + create(:note, commit_id: merge_request.commits.first.id, noteable_type: 'Commit')
46 create(:note, noteable: merge_request) 46 create(:note, noteable: merge_request)
47 end 47 end
48 48
spec/models/note_spec.rb
@@ -81,18 +81,18 @@ describe Note do @@ -81,18 +81,18 @@ describe Note do
81 describe "Commit notes" do 81 describe "Commit notes" do
82 before do 82 before do
83 @note = create(:note, 83 @note = create(:note,
84 - noteable_id: commit.id, 84 + commit_id: commit.id,
85 noteable_type: "Commit") 85 noteable_type: "Commit")
86 end 86 end
87 87
88 it "should be accessible through #noteable" do 88 it "should be accessible through #noteable" do
89 - @note.noteable_id.should == commit.id 89 + @note.commit_id.should == commit.id
90 @note.noteable.should be_a(Commit) 90 @note.noteable.should be_a(Commit)
91 @note.noteable.should == commit 91 @note.noteable.should == commit
92 end 92 end
93 93
94 it "should save a valid note" do 94 it "should save a valid note" do
95 - @note.noteable_id.should == commit.id 95 + @note.commit_id.should == commit.id
96 @note.noteable == commit 96 @note.noteable == commit
97 end 97 end
98 98
@@ -104,13 +104,13 @@ describe Note do @@ -104,13 +104,13 @@ describe Note do
104 describe "Pre-line commit notes" do 104 describe "Pre-line commit notes" do
105 before do 105 before do
106 @note = create(:note, 106 @note = create(:note,
107 - noteable_id: commit.id, 107 + commit_id: commit.id,
108 noteable_type: "Commit", 108 noteable_type: "Commit",
109 line_code: "0_16_1") 109 line_code: "0_16_1")
110 end 110 end
111 111
112 it "should save a valid note" do 112 it "should save a valid note" do
113 - @note.noteable_id.should == commit.id 113 + @note.commit_id.should == commit.id
114 @note.noteable.id.should == commit.id 114 @note.noteable.id.should == commit.id
115 end 115 end
116 116
spec/requests/issues_spec.rb
@@ -91,13 +91,13 @@ describe &quot;Issues&quot; do @@ -91,13 +91,13 @@ describe &quot;Issues&quot; do
91 title: title) 91 title: title)
92 end 92 end
93 93
94 - issue = Issue.first # with title 'foobar'  
95 - issue.milestone = create(:milestone, project: project)  
96 - issue.assignee = nil  
97 - issue.save 94 + @issue = Issue.first # with title 'foobar'
  95 + @issue.milestone = create(:milestone, project: project)
  96 + @issue.assignee = nil
  97 + @issue.save
98 end 98 end
99 99
100 - let(:issue) { Issue.first } 100 + let(:issue) { @issue }
101 101
102 it "should allow filtering by issues with no specified milestone" do 102 it "should allow filtering by issues with no specified milestone" do
103 visit project_issues_path(project, milestone_id: '0') 103 visit project_issues_path(project, milestone_id: '0')