https://github.com/twopoint718/reactive_record
Generate ActiveRecord models for a pre-existing Postgres db
https://github.com/twopoint718/reactive_record
activerecord-models constraints database postgres postgres-db rails ruby sql
Last synced: about 2 months ago
JSON representation
Generate ActiveRecord models for a pre-existing Postgres db
- Host: GitHub
- URL: https://github.com/twopoint718/reactive_record
- Owner: twopoint718
- License: mit
- Created: 2013-02-03T06:13:51.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2021-11-26T17:37:38.000Z (about 4 years ago)
- Last Synced: 2024-05-11T21:02:18.845Z (almost 2 years ago)
- Topics: activerecord-models, constraints, database, postgres, postgres-db, rails, ruby, sql
- Language: Ruby
- Size: 83 KB
- Stars: 131
- Watchers: 7
- Forks: 3
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
Logo design: Kelly Rauwerdink,
@missingdink,
Dribbble.
[](http://badge.fury.io/rb/reactive_record)
Generates ActiveRecord models to fit a pre-existing Postgres database.
Now you can use Rails with the db schema you always wanted. It's
**your** convention over configuration.
## Why?
1. Your app is specific to Postgres and proud of it. You use the mature
declarative data validation that only a real database can provide.
1. You have inherited a database or are more comfortable creating one
yourself.
1. You're a grown-ass DBA who doesn't want to speak ORM baby-talk.
## Features
* Fully automatic. It just works.
* Creates a model for every table.
* Creates a comprehensive initial migration.
* Declares key-, uniqueness-, and presence-constraints.
* Creates associations.
* Adds custom validation methods for `CHECK` constraints.
## Usage
### Already familiar with Rails?
* Set up a postgres db normally
* Set `config.active_record.schema_format = :sql` to use a SQL `schema.rb`
* After you have migrated up a table, use `rails generate reactive_record:install`
* Go examine your generated models
### Want more details?
**First** Include the `reactive_record` gem in your project's
`Gemfile`. *Oh by the way*, you'll have to use postgres in your
project. Setting up Rails for use with postgres is a bit outside
the scope of this document. Please see [Configuring a Database]
(http://guides.rubyonrails.org/configuring.html#configuring-a-database)
for what you need to do.
``` ruby
gem 'reactive_record'
```
Bundle to include the library
``` shell
$ bundle
```
**Next** Tell `ActiveRecord` to go into beast-mode. Edit your
`config/application.rb`, adding this line to use sql as the schema
format:
``` ruby
module YourApp
class Application < Rails::Application
# other configuration bric-a-brac...
config.active_record.schema_format = :sql
end
end
```
**Next** Create the database(s) just like you normally would:
``` shell
rake db:create
```
**Next** Generate a migration that will create the initial table:
``` shell
$ rails generate migration create_employees
```
Use your SQL powers to craft some
[DDL](http://en.wikipedia.org/wiki/Data_definition_language), perhaps
the "Hello, World!" of DB applications, `employees`?
``` ruby
class CreateEmployees < ActiveRecord::Migration
def up
execute <<-SQL
CREATE TABLE employees (
id SERIAL,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
start_date DATE NOT NULL,
PRIMARY KEY (id),
CONSTRAINT company_email CHECK (email LIKE '%@example.com')
);
SQL
end
def down
drop_table :employees
end
end
```
**Lastly** Deploy the `reactive_record` generator:
``` shell
$ rails generate reactive_record:install
```
Go look at the generated file:
``` ruby
class Employees < ActiveRecord::Base
set_table_name 'employees'
set_primary_key :id
validate :id, :name, :email, :start_date, presence: true
validate :email, uniqueness: true
validate { errors.add(:email, "Expected TODO") unless email =~ /.*@example.com/ }
end
```
Reactive record does not currently attempt to generate any kind of
reasonable error message (I'm working on it) :)
**Enjoy**
## Credits
Firstly, thank you,
[contributors](https://github.com/twopoint718/reactive_record/graphs/contributors)!
Also a special thanks to Joe Nelson,
[@begriffs](https://github.com/begriffs), for contributions and
inspiration; Reactive Record would not exist without his efforts. Thanks to [Bendyworks](http://bendyworks.com/) for the 20%
time to work on this project!
And, of *course*, a huge thank you to Kelly Rauwerdink for her amazing ability to make "an art" even when all I can do is sorta half-articulate what I'm talking about. Thanks!
