=force_schema

class Car < ActiveRecord::Base
  set_primary_key :name
  force_schema do
    string   'name'       # Nissan Altima, will automatically be indexed because it's the primary key
    string   'make_name'  # Nissan
    string   'model_name' # Altime
    float    'fuel_efficiency_city'
    string   'fuel_efficiency_city_units'
    float    'fuel_efficiency_highway'
    string   'fuel_efficiency_highway_units'
    integer  'year'
    datetime 'released_at'
    date     'released_on'
    index    ['name', 'make_name']
  end
end

Warning: data loss

If you call Car.force_schema!, it may destroy data by adding, removing, or changing the type of columns. No attempt is made to preserve data.

What it synchronizes

  • adds missing columns
  • adds missing indexes
  • removes unrecognized columns
  • removes unrecognized indexes
  • changes the type of columns if it doesn't match what you asked for
  • changes the columns indexed if they don't match what you asked for
  • makes sure default value is synced up

Supported databases

  • MySQL is tested
  • SQLite is tested
  • Postgres is not supported

History

Extracted from https://github.com/seamusabshere/data_miner. In production use at http://carbon.brighterplanet.com and http://data.brighterplanet.com.

Copyright 2011 Seamus Abshere