Method: Og::Evolution#evolve_schema
- Defined in:
- lib/og/store/sql/evolution.rb
#evolve_schema(klass) ⇒ Object
Evolve the schema (table in sql stores) for the given class. Compares the fields in the database schema with the serializable attributes of the given class and tries to fix mismatches by adding are droping columns.
Evolution options
-
:evolve_schema => :add (only add, dont remove columns)
-
:evolve_schema => :full (add and delete columns)
-
:evolve_schema => :warn (only emit warnings, DEFAULT_
-
:evolve_schema => false (no evolution)
Example
Og.setup(
..
:evolve_schema => :full
..
)
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/og/store/sql/evolution.rb', line 59 def evolve_schema(klass) return unless [:evolve_schema] sql_fields = create_field_map(klass).keys attrs = serializable_attributes_for_class(klass) # Add new fields to the table. for field in attrs unless sql_fields.include? field unless [:evolve_schema] == :warn add_sql_field klass, field, klass.ann(field) else Logger.warn "Missing field '#{field}' on table '#{klass.table}'!" end end end # Remove obsolete fields from the table. for field in sql_fields unless attrs.include? field if [:evolve_schema] == :full remove_sql_field klass, field else Logger.warn "Obsolete field '#{field}' found on table '#{klass.table}'!" end end end end |