]> git.openstreetmap.org Git - rails.git/commitdiff
Add NOT NULL constraints to redaction title and description
authorIshmeet Singh <singhishmeet16@gmail.com>
Thu, 7 Mar 2024 18:38:26 +0000 (00:08 +0530)
committerTom Hughes <tom@compton.nu>
Mon, 11 Mar 2024 09:27:11 +0000 (09:27 +0000)
app/models/redaction.rb
db/migrate/20240307180830_add_check_constraint_to_redaction_title_and_description.rb [new file with mode: 0644]
db/migrate/20240307181018_validate_and_modify_redaction_title_and_description.rb [new file with mode: 0644]
db/structure.sql

index f4eedde0a2daddb172843d910507494261c1b4ae..5e9e0decde935a87b1514e3a8ffec16085026b79 100644 (file)
@@ -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 (file)
index 0000000..46de9e1
--- /dev/null
@@ -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 (file)
index 0000000..9df7016
--- /dev/null
@@ -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
index a41cb6991f5715054451e17c74a9a0202da8d78e..294fda4c8f503e9472bbf1db16f2d3b1c462c8e0 100644 (file)
@@ -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');
+