Foreigner -- Foreign Key Constraints for PostgreSQL and MySQL. *Thoroughly* tested via rspec.
Ruby
Pull request Compare This branch is 95 commits ahead of dwilkie:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
tasks
.gitignore
.rbenv-version [ARCH] Added default rbenv 1.8.7 Aug 28, 2011
.rspec
.travis.yml
Gemfile
Gemfile.lock
MIT-LICENSE
README.textile
Rakefile
VERSION

README.textile

Sparkfly Foreigner

sparkfly-foreigner is a fork of Matt Higgins Foreigner and the dwilkie-foreigner

Sparkfly requires an enterprise-ready Rails environment, which means we need solid PostgreSQL support and referential integrity.
Further, all of our plugins must be thoroughly tested. To make sure that PostgreSQL support is where we want it to be, the work
done by Matt Higgins and D. Wilkie were forked and all the tests were converted to use rspec. Dependency on plugin_test_helper
was taken out.

Test Coverage

sparkfly-foreigner runs on Travis CI, and you can see the results
tested against Ruby 1.8.7, 1.9.2, and 1.9.3 across all three databases.

These tests were created and tested against our target platform, Rails 3. If you are using Rails 2.3.5,
use sparkfly-foreigner 0.6.0. this currently works with Rails 3.0 beta 3.

Rspec coverage was against Rspec 2.×. (Currently 2.5)

PostgreSQL (tested against 8.3.9, 8.4.2, 9.0.3)
– “Schema” tests: do the table migrations actually add referential integrity?
– “Schema Extractor” tests: does the foreign_key() method extract out the foreign key from information_schema?
– “Semantics” tests: do the add/drop constraints emit proper SQL?

MySQL (tested against 5.0.90, 5.1.56)
– “Schema” tests: do the table migrations actually add referential integrity?
– “Schema Extractor” tests: does the foreign_key() method extract out the foreign key from information_schema?
– “Semantics” tests: do the add/drop constraints emit proper SQL?

SQLite3 (tested against 3.6.22, 3.7.6.3 in-memory only)
– “Semantics” tests: do the add/drop constraints emit proper SQL?
– Warning: sqlite3 probably does not actually work when you use it.

Schema Dumper
– Are we emitting the correct schema.rb syntax for adding foreign key constraints?

TODO:
– CHECK Constraints (PostgreSQL-only)
– CHECK Constraints implemented as triggers for MySQL. This is unlikely to be supported since we don’t plan to deploy to MySQL

Usage

Installation

Add this to your Gemfile:


gem ‘sparkfly-foreigner’, :require => ‘foreigner’

Migrations

To specify a different column name you can do the following:


create_table :comments do |t|
t.integer :article_id, :null => false
t.foreign_key :post, :column => :article_id
end

To specify dependencies (nullify or delete) you can do the following:


create_table :comments do |t|
t.references :post, :foreign_key => {:dependent => :delete}, :null => false
end

Here’s another example using a different column name and the dependent option


create_table :comments do |t|
t.integer :article_id, :null => false
t.foreign_key :post, :column => :article_id, :dependent => :nullify
end

To specify a reference with a foreign key constraint using Rails convention:


create_table :comments do |t|
t.references :post, :foreign_key => true, :null => false
end

Read the specs for more example usage.

schema.rb

All of the constrants are updated in schema.rb when you either:


rake db:migrate
rake db:schema:dump

This allows you to see the state of your migratons and
take advantage of using
rake db:schema:load

Testing

You will need rspec 2.0. At the time of this writing, you install it with:


gem install rspec —pre

You will also want to make sure you have Rubygems 1.3.6

Run “rake spec” to run all the spec. Make sure that all of the PostgreSQL and MySQL databases defined in
spec/spec_helper.rb are created before you run those tests.

Copyrights

Copyright © 2010 Ho-Sheng Hsiao, released under the MIT license

Modified from code written by David Wilkie and Matt Higgins. See http://github.com/sparkfly/foreigner for details on
what got changed from what.

Contributions:
– theaboutbox https://github.com/theaboutbox/foreigner