Database Introspection

This gem will introspect the database and create dynamically ActiveRecord::Base descendants that can be used by your application, including some Rails associations helper methods.

It is intended to be primarily used within rails applications but nothing prevents you to use standalone, provided the fact you are already connected to a database. This gem does a bit the reverse action of what you do with Rails generator, when you want to generate the database from you migrations.

Basically, it will infer the database and create ActiveRecord::Base descendants to handle tables already defined in the database.

The cool point is that it will as well guess from column names in the tables the relations between tables and dynamically create the associations in the generated classes and thus all helper methods (for belongs_to, has_many and even has_many :through).

Installation

Add this line to your application's Gemfile:

gem 'database_introspection'

And then execute:

$ bundle

Or install it yourself as:

$ gem install database_introspection

Classes documentation is available here

Usage

Basic database introspection

To introspect the database, you just have to call the following method:

DynamicModel.introspect_database

By default it will analyse all database tables starting by "user_defined_", and will create ActiveRecord::Base descendant to handle them.

For example if your database contains the following tables:

user_defined_table1
user_defined_table2
user_defined_table3
user_defined_table4

The call to DynamicModel.introspect_database, will inject the following classes in your application:

DynamicModel::ManagedDomains::UserDefined::Table1
DynamicModel::ManagedDomains::UserDefined::Table2
DynamicModel::ManagedDomains::UserDefined::Table3
DynamicModel::ManagedDomains::UserDefined::Table4

Architecture

Classes and modules generated

  • DynamicModel is the module that contains methods to introspect the database.
  • DynamicModel::ManagedDomain contains some methods to manipulate the domains introspected.
  • Then for example in the first example, DynamicModel::ManagedDomains::UserDefined is a module created dynamically from the domain name (ie tables prefix). It contains itself some methods to easily manipulate the tables or generated classes of this particular domain.
  • The DynamicModel::ManagedDomains::UserDefined::Tablex classes are descendants of ActiveRecord::Base that you will use in your application.

Of course if you provide another domain name (ie tables prefix) the corresponding modules and classes will be created accordingly. Running:

DynamicModel.introspect_database :another_domain

On a database containing the following tables:

another_domain_table1
another_domain_table2

Will inject the following classes in your application:

DynamicModel::ManagedDomains::AnotherDomain::Table1
DynamicModel::ManagedDomains::AnotherDomain::Table2

and of course the following module:

DynamicModel::ManagedDomains::AnotherDomain

Generated classes

The classes generated will actually have some behaviour added by extending the module DynamicModel::ActiveRecordExtension to basically be aware of the domain they belong to.

Table relationships

Provided you follow the standard rails rules for ids, for example:

if user_defined_table1 contains user_defined_table2_id or simply table2_id (if table2 is part of the same domain user_defined), the introspection process will understand there is a relationship between the tables and create the ActiveRecord associations accordingly adding all the standard helper methods to the generated classes !

To do

  • Improve Readme.
  • Add code comments.
  • Improve table relationship introspection.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request