Commit 7965dacb authored by Sean McGivern's avatar Sean McGivern

Handle commas in weight system notes in code, not the DB

We will tackle the migration in a future release, as a background migration.

For now, hide these commas at the model layer.
parent 2a0ad260
...@@ -11,6 +11,24 @@ module EE ...@@ -11,6 +11,24 @@ module EE
noteable.is_a?(Epic) noteable.is_a?(Epic)
end end
# Remove with https://gitlab.com/gitlab-org/gitlab-ee/issues/6347
def note
raw_note = super
return raw_note unless system? && system_note_metadata&.action == 'weight'
raw_note.delete(',')
end
# Remove with https://gitlab.com/gitlab-org/gitlab-ee/issues/6347
def note_html
raw_note_html = super
return raw_note_html unless system? && system_note_metadata&.action == 'weight'
raw_note_html.delete(',')
end
override :for_project_noteable? override :for_project_noteable?
def for_project_noteable? def for_project_noteable?
!for_epic? && super !for_epic? && super
......
class RemoveCommasFromWeightNote < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index(:system_note_metadata, :action, where: "action = 'weight'")
notes_table = Arel::Table.new(:notes)
metadata_table = Arel::Table.new(:system_note_metadata)
update_value = Arel.sql("TRIM(TRAILING ',' FROM note), note_html = NULL")
weight_system_notes =
notes_table
.join(metadata_table).on(notes_table[:id].eq(metadata_table[:note_id]))
.where(metadata_table[:action].eq('weight'))
.where(notes_table[:system].eq(true))
.where(notes_table[:note].matches('%,'))
.project(notes_table[:id])
if Gitlab::Database.mysql?
weight_system_notes = weight_system_notes.from(
notes_table.project([notes_table[:id], notes_table[:system]]).as('notes')
)
end
update_column_in_batches(:notes, :note, update_value) do |table, query|
query.where(table[:id].in(weight_system_notes))
end
remove_concurrent_index(:system_note_metadata, :action)
end
def down
# We can't reliably find notes that would need a comma, so do nothing
end
end
require 'spec_helper'
require Rails.root.join('ee', 'db', 'migrate', '../../db/migrate/20180530201303_remove_commas_from_weight_note.rb')
describe RemoveCommasFromWeightNote, :migration do
RSpec::Matchers.define_negated_matcher :not_change, :change
describe '#up' do
let(:notes) { table(:notes) }
let(:system_note_metadata) { table(:system_note_metadata) }
def create_system_note(note, metadata_action)
notes.create(note: note, system: true).tap do |system_note|
system_note_metadata.create(note_id: system_note.id, action: metadata_action)
end
end
let(:weight_note_with_comma) { create_system_note('changed weight to 5,', 'weight') }
let(:weight_note_without_comma) { create_system_note('removed the weight', 'weight') }
let(:title_note) { create_system_note('changed title from 5, to 5,', 'title') }
let(:user_note) { notes.create(note: 'changed weight to 5,') }
it 'removes all trailing commas from weight system notes' do
expect { migrate! }
.to change { Note.where("note LIKE '%,'").count }.from(3).to(2)
.and change { weight_note_with_comma.reload.note }
.and not_change { weight_note_without_comma.reload.note }
.and not_change { title_note.reload.note }
.and not_change { user_note.reload.note }
end
end
end
require 'spec_helper'
describe EE::Note do
# Remove with https://gitlab.com/gitlab-org/gitlab-ee/issues/6347
describe "#note and #note_html overrides for weight" do
using RSpec::Parameterized::TableSyntax
where(:system, :action, :result) do
false | nil | 'this, has, some, commas'
true | nil | 'this, has, some, commas'
true | 'relate' | 'this, has, some, commas'
true | 'weight' | 'this has some commas'
end
with_them do
let(:note) { create(:note, system: system, note: 'this, has, some, commas') }
before do
create(:system_note_metadata, action: action, note: note) if action
end
it 'returns the right raw note' do
expect(note.note).to eq(result)
end
it 'returns the right HTML' do
expect(note.note_html).to eq("<p dir=\"auto\">#{result}</p>")
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