Reputation: 26848
Is there ORM for Ruby that able to generate Model/Migration scripts from existing database?
i will use it with sinatra.
Upvotes: 1
Views: 244
Reputation: 12251
You can use Sequel's migrations to dump an existing schema (see "Dumping the current schema as a migration" down the linked page).
Also, I know that if you're on Postgresql or MS SQL Server you can dump the database into scripts, either as a schema or with data too. If the ORM you want to use doesn't have a to-migration facility (or even if it does) you can use those scripts, and even embed them within a migration (Sequel will accept standard SQL strings too, other ORM's probably can too).
Edit: generating models.
If you want to generate models, then Sequel has a reflection API. Something like this could get you started:
generator = ->(table) {
s = <<STR
require 'sequel'
class #{table} < Sequel::Model
# other stuff here
end
STR
}
DB.tables.reject{|name| name == :schema_info }
.each do |table|
File.new "app/models/#{table}.rb" do |f|
f.write generator.call(table)
end
end
To do the associations would be harder, but I think possible. Take a look at Database#foreign_key_list and Model association basics.
Upvotes: 2
Reputation: 11076
It's not an ORM, but there's a very old gem called magic_models which does this, not sure if it still works: http://magicmodels.rubyforge.org/
Upvotes: 1