Skip to content
forked from hosh/foreigner

Foreigner -- Foreign Key Constraints for PostgreSQL and MySQL. *Thoroughly* tested via rspec.

License

Notifications You must be signed in to change notification settings

sparkfly/foreigner

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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.0 beta6

PostgreSQL (tested against 8.3.9, 8.4.2)
– “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 / Gentoo mysql-5.0.90-r2 hardened x86_64)
– “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 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.

About

Foreigner -- Foreign Key Constraints for PostgreSQL and MySQL. *Thoroughly* tested via rspec.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%