From a33174acb8ebf4cff67281889ceca940b6b5a48b Mon Sep 17 00:00:00 2001 From: Ishmeet Singh Date: Fri, 8 Mar 2024 00:08:26 +0530 Subject: [PATCH] Add NOT NULL constraints to redaction title and description --- app/models/redaction.rb | 4 ++-- ...aint_to_redaction_title_and_description.rb | 24 +++++++++++++++++++ ..._modify_redaction_title_and_description.rb | 19 +++++++++++++++ db/structure.sql | 7 ++++-- 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20240307180830_add_check_constraint_to_redaction_title_and_description.rb create mode 100644 db/migrate/20240307181018_validate_and_modify_redaction_title_and_description.rb diff --git a/app/models/redaction.rb b/app/models/redaction.rb index f4eedde0a..5e9e0decd 100644 --- a/app/models/redaction.rb +++ b/app/models/redaction.rb @@ -3,8 +3,8 @@ # 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 diff --git a/db/migrate/20240307180830_add_check_constraint_to_redaction_title_and_description.rb b/db/migrate/20240307180830_add_check_constraint_to_redaction_title_and_description.rb new file mode 100644 index 000000000..46de9e1fd --- /dev/null +++ b/db/migrate/20240307180830_add_check_constraint_to_redaction_title_and_description.rb @@ -0,0 +1,24 @@ +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 diff --git a/db/migrate/20240307181018_validate_and_modify_redaction_title_and_description.rb b/db/migrate/20240307181018_validate_and_modify_redaction_title_and_description.rb new file mode 100644 index 000000000..9df70165d --- /dev/null +++ b/db/migrate/20240307181018_validate_and_modify_redaction_title_and_description.rb @@ -0,0 +1,19 @@ +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 diff --git a/db/structure.sql b/db/structure.sql index a41cb6991..294fda4c8 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1304,8 +1304,8 @@ ALTER SEQUENCE public.oauth_tokens_id_seq OWNED BY public.oauth_tokens.id; 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, @@ -3512,6 +3512,8 @@ INSERT INTO "schema_migrations" (version) VALUES ('23'), ('22'), ('21'), +('20240307181018'), +('20240307180830'), ('20240228205723'), ('20240117185445'), ('20231213182102'), @@ -3597,3 +3599,4 @@ INSERT INTO "schema_migrations" (version) VALUES ('11'), ('10'), ('1'); + -- 2.39.5