Low commit activity in last 3 years
A long-lived project that still receives updates
Dynamically create ActiveRecord models for tables
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

Build Status Maintainability Test Coverage

Dynamic Active Model - Automatic Database Discovery, Model Creation, and Relationship Mapping for Rails

Dynamic Active Model automatically reads a database, and maps the database classes to Active Record models. This includes defining relationships based on foreign keys. Currently, has_many and belongs_to relationships are supported. By default, Dynamic Active Model is best used for creating missing Active Record models, or exploring a database without having to create the models.

Basic Usage

Explore Database Models

This allows you to create Active Record models in memory.

# Define your database model scope. This is neccesary to prevent conflicts.
module DB; end

# Intiialize models and relationships. This can be broken apart into separate calls if you'd like.
#
# Create the actual class models
#
#   db = DynamicActiveModel::Database.new(DB, username: 'root', adapter: 'postgresql', database: 'rails_development', password: 'password')
#   db.create_models!
#
# Create the relationships
#
#   relations = DynamicActiveModel::Associations.new(db)
#   relations.build!
#
# Instead, this combines both those methods into one
DynamicActiveModel::Explorer.explore(DB, username: 'root', adapter: 'postgresql', database: 'rails_development', password: 'password')

# find some model
movie = DB::Movies.first

# some attribute
movie.name

# some relationships
movie.actors

Blacklist (Skip) Tables to Create Models For

You can blacklist tables to create models for, to ignore certain specific tables

# initialize the database
db = DynamicActiveModel::Database.new(DB, username: 'root', adapter: 'postgresql', database: 'rails_development', password: 'password')

# skip a single table
db.skip_table 'actors'

# skip tables by regex
db.skip_table /^temp/

# skip multiple tables
db.skip_tables ['2018-01-01_temp', /^daily/]

db.create_models!

Whitelist Tables to Create Models For

If you'd like to whitelist instead, that's also available.

# initialize the database
db = DynamicActiveModel::Database.new(DB, username: 'root', adapter: 'postgresql', database: 'rails_development', password: 'password')

# include a single table
db.include_table 'actors'

# include tables by regex
db.include_table /^special/

# include multiple tables
db.include_tables ['movies', 'salaries']

db.create_models!

Create Model Files

If you'd like to actually create the files for models, you can do so through

 dynamic-db-explorer --username root --adapter postgresql --host localhost --database rails_development --password password --create-class-files /path/to/folder/for/model/files

Extend Model

Add additional functionality to any model by using the update_model method.

# Specify the model by the table name
db.update_model(:movies) do
  attr_accessor :imdb_id

  def first_actor
    actors.first
  end
end

Additional functionality can be added through files as well.

# file: lib/db/movies.ext.rb
attr_accessor :imdb_id

def first_actor
  actors.first
end

Then call update_model with

db.update_model(:movies, 'lib/db/movies.ext.rb')

Mass apply updates to multiple models with update_all_models. The base_dir is the path to model extension files. The files are expected to use the .ext.rb extension.

db.update_all_models('lib/db')