Skip to content

Instantly share code, notes, and snippets.

@kyamaguchi
Created August 31, 2013 04:16
Show Gist options
  • Save kyamaguchi/6396196 to your computer and use it in GitHub Desktop.
Save kyamaguchi/6396196 to your computer and use it in GitHub Desktop.
Migration to create tables using CoreData file
# gem 'core_data'
class CreateModelsFromCoreData < ActiveRecord::Migration
TYPE_MAP = {
"Boolean" => 'boolean',
"Date" => 'datetime',
"Integer 32" => 'integer',
"String" => 'string',
}
def up
schema = CoreData::DataModel.new("#{Rails.root}/db/data.xcdatamodeld")
entities = schema.entities
entities.each do |entity|
create_table entity.name.tableize.to_sym do |t|
entity.attributes.each do |attribute|
t.send(TYPE_MAP[attribute.type], attribute.name.underscore.to_sym)
end
entity.relationships.each do |relationship|
t.send('integer', "#{relationship.name.underscore}_id".to_sym) if relationship.to_one?
end
t.timestamps
end
end
end
def down
schema = CoreData::DataModel.new("#{Rails.root}/db/sekken.xcdatamodeld")
entities = schema.entities
entities.each do |entity|
drop_table entity.name.tableize.to_sym
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment