From 146a517123b157cd405c0fce8508ce8360f522b1 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 13 Mar 2024 09:56:06 +0000 Subject: [PATCH] Add default and constraint to CensorRule#regexp Add default value and not null constraint to `CensorRule#regexp`. While effectively the same behaviour, this makes the intent explicit and prevents empty values appearing in admin tables where we render censor rules. --- app/models/censor_rule.rb | 4 ++-- ..._add_not_null_constraint_to_censor_rule_regexp.rb | 12 ++++++++++++ doc/CHANGES.md | 1 + spec/factories/censor_rules.rb | 4 ++-- spec/models/censor_rule_spec.rb | 4 ++-- 5 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20240313094449_add_not_null_constraint_to_censor_rule_regexp.rb diff --git a/app/models/censor_rule.rb b/app/models/censor_rule.rb index 0ecd281508..4548f21b2c 100644 --- a/app/models/censor_rule.rb +++ b/app/models/censor_rule.rb @@ -1,5 +1,5 @@ # == Schema Information -# Schema version: 20210114161442 +# Schema version: 20240313094449 # # Table name: censor_rules # @@ -13,7 +13,7 @@ # last_edit_comment :text not null # created_at :datetime not null # updated_at :datetime not null -# regexp :boolean +# regexp :boolean default(FALSE), not null # # models/censor_rule.rb: diff --git a/db/migrate/20240313094449_add_not_null_constraint_to_censor_rule_regexp.rb b/db/migrate/20240313094449_add_not_null_constraint_to_censor_rule_regexp.rb new file mode 100644 index 0000000000..650f2e6cfa --- /dev/null +++ b/db/migrate/20240313094449_add_not_null_constraint_to_censor_rule_regexp.rb @@ -0,0 +1,12 @@ +class AddNotNullConstraintToCensorRuleRegexp < ActiveRecord::Migration[7.0] + def up + change_column_default :censor_rules, :regexp, false + CensorRule.where(regexp: nil).update_all(regexp: false) + change_column_null :censor_rules, :regexp, false + end + + def down + change_column_null :censor_rules, :regexp, true + change_column_default :censor_rules, :regexp, nil + end +end diff --git a/doc/CHANGES.md b/doc/CHANGES.md index 4bd9745911..9210750847 100644 --- a/doc/CHANGES.md +++ b/doc/CHANGES.md @@ -2,6 +2,7 @@ ## Highlighted Features +* Add default value and not null constraint to `CensorRule#regexp` (Gareth Rees) * Reduce amount of storage related background jobs (Graeme Porteous) * Add automatic parsing of emails contain Excel spreadsheets (Graeme Porteous) * Improve rendering of admin hidden request prominence and explanations (Graeme diff --git a/spec/factories/censor_rules.rb b/spec/factories/censor_rules.rb index 3ebe63c206..e9e3ad4f46 100644 --- a/spec/factories/censor_rules.rb +++ b/spec/factories/censor_rules.rb @@ -1,5 +1,5 @@ # == Schema Information -# Schema version: 20210114161442 +# Schema version: 20240313094449 # # Table name: censor_rules # @@ -13,7 +13,7 @@ # last_edit_comment :text not null # created_at :datetime not null # updated_at :datetime not null -# regexp :boolean +# regexp :boolean default(FALSE), not null # FactoryBot.define do diff --git a/spec/models/censor_rule_spec.rb b/spec/models/censor_rule_spec.rb index 7d7d923079..04de33231d 100644 --- a/spec/models/censor_rule_spec.rb +++ b/spec/models/censor_rule_spec.rb @@ -1,5 +1,5 @@ # == Schema Information -# Schema version: 20210114161442 +# Schema version: 20240313094449 # # Table name: censor_rules # @@ -13,7 +13,7 @@ # last_edit_comment :text not null # created_at :datetime not null # updated_at :datetime not null -# regexp :boolean +# regexp :boolean default(FALSE), not null # require 'spec_helper'