# \[ANN\] db\_discovery generator gem

**URL:** <https://www.ruby-forum.com/t/ann-db-discovery-generator-gem/72182>\
**Category:** Rails\
**Created:** [October 15, 2006, 5:19am UTC](https://www.ruby-forum.com/t/ann-db-discovery-generator-gem/72182 "2006-10-15T05:19:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sergey\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@sergey\_k](https://www.ruby-forum.com/u/sergey_k)\
**Post date:** [October 15, 2006, 5:19am UTC](https://www.ruby-forum.com/t/ann-db-discovery-generator-gem/72182/1 "2006-10-15T05:19:32Z")

</div>

Hi Everyone,

I just made first version (0.1.0) of db\_discovery\_generator.

DB discovery generator allows you to discover exact ( almost exact to be  
exact 🙂 structure of your tables and create the models in app/models  
including discovered associations.  
If user uses mixedCase field names and unusual (different from id)  
primary keys this generator will properly recognize that and will setup  
correct table\_name and primary\_key in the model.

Also it discovers associations between tables and creates correct  
belongs\_to/has\_many links.

At this moment it uses only has\_many associations, next versions will be  
more clever on this matter, and will use information about foreign keys.

It properly recognizes fields like user\_id or UserID and if table users  
or user exists it will link them together.  
HABTM is not done yet, but in the TODO list.

At this moment it correctly recognizes primary keys only for PostgreSQL  
and MySQL, other RDBMSes on the way ( I don’t have an access to them 🙂

I created project on RubyForge: [http://db-discovery.rubyforge.org](http://db-discovery.rubyforge.org) and  
released files but they are not shown up yet.  
At this moment you can download gem and tarball from  
[http://db-discovery.deeptown.org](http://db-discovery.deeptown.org).

If you have any suggestions and ideas do not hesitate to contact me via  
email.

At this moment if you will type script/generate db\_discovery - it will  
start discover right away. If you would like to see the parameters you  
have to add --help  
switch.

All the best!  
Sergey.

---

<div class="post-metadata">

**Author:** ![sergey\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@sergey\_k](https://www.ruby-forum.com/u/sergey_k)\
**Post date:** [October 15, 2006, 8:33am UTC](https://www.ruby-forum.com/t/ann-db-discovery-generator-gem/72182/2 "2006-10-15T08:33:43Z")

</div>

> At this moment if you will type script/generate db\_discovery - it will  
> start discover right away. If you would like to see the parameters you  
> have to add --help  
> switch.
> 
> All the best!  
> Sergey.

Sounds good, I will play with it soon. When I try to install I get the  
following error:

02:29:44:~ \> sudo gem install db\_discovery  
Need to update 24 gems from [http://gems.rubyforge.org](http://gems.rubyforge.org)  
…  
complete  
ERROR: While executing gem … (Gem::GemNotFoundException)  
Could not find db\_discovery (\> 0) in the repository

---

<div class="post-metadata">

**Author:** ![sergey\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@sergey\_k](https://www.ruby-forum.com/u/sergey_k)\
**Post date:** [October 15, 2006, 1:02pm UTC](https://www.ruby-forum.com/t/ann-db-discovery-generator-gem/72182/3 "2006-10-15T13:02:01Z")

</div>

I don’t know how to put it to rubyforge gem repository. It’s on my web  
server yet, and in release files on [db-discovery.rubyforge.org](http://db-discovery.rubyforge.org)

All the Best!  
Sergey.

---

<div class="post-metadata">

**Author:** ![sergey\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@sergey\_k](https://www.ruby-forum.com/u/sergey_k)\
**Post date:** [October 15, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/ann-db-discovery-generator-gem/72182/4 "2006-10-15T17:42:35Z")

</div>

How do I install this?

---

<div class="post-metadata">

**Author:** ![sergey\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@sergey\_k](https://www.ruby-forum.com/u/sergey_k)\
**Post date:** [October 15, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/ann-db-discovery-generator-gem/72182/5 "2006-10-15T14:44:14Z")

</div>

Fixed few minor bugs, put it as version 0.1.1.

If you start it without parameters it shows help, to do the actual  
discovery you have to provide --discover  
flag.

It generates fixtures and unit tests.

All the Best!  
Sergey.

---

<div class="post-metadata">

**Author:** ![sergey\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@sergey\_k](https://www.ruby-forum.com/u/sergey_k)\
**Post date:** [October 18, 2006, 9:57am UTC](https://www.ruby-forum.com/t/ann-db-discovery-generator-gem/72182/6 "2006-10-18T09:57:46Z")

</div>

Very nice, I did almost exactly the same thing described here…

[http://blog.wolfman.com/articles/2006/05/24/create-models-from-ddl](http://blog.wolfman.com/articles/2006/05/24/create-models-from-ddl)

but your looks better and cleaner, and doesn’t use DDL.

However you may get ideas from mine I do handle HABTM, and I add some  
validations too where I can  
figure them out.

I’ll link to yours on my blog as I think it is a better solution.

Sergey K. wrote:

> At this moment it correctly recognizes primary keys only for PostgreSQL  
> At this moment if you will type script/generate db\_discovery - it will  
> start discover right away. If you would like to see the parameters you  
> have to add --help  
> switch.
> 
> All the best!  
> Sergey.
> 
> >

–  
Jim M., [http://blog.wolfman.com](http://blog.wolfman.com)

---

<div class="post-metadata">

**Author:** ![sergey\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@sergey\_k](https://www.ruby-forum.com/u/sergey_k)\
**Post date:** [October 18, 2006, 3:25pm UTC](https://www.ruby-forum.com/t/ann-db-discovery-generator-gem/72182/7 "2006-10-18T15:25:58Z")

</div>

Jim,

Jim M. wrote:

> Very nice, I did almost exactly the same thing described here…
> 
> [Wolfmans Howlings](http://blog.wolfman.com/articles/2006/05/24/create-models-from-ddl)
> 
> but your looks better and cleaner, and doesn’t use DDL.

Thanks a lot for your kind words! I definitely will think how to use  
your ideas.  
In case of Rails convention based HABTM this is not a problem to create  
HABTM relation.  
I will have one more pass through the model classes to distinguish this  
relation. In other case  
I have to check if the names are the same, or if there is a foreign keys  
to that table from  
both referrers, or record names in HABTM table belongs to that tables,  
and there is no more  
fields in that table.

> However you may get ideas from mine I do handle HABTM, and I add some  
> validations too where I can figure them out.

I want to add validations as well such as validate\_presence\_of and so on  
based on NULL/NOT NULL and  
field type.

> I’ll link to yours on my blog as I think it is a better solution.

Thanks a lot! I appreciate that.

All the Best!  
Sergey.

---

<div class="post-metadata">

**Author:** ![sergey\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@sergey\_k](https://www.ruby-forum.com/u/sergey_k)\
**Post date:** [October 15, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/ann-db-discovery-generator-gem/72182/8 "2006-10-15T18:04:39Z")

</div>

Just type:

gem install db\_discovery\_generator

It’s already on rubyforge gem repository,

All the Best!  
Sergey.
