class DBPurger::PlanValidator

DBPurger::PlanValidator is used to ensure that all tables in the database are part of the purge plan.

It verifies that all table definitions are correct as well, checking that child/parent fields are part
of the table.

Public Class Methods

new(database, plan) click to toggle source
# File lib/db-purger/plan_validator.rb, line 14
def initialize(database, plan)
  @database = database
  @plan = plan
end

Public Instance Methods

missing_tables() click to toggle source

tables that are part of the database but not part of the plan

# File lib/db-purger/plan_validator.rb, line 20
def missing_tables
  database_table_names - @plan.table_names.map(&:to_s)
end
unknown_tables() click to toggle source

tables that are part of the plan put not part of the database

# File lib/db-purger/plan_validator.rb, line 25
def unknown_tables
  @plan.table_names.map(&:to_s) - database_table_names
end

Private Instance Methods

database_table_names() click to toggle source
# File lib/db-purger/plan_validator.rb, line 64
def database_table_names
  @database_table_names ||= filter_table_names(@database.models.map(&:table_name))
end
filter_table_names(table_names) click to toggle source
# File lib/db-purger/plan_validator.rb, line 60
def filter_table_names(table_names)
  table_names.reject { |table_name| @plan.ignore_table?(table_name) }
end
find_model_for_table(table) click to toggle source
# File lib/db-purger/plan_validator.rb, line 56
def find_model_for_table(table)
  @database.models.detect { |model| model.table_name == table.name.to_s }
end
validate_no_missing_tables() click to toggle source
# File lib/db-purger/plan_validator.rb, line 31
def validate_no_missing_tables
  errors.add(:missing_tables, missing_tables.sort.join(',')) unless missing_tables.empty?
end
validate_no_unknown_tables() click to toggle source
# File lib/db-purger/plan_validator.rb, line 35
def validate_no_unknown_tables
  errors.add(:unknown_tables, unknown_tables.sort.join(',')) unless unknown_tables.empty?
end
validate_table_definition(table) click to toggle source
# File lib/db-purger/plan_validator.rb, line 43
def validate_table_definition(table)
  unless (model = find_model_for_table(table))
    errors.add(:table, "#{table.name} has no model")
    return
  end

  table.fields.each do |field|
    unless model.column_names.include?(field.to_s)
      errors.add(:table, "#{table.name}.#{field} is missing in the database")
    end
  end
end
validate_tables() click to toggle source
# File lib/db-purger/plan_validator.rb, line 39
def validate_tables
  @plan.tables.each { |table| validate_table_definition(table) }
end