Module: ModelSchema::Plugin::ClassMethods

Defined in:
lib/model_schema/plugin.rb

Instance Method Summary collapse

Instance Method Details

#model_schema(options = {}, &block) ⇒ Object

Checks if the model’s table schema matches the schema specified by the given block. Raises a SchemaError if this isn’t the case.

options: :disable => true to disable schema checks;

you may also set the ENV variable DISABLE_MODEL_SCHEMA=1

:no_indexes => true to disable index checks

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/model_schema/plugin.rb', line 15

def model_schema(options={}, &block)
  return if ENV[DISABLE_MODEL_SCHEMA_KEY] == '1' || options[:disable]
  db.extension(:schema_dumper)

  # table generators are Sequel's way of representing schemas
  db_generator = table_generator
  exp_generator = db.create_table_generator(&block)
  
  schema_errors = check_all(FIELD_COLUMNS, db_generator, exp_generator)
  if !options[:no_indexes]
    schema_errors += check_all(FIELD_INDEXES, db_generator, exp_generator)
  end
  
  raise SchemaError.new(table_name, schema_errors) if schema_errors.length > 0
end