# Table name: redactions
#
# id :integer not null, primary key
-# title :string
-# description :text
+# title :string not null
+# description :text not null
# created_at :datetime
# updated_at :datetime
# user_id :bigint(8) not null
--- /dev/null
+class AddCheckConstraintToRedactionTitleAndDescription < ActiveRecord::Migration[7.1]
+ disable_ddl_transaction!
+
+ def up
+ Redaction.where(:title => nil).find_in_batches(:batch_size => 1000) do |redactions|
+ redactions.each do |r|
+ r.title = "Redaction #{r.id}"
+ r.save!(:validate => false)
+ end
+ end
+
+ Redaction.where(:description => nil).find_in_batches(:batch_size => 1000) do |redactions|
+ redactions.each { |r| r.update!(:description => "No description") }
+ end
+
+ add_check_constraint :redactions, "title IS NOT NULL", :name => "redaction_title_not_null", :validate => false
+ add_check_constraint :redactions, "description IS NOT NULL", :name => "redaction_description_not_null", :validate => false
+ end
+
+ def down
+ remove_check_constraint :redactions, :name => "redaction_title_not_null", :if_exists => true
+ remove_check_constraint :redactions, :name => "redaction_description_not_null", :if_exists => true
+ end
+end
--- /dev/null
+class ValidateAndModifyRedactionTitleAndDescription < ActiveRecord::Migration[7.1]
+ disable_ddl_transaction!
+
+ def up
+ validate_check_constraint :redactions, :name => "redaction_title_not_null"
+ validate_check_constraint :redactions, :name => "redaction_description_not_null"
+
+ change_column_null :redactions, :title, false
+ change_column_null :redactions, :description, false
+
+ remove_check_constraint :redactions, :name => "redaction_title_not_null"
+ remove_check_constraint :redactions, :name => "redaction_description_not_null"
+ end
+
+ def down
+ change_column_null :redactions, :title, true
+ change_column_null :redactions, :description, true
+ end
+end
CREATE TABLE public.redactions (
id integer NOT NULL,
- title character varying,
- description text,
+ title character varying NOT NULL,
+ description text NOT NULL,
created_at timestamp without time zone,
updated_at timestamp without time zone,
user_id bigint NOT NULL,
('23'),
('22'),
('21'),
+('20240307181018'),
+('20240307180830'),
('20240228205723'),
('20240117185445'),
('20231213182102'),
('11'),
('10'),
('1');
+
assert_predicate(node_v1, :redacted?, "Expected node version 1 to be redacted after redact! call.")
assert_not_predicate(node_v2, :redacted?, "Expected node version 2 to not be redacted after redact! call.")
end
+
+ def test_invalid_with_empty_title
+ redaction = build(:redaction, :title => "")
+ assert_not redaction.valid?
+ assert_includes redaction.errors.messages[:title], "can't be blank"
+ end
+
+ def test_invalid_with_empty_description
+ redaction = build(:redaction, :description => "")
+ assert_not redaction.valid?
+ assert_includes redaction.errors.messages[:description], "can't be blank"
+ end
end