public
Fork of vigetlabs/foreign_key_migrations
Description: A gem/plugin for ActiveRecord that lets you define foreign keys in migrations.
Clone URL: git://github.com/crnixon/foreign_key_migrations.git
added ability to create foreign keys with .references and .belongs_to
crnixon (author)
Tue Jul 15 12:27:50 -0700 2008
commit  09363322235d68e723d30d433352c09492780ed9
tree    1fd81762ce76e76176f2cca6b1ae53809ffa4c7a
parent  c55e20743ac60efbe2da1505f907e1534743d0e2
...
14
15
16
17
 
18
19
20
...
28
29
30
 
 
 
 
 
 
 
 
 
 
 
 
 
31
32
33
...
14
15
16
 
17
18
19
20
...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
0
@@ -14,7 +14,7 @@ via this plugin are currently supported for the following database adapters:
0
 * SQLite
0
 * SQLite3
0
 
0
-Foreign keys can be specified in three ways, modeled on indexes.
0
+Foreign keys can be specified in four ways, modeled on indexes and current ActiveRecord migration syntax.
0
 
0
 * In a column definition:
0
 
0
@@ -28,6 +28,19 @@ Foreign keys can be specified in three ways, modeled on indexes.
0
       
0
     end
0
     
0
+* In a column definition, using @references@ or @belongs_to@:
0
+
0
+ class AddUsersTable < ActiveRecord::Migration
0
+
0
+ def self.up
0
+ create_table :users do |t|
0
+ t.references :department
0
+ # t.belongs_to :department also works
0
+ end
0
+ end
0
+
0
+ end
0
+
0
 * In a table definition:
0
 
0
     class AddUsersTable < ActiveRecord::Migration
...
7
8
9
 
 
10
11
12
...
14
15
16
 
 
 
 
 
 
 
 
 
 
17
18
19
...
7
8
9
10
11
12
13
14
...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
0
@@ -7,6 +7,8 @@ module Viget
0
           attr_accessor :constraints, :name
0
           alias_method_chain :column, :foreign_keys
0
           alias_method_chain :to_sql, :foreign_keys
0
+ alias_method_chain :references, :foreign_keys
0
+ alias_method_chain :belongs_to, :foreign_keys
0
         end
0
       end
0
         
0
@@ -14,6 +16,16 @@ module Viget
0
         parse_foreign_key_from_column(name, options)
0
         column_without_foreign_keys(name, type, options)
0
       end
0
+
0
+ def references_with_foreign_keys(name, options = {})
0
+ unless options[:polymorphic]
0
+ parse_foreign_key_from_column("#{name}_id",
0
+ options.merge({:references => name.to_s.pluralize}))
0
+ end
0
+ references_without_foreign_keys(name, options)
0
+ end
0
+
0
+ alias :belongs_to_with_foreign_keys :references_with_foreign_keys
0
             
0
       def foreign_key(params)
0
         unless @base.respond_to?(:supports_foreign_keys?) && @base.supports_foreign_keys?
...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
26
27
...
37
38
39
40
 
41
42
43
...
51
52
53
54
 
55
56
57
...
65
66
67
68
 
69
70
71
...
79
80
81
82
 
83
84
85
...
94
95
96
97
 
98
99
100
...
8
9
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
...
50
51
52
 
53
54
55
56
...
64
65
66
 
67
68
69
70
...
78
79
80
 
81
82
83
84
...
92
93
94
 
95
96
97
98
...
107
108
109
 
110
111
112
113
0
@@ -8,20 +8,33 @@ class TestCreateTable < Test::Unit::TestCase
0
       @adapter.stubs(:supports_foreign_keys?).returns(true)
0
     end
0
     
0
- # should "create a foreign key on table.references" do
0
- # @adapter.expects(:execute).with(
0
- # 'CREATE TABLE users (' +
0
- # 'id primary_key, ' +
0
- # 'department_id integer NOT NULL, ' +
0
- # 'CONSTRAINT fk_users_department_id_departments_id ' +
0
- # 'FOREIGN KEY (department_id) REFERENCES departments(id)) '
0
- # ).returns(true)
0
- #
0
- # @adapter.create_table :users do |t|
0
- # t.references :departments, :null => false
0
- # end
0
- #
0
- # end
0
+ should "create a foreign key on table.references" do
0
+ @adapter.expects(:execute).with(
0
+ 'CREATE TABLE users (' +
0
+ 'id primary_key, ' +
0
+ 'department_id integer NOT NULL, ' +
0
+ 'CONSTRAINT fk_users_department_id_departments_id ' +
0
+ 'FOREIGN KEY (department_id) REFERENCES departments(id)) '
0
+ ).returns(true)
0
+
0
+ @adapter.create_table :users do |t|
0
+ t.references :department, :null => false
0
+ end
0
+ end
0
+
0
+ should "create a foreign key on table.belongs_to" do
0
+ @adapter.expects(:execute).with(
0
+ 'CREATE TABLE users (' +
0
+ 'id primary_key, ' +
0
+ 'department_id integer NOT NULL, ' +
0
+ 'CONSTRAINT fk_users_department_id_departments_id ' +
0
+ 'FOREIGN KEY (department_id) REFERENCES departments(id)) '
0
+ ).returns(true)
0
+
0
+ @adapter.create_table :users do |t|
0
+ t.belongs_to :department, :null => false
0
+ end
0
+ end
0
     
0
     should "allow simple foreign key definitions" do
0
       @adapter.expects(:execute).with(
0
@@ -37,7 +50,7 @@ class TestCreateTable < Test::Unit::TestCase
0
       end
0
     end
0
 
0
- should "allow_action_options" do
0
+ should "allow action options" do
0
       @adapter.expects(:execute).with(
0
         'CREATE TABLE users (' +
0
         'id primary_key, ' +
0
@@ -51,7 +64,7 @@ class TestCreateTable < Test::Unit::TestCase
0
       end
0
     end
0
 
0
- should "allow_multiple_action_options" do
0
+ should "allow multiple action options" do
0
       @adapter.expects(:execute).with(
0
         'CREATE TABLE users (' +
0
         'id primary_key, ' +
0
@@ -65,7 +78,7 @@ class TestCreateTable < Test::Unit::TestCase
0
       end
0
     end
0
 
0
- should "allow_complex_foreign_key_definitions" do
0
+ should "allow complex foreign key definitions" do
0
       @adapter.expects(:execute).with(
0
         'CREATE TABLE users (' +
0
         'id primary_key, ' +
0
@@ -79,7 +92,7 @@ class TestCreateTable < Test::Unit::TestCase
0
       end
0
     end
0
     
0
- should "allow_constraint_syntax" do
0
+ should "allow constraint syntax" do
0
       @adapter.expects(:execute).with(
0
         'CREATE TABLE users (' +
0
         'id primary_key, ' +
0
@@ -94,7 +107,7 @@ class TestCreateTable < Test::Unit::TestCase
0
       end
0
     end
0
 
0
- should "allow_complex_constraint_syntax" do
0
+ should "allow complex constraint syntax" do
0
       @adapter.expects(:execute).with(
0
         'CREATE TABLE users (' +
0
         'id primary_key, ' +

Comments

    No one has commented yet.