]> git.openstreetmap.org Git - rails.git/blobdiff - db/migrate/20240307180830_add_check_constraint_to_redaction_title_and_description.rb
Add NOT NULL constraints to redaction title and description
[rails.git] / db / migrate / 20240307180830_add_check_constraint_to_redaction_title_and_description.rb
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