Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Bassett committed Apr 12, 2017
1 parent 0e01301 commit 0a92ccc
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ database integrity without resorting to hand-crafted SQL. Rein (pronounced
"rain") adds a handful of methods to your ActiveRecord migrations so that you
can easily tame the data in your database.

All methods in the DSL are automatically *reversible*, so you can take
advantage of reversible Rails migrations.

## Table of contents

* [Rein](#rein)
Expand All @@ -34,7 +37,24 @@ can easily tame the data in your database.

Install the gem:

gem install rein
```
> gem install rein
```

Add a constraint to your migrations:

```ruby
class CreateAuthorsTable < ActiveRecord::Migration
def change
create_table :authors do |t|
t.string :name, null: false
end

# An author must have a name.
add_presence_constraint :authors, :name
end
end
```

## Constraint types

Expand All @@ -50,6 +70,14 @@ For example, let's say that we want to constrain the `author_id` column in the
add_foreign_key_constraint :books, :authors
```

Adding a foreign key doesn't automatically create an index on the referenced
column. Having an index will generally speed up any joins you perform on the
foreign key. To create an index you can specify the `index` option:

```ruby
add_foreign_key_constraint :books, :authors, index: true
```

Rein will automatically infer the column names for the tables, but if we need
to be explicit we can using the `referenced` and `referencing` options:

Expand Down Expand Up @@ -85,8 +113,6 @@ remove_foreign_key_constraint :books, :authors

### Inclusion constraints

*(PostgreSQL only)*

An inclusion constraint specifies the possible values that a column value can
take.

Expand Down Expand Up @@ -122,8 +148,6 @@ add_inclusion_constraint :books, :state,

### Numericality constraints

*(PostgreSQL only)*

A numericality constraint specifies the range of values that a numeric column
value can take.

Expand Down Expand Up @@ -172,8 +196,6 @@ remove_numericality_constraint :books, :publication_month

### Presence constraints

*(PostgreSQL only)*

A presence constraint ensures that a string column value is non-empty.

A `NOT NULL` constraint will be satisfied by an empty string, but sometimes may
Expand Down Expand Up @@ -204,8 +226,6 @@ remove_presence_constraint :books, :title

### Null constraints

*(PostgreSQL only)*

A null constraint ensures that a column does *not* contain a null value. This
is the same as adding `NOT NULL` to a column, the difference being that it can
be _applied conditionally_.
Expand All @@ -227,8 +247,6 @@ remove_null_constraint :books, :due_date

### Enumerated types

*(PostgreSQL only)*

An enum is a data type that represents a static, ordered set of values.

```ruby
Expand Down Expand Up @@ -261,8 +279,6 @@ drop_view :available_books

## Schemas

*(PostgreSQL only)*

A database can contain one or more named schemas, which in turn contain tables.
Sometimes it might be helpful to split your database into multiple schemas to
logically group tables together.
Expand Down

0 comments on commit 0a92ccc

Please sign in to comment.