class OnlineMigrations::BackgroundMigrations::BackfillColumn

@private

Attributes

model_name[R]
table_name[R]
updates[R]

Public Class Methods

new(table_name, updates, model_name = nil) click to toggle source
# File lib/online_migrations/background_migrations/backfill_column.rb, line 9
def initialize(table_name, updates, model_name = nil)
  @table_name = table_name
  @updates = updates
  @model_name = model_name
end

Public Instance Methods

count() click to toggle source
# File lib/online_migrations/background_migrations/backfill_column.rb, line 33
def count
  # Exact counts are expensive on large tables, since PostgreSQL
  # needs to do a full scan. An estimated count should give a pretty decent
  # approximation of rows count in this case.
  Utils.estimated_count(connection, table_name)
end
process_batch(relation) click to toggle source
# File lib/online_migrations/background_migrations/backfill_column.rb, line 29
def process_batch(relation)
  relation.update_all(updates)
end
relation() click to toggle source
# File lib/online_migrations/background_migrations/backfill_column.rb, line 15
def relation
  column, value = updates.first

  if updates.size == 1 && !value.nil?
    # If value is nil, the generated SQL is correct (`WHERE column IS NOT NULL`).
    # Otherwise, the SQL is `WHERE column != value`. This condition ignores column
    # with NULLs in it, so we need to also manually check for NULLs.
    quoted_column = connection.quote_column_name(column)
    model.unscoped.where("#{quoted_column} != ? OR #{quoted_column} IS NULL", value)
  else
    Utils.ar_where_not_multiple_conditions(model.unscoped, updates)
  end
end

Private Instance Methods

connection() click to toggle source
# File lib/online_migrations/background_migrations/backfill_column.rb, line 49
def connection
  model.connection
end
model() click to toggle source
# File lib/online_migrations/background_migrations/backfill_column.rb, line 41
def model
  @model ||= if model_name.present?
               Object.const_get(model_name, false)
             else
               Utils.define_model(table_name)
             end
end