pg_triggers 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5fd69d976d7bfa873479a91959a658a6759d5865
4
+ data.tar.gz: a680642cbb7969e151ad170fccbb82d63c95f5e4
5
+ SHA512:
6
+ metadata.gz: 2545b586d2434cca289761fbe9e3cb0011a7fd1069cb7ddeacd7ac36f682b51107eb2bb54b2d88cc77db5a217f5e0170d0958379c05f10b9901b0a341bdf2dbf
7
+ data.tar.gz: 14749fe98fbe669e2326801d0a27bf36714ec5f62709d9f879f9e611ad44bb7d6bff1764e8fe13f6844d9c0893772bfd0ec153323cf445ebfd0d7e9fa49523cf
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ### 0.0.1 (2014-08-21)
2
+
3
+ * Initial release.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pg_triggers.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Chris Hanks
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # PgTriggers
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pg_triggers'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pg_triggers
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/pg_triggers/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,52 @@
1
+ require "pg_triggers/version"
2
+
3
+ module PgTriggers
4
+ class << self
5
+ def counter_cache(main_table, counter_column, counted_table, relationship, options = {})
6
+ where = proc { |source| relationship.map{|k, v| "#{k} = #{source}.#{v}"}.join(' AND ') }
7
+ columns = relationship.values
8
+ changed = columns.map{|c| "((OLD.#{c} <> NEW.#{c}) OR (OLD.#{c} IS NULL <> NEW.#{c} IS NULL))"}.join(' OR ')
9
+
10
+ condition = proc do |source|
11
+ a = []
12
+ a << columns.map{|c| "#{source}.#{c} IS NOT NULL"}.join(' AND ')
13
+ a << options[:where].gsub('ROW.', "#{source}.") if options[:where]
14
+ a.join(' AND ')
15
+ end
16
+
17
+ <<-SQL
18
+ CREATE FUNCTION pg_triggers_counter_#{main_table}_#{counter_column}() RETURNS trigger
19
+ LANGUAGE plpgsql
20
+ AS $$
21
+ BEGIN
22
+ IF (TG_OP = 'INSERT') THEN
23
+ IF (#{condition['NEW']}) THEN
24
+ UPDATE #{main_table} SET #{counter_column} = #{counter_column} + 1 WHERE #{where['NEW']};
25
+ END IF;
26
+ RETURN NEW;
27
+ ELSIF (TG_OP = 'UPDATE') THEN
28
+ IF (#{changed}) THEN
29
+ IF (#{condition['OLD']}) THEN
30
+ UPDATE #{main_table} SET #{counter_column} = #{counter_column} - 1 WHERE #{where['OLD']};
31
+ END IF;
32
+ IF (#{condition['NEW']}) THEN
33
+ UPDATE #{main_table} SET #{counter_column} = #{counter_column} + 1 WHERE #{where['NEW']};
34
+ END IF;
35
+ END IF;
36
+ RETURN NEW;
37
+ ELSIF (TG_OP = 'DELETE') THEN
38
+ IF (#{condition['OLD']}) THEN
39
+ UPDATE #{main_table} SET #{counter_column} = #{counter_column} - 1 WHERE #{where['OLD']};
40
+ END IF;
41
+ RETURN OLD;
42
+ END IF;
43
+ END;
44
+ $$;
45
+
46
+ CREATE TRIGGER pg_triggers_counter_#{main_table}_#{counter_column}
47
+ AFTER INSERT OR UPDATE OR DELETE ON #{counted_table}
48
+ FOR EACH ROW EXECUTE PROCEDURE pg_triggers_counter_#{main_table}_#{counter_column}();
49
+ SQL
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module PgTriggers
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pg_triggers/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pg_triggers"
8
+ spec.version = PgTriggers::VERSION
9
+ spec.authors = ["Chris Hanks"]
10
+ spec.email = ["christopher.m.hanks@gmail.com"]
11
+ spec.summary = %q{Helpers for Postgres Triggers}
12
+ spec.description = %q{Handy helpers to create PostgreSQL Triggers}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "sequel"
25
+ spec.add_development_dependency "pg"
26
+ end
@@ -0,0 +1,182 @@
1
+ require 'spec_helper'
2
+
3
+ describe PgTriggers, 'counter_cache' do
4
+ before do
5
+ DB.drop_table?(:counter_table)
6
+ DB.drop_table?(:counted_table)
7
+ end
8
+
9
+ it "should increment and decrement the count of related items in the associated table as rows are inserted and updated and deleted" do
10
+ DB.create_table :counter_table do
11
+ integer :id, null: false
12
+ integer :counted_count, null: false, default: 0
13
+ end
14
+
15
+ DB.create_table :counted_table do
16
+ integer :id, null: false
17
+ integer :counter_id, null: false
18
+ end
19
+
20
+ DB.run PgTriggers.counter_cache :counter_table, :counted_count, :counted_table, {id: :counter_id}
21
+
22
+ DB[:counter_table].insert(id: 1)
23
+ DB[:counter_table].insert(id: 2)
24
+ DB[:counter_table].where(id: 1).get(:counted_count).should == 0
25
+
26
+ DB[:counted_table].insert(id: 1, counter_id: 1)
27
+ DB[:counter_table].where(id: 1).get(:counted_count).should == 1
28
+
29
+ DB[:counted_table].insert(id: 2, counter_id: 1)
30
+ DB[:counter_table].where(id: 1).get(:counted_count).should == 2
31
+
32
+ DB[:counted_table].insert(id: 3, counter_id: 2)
33
+ DB[:counter_table].where(id: 1).get(:counted_count).should == 2
34
+ DB[:counter_table].where(id: 2).get(:counted_count).should == 1
35
+
36
+ DB[:counted_table].insert(id: 4, counter_id: 1)
37
+ DB[:counter_table].where(id: 1).get(:counted_count).should == 3
38
+
39
+ DB[:counted_table].where(id: 4).update(counter_id: 2).should == 1
40
+ DB[:counter_table].where(id: 1).get(:counted_count).should == 2
41
+ DB[:counter_table].where(id: 2).get(:counted_count).should == 2
42
+
43
+ DB[:counted_table].where(counter_id: 1).delete.should == 2
44
+ DB[:counter_table].where(id: 1).get(:counted_count).should == 0
45
+ end
46
+
47
+ it "should work when the tables are related by multiple columns" do
48
+ DB.create_table :counter_table do
49
+ integer :id1, null: false
50
+ integer :id2, null: false
51
+ integer :counted_count, null: false, default: 0
52
+ end
53
+
54
+ DB.create_table :counted_table do
55
+ integer :id, null: false
56
+ integer :counter_id1, null: false
57
+ integer :counter_id2, null: false
58
+ end
59
+
60
+ DB.run PgTriggers.counter_cache :counter_table, :counted_count, :counted_table, {id1: :counter_id1, id2: :counter_id2}
61
+
62
+ DB[:counter_table].insert(id1: 1, id2: 1)
63
+ DB[:counter_table].insert(id1: 2, id2: 1)
64
+ DB[:counter_table].where(id1: 1).get(:counted_count).should == 0
65
+
66
+ DB[:counted_table].insert(id: 1, counter_id1: 1, counter_id2: 1)
67
+ DB[:counter_table].where(id1: 1).get(:counted_count).should == 1
68
+
69
+ DB[:counted_table].insert(id: 2, counter_id1: 1, counter_id2: 1)
70
+ DB[:counter_table].where(id1: 1).get(:counted_count).should == 2
71
+
72
+ DB[:counted_table].insert(id: 3, counter_id1: 2, counter_id2: 1)
73
+ DB[:counter_table].where(id1: 1).get(:counted_count).should == 2
74
+ DB[:counter_table].where(id1: 2).get(:counted_count).should == 1
75
+
76
+ DB[:counted_table].insert(id: 4, counter_id1: 1, counter_id2: 1)
77
+ DB[:counter_table].where(id1: 1).get(:counted_count).should == 3
78
+
79
+ DB[:counted_table].where(id: 4).update(counter_id1: 2).should == 1
80
+ DB[:counter_table].where(id1: 1).get(:counted_count).should == 2
81
+ DB[:counter_table].where(id1: 2).get(:counted_count).should == 2
82
+
83
+ DB[:counted_table].where(counter_id1: 1).delete.should == 2
84
+ DB[:counter_table].where(id1: 1).get(:counted_count).should == 0
85
+ end
86
+
87
+ it "should work as expected when any of the columns are set to null or an unknown value" do
88
+ DB.create_table :counter_table do
89
+ integer :id1, null: false
90
+ integer :id2, null: false
91
+ integer :counted1_count, null: false, default: 0
92
+ integer :counted2_count, null: false, default: 0
93
+ end
94
+
95
+ DB.create_table :counted_table do
96
+ integer :id, null: false
97
+ integer :counter_id1
98
+ integer :counter_id2
99
+ end
100
+
101
+ DB.run PgTriggers.counter_cache :counter_table, :counted1_count, :counted_table, {id1: :counter_id1}
102
+ DB.run PgTriggers.counter_cache :counter_table, :counted2_count, :counted_table, {id1: :counter_id1, id2: :counter_id2}
103
+
104
+ DB[:counter_table].insert(id1: 1, id2: 1)
105
+ DB[:counted_table].insert(id: 1, counter_id1: 1, counter_id2: 1)
106
+ DB[:counted_table].insert(id: 2, counter_id1: 1, counter_id2: 1)
107
+ DB[:counted_table].insert(id: 3, counter_id1: 1, counter_id2: 1)
108
+ DB[:counted_table].insert(id: 4, counter_id1: 1, counter_id2: 1)
109
+ DB[:counter_table].where(id1: 1, id2: 1).get([:counted1_count, :counted2_count]).should == [4, 4]
110
+
111
+ DB[:counted_table].where(id: 1).update(counter_id1: nil).should == 1
112
+ DB[:counter_table].where(id1: 1, id2: 1).get([:counted1_count, :counted2_count]).should == [3, 3]
113
+
114
+ DB[:counted_table].where(id: 2).update(counter_id2: nil).should == 1
115
+ DB[:counter_table].where(id1: 1, id2: 1).get([:counted1_count, :counted2_count]).should == [3, 2]
116
+
117
+ DB[:counted_table].where(id: 3).update(counter_id1: 90000000).should == 1
118
+ DB[:counter_table].where(id1: 1, id2: 1).get([:counted1_count, :counted2_count]).should == [2, 1]
119
+
120
+ DB[:counted_table].where(id: 4).update(counter_id2: 90000000).should == 1
121
+ DB[:counter_table].where(id1: 1, id2: 1).get([:counted1_count, :counted2_count]).should == [2, 0]
122
+
123
+ DB[:counted_table].insert(id: 5, counter_id1: 1, counter_id2: nil)
124
+ DB[:counter_table].where(id1: 1, id2: 1).get([:counted1_count, :counted2_count]).should == [3, 0]
125
+
126
+ DB[:counted_table].insert(id: 6, counter_id1: nil, counter_id2: nil)
127
+ DB[:counter_table].where(id1: 1, id2: 1).get([:counted1_count, :counted2_count]).should == [3, 0]
128
+ end
129
+
130
+ it "should accept a :where clause to filter the rows that are counted" do
131
+ DB.create_table :counter_table do
132
+ integer :id, null: false
133
+
134
+ integer :condition_count, null: false, default: 0
135
+ integer :value_count, null: false, default: 0
136
+ integer :condition_value_count, null: false, default: 0
137
+ end
138
+
139
+ DB.create_table :counted_table do
140
+ integer :id, null: false
141
+ integer :counter_id, null: false
142
+
143
+ boolean :condition
144
+ integer :value
145
+ end
146
+
147
+ DB.run PgTriggers.counter_cache :counter_table, :condition_count, :counted_table, {id: :counter_id}, where: "ROW.condition"
148
+ DB.run PgTriggers.counter_cache :counter_table, :value_count, :counted_table, {id: :counter_id}, where: "ROW.value > 5"
149
+ DB.run PgTriggers.counter_cache :counter_table, :condition_value_count, :counted_table, {id: :counter_id}, where: "ROW.condition AND ROW.value > 5"
150
+
151
+ def values
152
+ DB[:counter_table].where(id: 1).get([:condition_count, :value_count, :condition_value_count])
153
+ end
154
+
155
+ DB[:counter_table].insert(id: 1)
156
+
157
+ values.should == [0, 0, 0]
158
+ DB[:counted_table].insert(id: 1, counter_id: 1, condition: true, value: 4)
159
+ values.should == [1, 0, 0]
160
+ DB[:counted_table].insert(id: 2, counter_id: 1, condition: false, value: 4)
161
+ values.should == [1, 0, 0]
162
+ DB[:counted_table].insert(id: 3, counter_id: 1, condition: true, value: 6)
163
+ values.should == [2, 1, 1]
164
+ DB[:counted_table].insert(id: 4, counter_id: 1, condition: false, value: 6)
165
+ values.should == [2, 2, 1]
166
+ DB[:counted_table].insert(id: 5, counter_id: 1, condition: nil, value: 4)
167
+ values.should == [2, 2, 1]
168
+ DB[:counted_table].insert(id: 6, counter_id: 1, condition: false, value: nil)
169
+ values.should == [2, 2, 1]
170
+ DB[:counted_table].insert(id: 7, counter_id: 1, condition: true, value: nil)
171
+ values.should == [3, 2, 1]
172
+ DB[:counted_table].insert(id: 8, counter_id: 1, condition: nil, value: 6)
173
+ values.should == [3, 3, 1]
174
+
175
+ DB[:counted_table].where(id: 3).update(counter_id: 2).should == 1
176
+ values.should == [2, 2, 0]
177
+ DB[:counted_table].where(id: [4, 7]).delete.should == 2
178
+ values.should == [1, 1, 0]
179
+ DB[:counted_table].where(id: 3).update(counter_id: 1).should == 1
180
+ values.should == [2, 2, 1]
181
+ end
182
+ end
@@ -0,0 +1,14 @@
1
+ require 'sequel'
2
+ require 'pg_triggers'
3
+
4
+ url = ENV['PG_TRIGGERS_URL'] || 'postgres:///pg_triggers'
5
+
6
+ DB = Sequel.connect(url)
7
+
8
+ RSpec.configure do |config|
9
+ config.expect_with(:rspec) { |c| c.syntax = [:expect, :should] }
10
+
11
+ config.around do |example|
12
+ DB.transaction(rollback: :always, auto_savepoint: true) { example.run }
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_triggers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Hanks
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sequel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Handy helpers to create PostgreSQL Triggers
84
+ email:
85
+ - christopher.m.hanks@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - CHANGELOG.md
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/pg_triggers.rb
97
+ - lib/pg_triggers/version.rb
98
+ - pg_triggers.gemspec
99
+ - spec/counter_cache_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: ''
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.2.2
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Helpers for Postgres Triggers
125
+ test_files:
126
+ - spec/counter_cache_spec.rb
127
+ - spec/spec_helper.rb