class ZeroDowntimeMigrations::Validation::AddIndex
Public Instance Methods
validate!()
click to toggle source
# File lib/zero_downtime_migrations/validation/add_index.rb 4 def validate! 5 return if concurrent? && migration.ddl_disabled? 6 error!(message) 7 end
Private Instance Methods
column()
click to toggle source
# File lib/zero_downtime_migrations/validation/add_index.rb 47 def column 48 args[1] 49 end
column_title()
click to toggle source
# File lib/zero_downtime_migrations/validation/add_index.rb 51 def column_title 52 Array(column).map(&:to_s).join("_and_").camelize 53 end
concurrent?()
click to toggle source
# File lib/zero_downtime_migrations/validation/add_index.rb 43 def concurrent? 44 options[:algorithm] == :concurrently 45 end
message()
click to toggle source
# File lib/zero_downtime_migrations/validation/add_index.rb 11 def message 12 <<-MESSAGE.strip_heredoc 13 Adding a non-concurrent index is unsafe! 14 15 This action can lock your database table while indexing existing data! 16 17 Instead, let's add the index concurrently in its own migration with 18 the DDL transaction disabled. 19 20 This allows PostgreSQL to build the index without locking in a way 21 that prevent concurrent inserts, updates, or deletes on the table. 22 Standard indexes lock out writes (but not reads) on the table. 23 24 class Index#{table_title}On#{column_title} < ActiveRecord::Migration 25 disable_ddl_transaction! 26 27 def change 28 add_index :#{table}, #{column.inspect}, algorithm: :concurrently 29 end 30 end 31 32 If you're 100% positive that this migration is already safe, then wrap the 33 call to `add_index` in a `safety_assured` block. 34 35 class Index#{table_title}On#{column_title} < ActiveRecord::Migration 36 def change 37 safety_assured { add_index :#{table}, #{column.inspect} } 38 end 39 end 40 MESSAGE 41 end
table()
click to toggle source
# File lib/zero_downtime_migrations/validation/add_index.rb 55 def table 56 args[0] 57 end
table_title()
click to toggle source
# File lib/zero_downtime_migrations/validation/add_index.rb 59 def table_title 60 table.to_s.camelize 61 end