Commit 65206e11 authored by charlieablett's avatar charlieablett

Address reviewer comments

- Use BackgroundMigration
- Style/readability
- Use pure function for Weight method
- use say in migration instead of logger
- make update query more concise
parent e13242f0
--- ---
title: Properly persist -1 and null for board weight title: Properly persist -1 and null for board weight
merge_request: 14180 merge_request:
author: author:
type: changed type: changed
# frozen_string_literal: true # frozen_string_literal: true
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class DefaultWeightToNil < ActiveRecord::Migration[5.1] class DefaultWeightToNil < ActiveRecord::Migration[5.1]
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
# Set this constant to true if this migration requires downtime.
DOWNTIME = false DOWNTIME = false
# When a migration requires downtime you **must** uncomment the following disable_ddl_transaction!
# constant and define a short and easy to understand explanation as to why the
# migration requires downtime.
# DOWNTIME_REASON = ''
# When using the methods "add_concurrent_index", "remove_concurrent_index" or
# "add_column_with_default" you must disable the use of transactions
# as these methods can not run in an existing transaction.
# When using "add_concurrent_index" or "remove_concurrent_index" methods make sure
# that either of them is the _only_ method called in the migration,
# any other changes should go in a separate migration.
# This ensures that upon failure _only_ the index creation or removing fails
# and can be retried or reverted easily.
#
# To disable transactions uncomment the following line and remove these
# comments:
# disable_ddl_transaction!
def up class Board < ActiveRecord::Base
update_board_weights_from_none_to_any self.table_name = 'boards'
end
def down include ::EachBatch
update_board_weights_from_any_to_none
end end
# up method def up
Gitlab::BackgroundMigration.steal('UpdateBoardWeightsFromNoneToAny')
def update_board_weights_from_none_to_any Board.where(weight: -1).each_batch(of: 50) do |batch|
execute("UPDATE boards SET weight = null WHERE weight = -1") range = batch.pluck('MIN(id)', 'MAX(id)').first
end
say "Setting board weights from None to Any: #{range[0]} - #{range[1]}"
# down method Gitlab::BackgroundMigration::UpdateBoardWeightsFromNoneToAny.new.perform(*range)
end
end
def update_board_weights_from_any_to_none def down
execute("UPDATE boards SET weight = -1 WHERE weight = null") # noop
end end
end end
...@@ -2,14 +2,25 @@ ...@@ -2,14 +2,25 @@
import WeightSelect from 'ee/weight_select'; import WeightSelect from 'ee/weight_select';
import { GlLoadingIcon } from '@gitlab/ui'; import { GlLoadingIcon } from '@gitlab/ui';
const ANY_WEIGHT_LABEL = 'Any Weight'; const ANY_WEIGHT = {
const NO_WEIGHT_LABEL = 'No Weight'; LABEL: 'Any Weight',
STRING: 'Any',
VALUE: null,
};
const NO_WEIGHT = {
LABEL: 'No Weight',
STRING: 'None',
VALUE: -1,
};
const NO_WEIGHT_STR_VALUE = 'None'; function getWeightValue(weight) {
const ANY_WEIGHT_STR_VALUE = 'Any'; if (Number.isNaN(Number(weight))) {
return weight === NO_WEIGHT.STRING ? NO_WEIGHT.VALUE : ANY_WEIGHT.VALUE;
}
const NO_WEIGHT_INT_VALUE = -1; return parseInt(weight, 10);
const ANY_WEIGHT_INT_VALUE = null; }
export default { export default {
components: { components: {
...@@ -23,7 +34,7 @@ export default { ...@@ -23,7 +34,7 @@ export default {
value: { value: {
type: [Number, String], type: [Number, String],
required: false, required: false,
default: ANY_WEIGHT_INT_VALUE, default: ANY_WEIGHT.VALUE,
}, },
canEdit: { canEdit: {
type: Boolean, type: Boolean,
...@@ -42,14 +53,14 @@ export default { ...@@ -42,14 +53,14 @@ export default {
}, },
computed: { computed: {
valueClass() { valueClass() {
if (this.value === ANY_WEIGHT_INT_VALUE) { if (this.value === ANY_WEIGHT.VALUE) {
return 'text-secondary'; return 'text-secondary';
} }
return 'bold'; return 'bold';
}, },
valueText() { valueText() {
if (this.value === NO_WEIGHT_INT_VALUE) return NO_WEIGHT_LABEL; if (this.value === NO_WEIGHT.VALUE) return NO_WEIGHT.LABEL;
if (this.value === ANY_WEIGHT_INT_VALUE) return ANY_WEIGHT_LABEL; if (this.value === ANY_WEIGHT.VALUE) return ANY_WEIGHT.LABEL;
return this.value; return this.value;
}, },
}, },
...@@ -62,29 +73,18 @@ export default { ...@@ -62,29 +73,18 @@ export default {
}, },
methods: { methods: {
isSelected(weight) { isSelected(weight) {
if (this.value === NO_WEIGHT_INT_VALUE) { if (this.value === NO_WEIGHT.VALUE) {
return weight === NO_WEIGHT_STR_VALUE; return weight === NO_WEIGHT.STRING;
} }
if (this.value === ANY_WEIGHT_INT_VALUE) { if (this.value === ANY_WEIGHT.VALUE) {
return weight === ANY_WEIGHT_STR_VALUE; return weight === ANY_WEIGHT.STRING;
} }
return this.value === weight; return this.value === weight;
}, },
selectWeight(weight) { selectWeight(weight) {
this.board.weight = this.weightInt(weight); this.board.weight = getWeightValue(weight);
},
weightInt(weight) {
if (weight === NO_WEIGHT_STR_VALUE) {
return NO_WEIGHT_INT_VALUE;
}
if (weight === ANY_WEIGHT_STR_VALUE) {
return ANY_WEIGHT_INT_VALUE;
}
return weight;
}, },
}, },
}; };
......
...@@ -25,11 +25,10 @@ module EE ...@@ -25,11 +25,10 @@ module EE
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def by_weight(items) def by_weight(items)
return items unless weights? return items unless weights?
return items if filter_by_any_weight?
if filter_by_no_weight? if filter_by_no_weight?
items.where(weight: nil) items.where(weight: nil)
elsif filter_by_any_weight?
items
else else
items.where(weight: params[:weight]) items.where(weight: params[:weight])
end end
......
...@@ -36,6 +36,14 @@ describe IssuesFinder do ...@@ -36,6 +36,14 @@ describe IssuesFinder do
expect(issues).to contain_exactly(issue_with_weight_42) expect(issues).to contain_exactly(issue_with_weight_42)
end end
end end
context 'filter issues with a specific weight with no issues' do
let(:params) { { weight: 7 } }
it 'returns no issues' do
expect(issues).to be_empty
end
end
end end
context 'filtering by assignee IDs' do context 'filtering by assignee IDs' do
......
...@@ -125,7 +125,7 @@ describe('WeightSelect', () => { ...@@ -125,7 +125,7 @@ describe('WeightSelect', () => {
setTimeout(() => { setTimeout(() => {
expect(activeDropdownItem()).toEqual('2'); expect(activeDropdownItem()).toEqual('2');
expect(board.weight).toEqual('2'); expect(board.weight).toEqual(2);
done(); done();
}); });
}); });
......
# frozen_string_literal: true
# rubocop:disable Style/Documentation
module Gitlab
module BackgroundMigration
class UpdateBoardWeightsFromNoneToAny
class Board < ActiveRecord::Base
self.table_name = 'boards'
end
def perform(start_id, stop_id)
Board.where(id: start_id..stop_id).update_all(milestone: nil)
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