]> git.openstreetmap.org Git - rails.git/blob - db/migrate/20240307180830_add_check_constraint_to_redaction_title_and_description.rb
Merge remote-tracking branch 'upstream/pull/4747'
[rails.git] / db / migrate / 20240307180830_add_check_constraint_to_redaction_title_and_description.rb
1 class AddCheckConstraintToRedactionTitleAndDescription < ActiveRecord::Migration[7.1]
2   disable_ddl_transaction!
3
4   def up
5     Redaction.where(:title => nil).find_in_batches(:batch_size => 1000) do |redactions|
6       redactions.each do |r|
7         r.title = "Redaction #{r.id}"
8         r.save!(:validate => false)
9       end
10     end
11
12     Redaction.where(:description => nil).find_in_batches(:batch_size => 1000) do |redactions|
13       redactions.each { |r| r.update!(:description => "No description") }
14     end
15
16     add_check_constraint :redactions, "title IS NOT NULL", :name => "redaction_title_not_null", :validate => false
17     add_check_constraint :redactions, "description IS NOT NULL", :name => "redaction_description_not_null", :validate => false
18   end
19
20   def down
21     remove_check_constraint :redactions, :name => "redaction_title_not_null", :if_exists => true
22     remove_check_constraint :redactions, :name => "redaction_description_not_null", :if_exists => true
23   end
24 end