Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate datamapper models from an existing database

Is it possible with datamapper to generate the models from an existing database schema? So, to do the inverse of a migration, which takes the models and generate the sql. What I want is given a database schema generate the models.

like image 483
Waiting for Dev... Avatar asked May 20 '26 15:05

Waiting for Dev...


2 Answers

try to check https://github.com/yogo/dm-reflection or any of its forks ..

like image 155
Tom Meinlschmidt Avatar answered May 23 '26 08:05

Tom Meinlschmidt


At last, I found that till now the best solution is to use dm-is-reflective plugin: https://github.com/godfat/dm-is-reflective.

It doesn't generate the code for DataMapper models reflecting an existing database schema, but its properties access methods are automatically available (as long as you keep using this plugin, of course).

Here is an example of use:

require 'data_mapper'
require 'dm-is-reflective'

DataMapper.setup(:default, "postgres://user:pwd@localhost/db")

class Table
   include DataMapper::Resource

   is :reflective #activate dm-is-reflective

   reflect #reflects eeach property. You can be more specific (look at plugin documentation)
end

DataMapper.finalize

#Even if no field is defined, all of them are accessible
entry = Table.first(nil, {:id => 469})
print entry.anotherField
like image 29
Waiting for Dev... Avatar answered May 23 '26 08:05

Waiting for Dev...



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!