rein 0.8.2 → 0.8.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rein/constraint/foreign_key.rb +6 -2
- data/lib/rein/version.rb +1 -1
- data/spec/rein/constraint/foreign_key_spec.rb +10 -10
- metadata +2 -8
@@ -4,7 +4,7 @@ module RC
|
|
4
4
|
referencing_attribute = (options[:referencing] || "#{referenced_table.to_s.singularize}_id").to_sym
|
5
5
|
referenced_attribute = (options[:referenced] || "id").to_sym
|
6
6
|
|
7
|
-
name =
|
7
|
+
name = options[:name] || default_constraint_name(referencing_table, referencing_attribute)
|
8
8
|
|
9
9
|
sql = "ALTER TABLE #{referencing_table}".tap do |sql|
|
10
10
|
sql << " ADD CONSTRAINT #{name}"
|
@@ -24,7 +24,7 @@ module RC
|
|
24
24
|
def remove_foreign_key_constraint(referencing_table, referenced_table, options = {})
|
25
25
|
referencing_attribute = options[:referencing] || "#{referenced_table.to_s.singularize}_id".to_sym
|
26
26
|
|
27
|
-
name = options[:name] ||
|
27
|
+
name = options[:name] || default_constraint_name(referencing_table, referencing_attribute)
|
28
28
|
|
29
29
|
if is_a_mysql_adapter?
|
30
30
|
execute "ALTER TABLE #{referencing_table} DROP FOREIGN KEY #{name}"
|
@@ -58,5 +58,9 @@ module RC
|
|
58
58
|
def is_a_mysql_adapter?
|
59
59
|
self.class.to_s =~ /Mysql[2]?Adapter/
|
60
60
|
end
|
61
|
+
|
62
|
+
def default_constraint_name(referencing_table, referencing_attribute)
|
63
|
+
"#{referencing_table}_#{referencing_attribute}_fk".to_sym
|
64
|
+
end
|
61
65
|
end
|
62
66
|
end
|
data/lib/rein/version.rb
CHANGED
@@ -17,22 +17,22 @@ describe RC::ForeignKey do
|
|
17
17
|
|
18
18
|
context "with no options" do
|
19
19
|
before { adapter.add_foreign_key_constraint(:books, :people) }
|
20
|
-
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT
|
20
|
+
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT books_person_id_fk FOREIGN KEY (person_id) REFERENCES people (id)") }
|
21
21
|
end
|
22
22
|
|
23
23
|
context "with a given referencing attribute" do
|
24
24
|
before { adapter.add_foreign_key_constraint(:books, :people, :referencing => :author_id) }
|
25
|
-
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT
|
25
|
+
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT books_author_id_fk FOREIGN KEY (author_id) REFERENCES people (id)") }
|
26
26
|
end
|
27
27
|
|
28
28
|
context "with a given referenced attribute" do
|
29
29
|
before { adapter.add_foreign_key_constraint(:books, :people, :referenced => :person_id) }
|
30
|
-
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT
|
30
|
+
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT books_person_id_fk FOREIGN KEY (person_id) REFERENCES people (person_id)") }
|
31
31
|
end
|
32
32
|
|
33
33
|
context "with a given referencing attribute and referenced attribute" do
|
34
34
|
before { adapter.add_foreign_key_constraint(:books, :people, :referencing => :author_id, :referenced => :person_id) }
|
35
|
-
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT
|
35
|
+
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT books_author_id_fk FOREIGN KEY (author_id) REFERENCES people (person_id)") }
|
36
36
|
end
|
37
37
|
|
38
38
|
context "with a given name" do
|
@@ -42,22 +42,22 @@ describe RC::ForeignKey do
|
|
42
42
|
|
43
43
|
context "with a given on delete referential action" do
|
44
44
|
before { adapter.add_foreign_key_constraint(:books, :people, :on_delete => :cascade) }
|
45
|
-
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT
|
45
|
+
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT books_person_id_fk FOREIGN KEY (person_id) REFERENCES people (id) ON DELETE CASCADE") }
|
46
46
|
end
|
47
47
|
|
48
48
|
context "with a given on update referential action" do
|
49
49
|
before { adapter.add_foreign_key_constraint(:books, :people, :on_update => :cascade) }
|
50
|
-
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT
|
50
|
+
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT books_person_id_fk FOREIGN KEY (person_id) REFERENCES people (id) ON UPDATE CASCADE") }
|
51
51
|
end
|
52
52
|
|
53
53
|
context "with a 'cascade' on delete and update referential action" do
|
54
54
|
before { adapter.add_foreign_key_constraint(:books, :people, :on_delete => :cascade, :on_update => :cascade) }
|
55
|
-
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT
|
55
|
+
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT books_person_id_fk FOREIGN KEY (person_id) REFERENCES people (id) ON DELETE CASCADE ON UPDATE CASCADE") }
|
56
56
|
end
|
57
57
|
|
58
58
|
context "with a 'no action' on delete and update referential action" do
|
59
59
|
before { adapter.add_foreign_key_constraint(:books, :people, :on_delete => :no_action, :on_update => :no_action) }
|
60
|
-
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT
|
60
|
+
it { should have_received.execute("ALTER TABLE books ADD CONSTRAINT books_person_id_fk FOREIGN KEY (person_id) REFERENCES people (id) ON DELETE NO ACTION ON UPDATE NO ACTION") }
|
61
61
|
end
|
62
62
|
|
63
63
|
describe "with a given add_index option" do
|
@@ -81,12 +81,12 @@ describe RC::ForeignKey do
|
|
81
81
|
|
82
82
|
context "with no options" do
|
83
83
|
before { adapter.remove_foreign_key_constraint(:books, :people) }
|
84
|
-
it { should have_received.execute("ALTER TABLE books DROP CONSTRAINT
|
84
|
+
it { should have_received.execute("ALTER TABLE books DROP CONSTRAINT books_person_id_fk") }
|
85
85
|
end
|
86
86
|
|
87
87
|
context "with a given referencing attribute" do
|
88
88
|
before { adapter.remove_foreign_key_constraint(:books, :people, :referencing => :author_id) }
|
89
|
-
it { should have_received.execute("ALTER TABLE books DROP CONSTRAINT
|
89
|
+
it { should have_received.execute("ALTER TABLE books DROP CONSTRAINT books_author_id_fk") }
|
90
90
|
end
|
91
91
|
|
92
92
|
context "with a given name" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rein
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -132,18 +132,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
132
132
|
- - ! '>='
|
133
133
|
- !ruby/object:Gem::Version
|
134
134
|
version: '0'
|
135
|
-
segments:
|
136
|
-
- 0
|
137
|
-
hash: 2852352367581315615
|
138
135
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
136
|
none: false
|
140
137
|
requirements:
|
141
138
|
- - ! '>='
|
142
139
|
- !ruby/object:Gem::Version
|
143
140
|
version: '0'
|
144
|
-
segments:
|
145
|
-
- 0
|
146
|
-
hash: 2852352367581315615
|
147
141
|
requirements: []
|
148
142
|
rubyforge_project: rein
|
149
143
|
rubygems_version: 1.8.23
|