pg_morph 0.3.0 → 1.0.0
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.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/.travis.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +38 -2
- data/Guardfile +9 -0
- data/README.md +16 -4
- data/Rakefile +7 -0
- data/lib/pg_morph/adapter.rb +6 -5
- data/lib/pg_morph/naming.rb +0 -8
- data/lib/pg_morph/polymorphic.rb +89 -49
- data/lib/pg_morph/version.rb +1 -1
- data/pg_morph.gemspec +3 -1
- data/spec/dummy/db/schema.rb +11 -14
- data/spec/lib/pg_morph/adapter_integration_spec.rb +263 -0
- data/spec/{pg_morph → lib/pg_morph}/adapter_spec.rb +0 -9
- data/spec/{pg_morph → lib/pg_morph}/naming_spec.rb +0 -4
- data/spec/lib/pg_morph/polymorphic_integration_spec.rb +125 -0
- data/spec/{pg_morph → lib/pg_morph}/polymorphic_spec.rb +44 -34
- data/spec/spec_helper.rb +8 -0
- metadata +65 -51
- data/spec/pg_morph/adapter_integration_spec.rb +0 -89
- data/spec/pg_morph/polymorphic_integration_spec.rb +0 -86
@@ -1,86 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe PgMorph::Polymorphic do
|
4
|
-
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
|
5
|
-
include PgMorph::Adapter
|
6
|
-
|
7
|
-
def run(query)
|
8
|
-
ActiveRecord::Base.connection.select_value(query)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
before do
|
13
|
-
@adapter = ActiveRecord::Base.connection
|
14
|
-
@comments_polymorphic = PgMorph::Polymorphic.new(:likes, :comments, column: :likeable)
|
15
|
-
@posts_polymorphic = PgMorph::Polymorphic.new(:likes, :posts, column: :likeable)
|
16
|
-
begin
|
17
|
-
Like.destroy_all
|
18
|
-
Comment.destroy_all
|
19
|
-
@adapter.remove_polymorphic_foreign_key(:likes, :comments, column: :likeable)
|
20
|
-
@adapter.remove_polymorphic_foreign_key(:likes, :posts, column: :likeable)
|
21
|
-
rescue
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe '#create_trigger_body' do
|
26
|
-
before do
|
27
|
-
@adapter.stub(:raise_unless_postgres)
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'raises error for updating trigger with duplicated partition' do
|
31
|
-
@adapter.add_polymorphic_foreign_key(:likes, :comments, column: :likeable)
|
32
|
-
|
33
|
-
-> { @comments_polymorphic.send(:create_trigger_body) }
|
34
|
-
.should raise_error PG::Error
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'updates trigger with new partition' do
|
38
|
-
@adapter.add_polymorphic_foreign_key(:likes, :comments, column: :likeable)
|
39
|
-
|
40
|
-
@posts_polymorphic.send(:create_trigger_body).squeeze(' ').should == %Q{
|
41
|
-
IF (NEW.likeable_type = 'Comment') THEN
|
42
|
-
INSERT INTO likes_comments VALUES (NEW.*);
|
43
|
-
ELSIF (NEW.likeable_type = 'Post') THEN
|
44
|
-
INSERT INTO likes_posts VALUES (NEW.*);
|
45
|
-
}.squeeze(' ')
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
describe '#create_before_insert_trigger_sql' do
|
50
|
-
it 'returns sql' do
|
51
|
-
@comments_polymorphic.create_before_insert_trigger_sql.squeeze(' ').should == %Q{
|
52
|
-
DROP TRIGGER IF EXISTS likes_likeable_insert_trigger ON likes;
|
53
|
-
CREATE TRIGGER likes_likeable_insert_trigger
|
54
|
-
BEFORE INSERT ON likes
|
55
|
-
FOR EACH ROW EXECUTE PROCEDURE likes_likeable_fun();
|
56
|
-
}.squeeze(' ')
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
describe '#remove_partition_table' do
|
61
|
-
it 'returns sql' do
|
62
|
-
@adapter.add_polymorphic_foreign_key(:likes, :comments, column: :likeable)
|
63
|
-
|
64
|
-
@comments_polymorphic.remove_partition_table.squeeze(' ').should == %Q{ DROP TABLE IF EXISTS likes_comments; }
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
describe 'remove_after_insert_trigger_sql' do
|
69
|
-
it 'returns sql' do
|
70
|
-
@adapter.add_polymorphic_foreign_key(:likes, :comments, column: :likeable)
|
71
|
-
|
72
|
-
@comments_polymorphic.remove_after_insert_trigger_sql.squeeze(' ').should == %Q{
|
73
|
-
DROP TRIGGER likes_after_insert_trigger ON likes;
|
74
|
-
DROP FUNCTION delete_from_likes_master_fun();
|
75
|
-
}.squeeze(' ')
|
76
|
-
end
|
77
|
-
|
78
|
-
it 'returns empty string if there are more partitions' do
|
79
|
-
@adapter.add_polymorphic_foreign_key(:likes, :comments, column: :likeable)
|
80
|
-
@adapter.add_polymorphic_foreign_key(:likes, :posts, column: :likeable)
|
81
|
-
|
82
|
-
@comments_polymorphic.remove_after_insert_trigger_sql.squeeze(' ').should == ''
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
end
|