sbfaulkner-sequel_cascading 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +14 -1
- data/lib/sequel_cascading.rb +6 -0
- data/sequel_cascading.gemspec +1 -1
- data/test/sequel_cascading_test.rb +39 -2
- metadata +1 -1
data/README.markdown
CHANGED
@@ -30,12 +30,25 @@ Install the gem(s):
|
|
30
30
|
|
31
31
|
class Post < Sequel::Model
|
32
32
|
one_to_many :comment, :order => :name
|
33
|
+
|
34
|
+
# destroy any associated records too
|
33
35
|
is :cascading, :destroy => :comments
|
36
|
+
|
37
|
+
# or, if you want to null the foreign key instead, you could...
|
38
|
+
# is :cascading, :nullify => :comments
|
39
|
+
|
40
|
+
# or, if you want the destroy to fail, you would...
|
41
|
+
# is :cascading, :restrict => :comments
|
34
42
|
end
|
43
|
+
|
44
|
+
## UPDATES
|
45
|
+
|
46
|
+
### 1.0.3
|
47
|
+
|
48
|
+
- added support for :nullify and :restrict options
|
35
49
|
|
36
50
|
## TODO
|
37
51
|
|
38
|
-
- add tests
|
39
52
|
- add :null and :deny support?
|
40
53
|
- publish in sequel www/pages/plugins
|
41
54
|
|
data/lib/sequel_cascading.rb
CHANGED
@@ -5,6 +5,12 @@ module Sequel
|
|
5
5
|
Array(options[:destroy]).each do |assoc|
|
6
6
|
model.instance_eval "before_destroy { #{assoc}_dataset.destroy }"
|
7
7
|
end
|
8
|
+
Array(options[:nullify]).each do |assoc|
|
9
|
+
model.instance_eval "before_destroy { remove_all_#{assoc} }"
|
10
|
+
end
|
11
|
+
Array(options[:restrict]).each do |assoc|
|
12
|
+
model.instance_eval "before_destroy { raise Error::InvalidOperation, 'Delete would orphan associated #{assoc}' unless #{assoc}_dataset.empty? }"
|
13
|
+
end
|
8
14
|
end
|
9
15
|
end
|
10
16
|
end
|
data/sequel_cascading.gemspec
CHANGED
@@ -3,7 +3,6 @@ require 'test/unit'
|
|
3
3
|
require 'sequel'
|
4
4
|
|
5
5
|
DB = Sequel.connect('sqlite:/')
|
6
|
-
# DB.execute("SET client_min_messages TO 'WARNING'")
|
7
6
|
|
8
7
|
class Author < Sequel::Model
|
9
8
|
set_schema do
|
@@ -21,7 +20,9 @@ class Post < Sequel::Model
|
|
21
20
|
create_table!
|
22
21
|
many_to_one :author
|
23
22
|
one_to_many :comments
|
24
|
-
|
23
|
+
one_to_many :nulled_items
|
24
|
+
one_to_many :restricted_items
|
25
|
+
is :cascading, :destroy => :comments, :nullify => :nulled_items, :restrict => :restricted_items
|
25
26
|
end
|
26
27
|
|
27
28
|
class Comment < Sequel::Model
|
@@ -33,6 +34,24 @@ class Comment < Sequel::Model
|
|
33
34
|
many_to_one :post
|
34
35
|
end
|
35
36
|
|
37
|
+
class NulledItem < Sequel::Model
|
38
|
+
set_schema do
|
39
|
+
primary_key :id
|
40
|
+
foreign_key :post_id, :posts
|
41
|
+
end
|
42
|
+
create_table!
|
43
|
+
many_to_one :post
|
44
|
+
end
|
45
|
+
|
46
|
+
class RestrictedItem < Sequel::Model
|
47
|
+
set_schema do
|
48
|
+
primary_key :id
|
49
|
+
foreign_key :post_id, :posts
|
50
|
+
end
|
51
|
+
create_table!
|
52
|
+
many_to_one :post
|
53
|
+
end
|
54
|
+
|
36
55
|
class SequelCascadingTest < Test::Unit::TestCase
|
37
56
|
def test_should_leave_orphans_when_destroyed
|
38
57
|
author = Author.create
|
@@ -40,6 +59,7 @@ class SequelCascadingTest < Test::Unit::TestCase
|
|
40
59
|
author.destroy
|
41
60
|
assert !author.exists?
|
42
61
|
assert post.exists?
|
62
|
+
assert_equal author.id, post.author_id
|
43
63
|
end
|
44
64
|
|
45
65
|
def test_should_destroy_children_when_destroyed
|
@@ -49,4 +69,21 @@ class SequelCascadingTest < Test::Unit::TestCase
|
|
49
69
|
assert !post.exists?
|
50
70
|
assert !comment.exists?
|
51
71
|
end
|
72
|
+
|
73
|
+
def test_should_nullify_children_when_destroyed
|
74
|
+
post = Post.create
|
75
|
+
nulled = post.add_nulled_item(NulledItem.new)
|
76
|
+
post.destroy
|
77
|
+
assert !post.exists?
|
78
|
+
assert nulled.exists?
|
79
|
+
assert_nil post.author_id
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_should_be_restricted_by_children_when_destroyed
|
83
|
+
post = Post.create
|
84
|
+
restricted = post.add_restricted_item(RestrictedItem.new)
|
85
|
+
assert_raises(Sequel::Error::InvalidOperation) { post.destroy }
|
86
|
+
assert post.exists?
|
87
|
+
assert restricted.exists?
|
88
|
+
end
|
52
89
|
end
|