sequel_postgresql_triggers 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b5de693db244fa94aba445980a43f06ecdb91cb8
4
- data.tar.gz: a0fb193ac3d11010daaace8c590eb5ab552bcdc1
3
+ metadata.gz: a388774a0a7f318413ccf24063648887c2d0c255
4
+ data.tar.gz: 94fda351f9e4a355471a06510086bdef2b5adac2
5
5
  SHA512:
6
- metadata.gz: 88098e2ec7f2e97e8ed193689fe12b7f7f631e28821869ef25955d19723df943742c8b2c0d594d0044cf068b6532e28682a09fa807412040c652b1974a7685e0
7
- data.tar.gz: d7fdef313991baed60bf2c5efe719bde984a8484f007b88ad0b08ec3382b2a1ee0b207412413994361faef917d8079d64565177062c94dbf4507fcae8d13e88c
6
+ metadata.gz: 36dbc0cf115d5ac8251d1fdef1062f229f91312e4903628f4e358c3d5d1e926b30f12dba25cd625901464165984f9bcc5f49c90cddcfa8cb18d4e0706cb12698
7
+ data.tar.gz: 17421670cd5eb2fb65741de020808f6629b7545111fbc65675a37849eea32ead8b39dad3135e6ca33c75d02ce295611aab77fce345e0087748dc1b6a6ecf9b06
@@ -35,11 +35,15 @@ module Sequel
35
35
 
36
36
  pgt_trigger(counted_table, trigger_name, function_name, [:insert, :update, :delete], <<-SQL)
37
37
  BEGIN
38
- IF (TG_OP = 'INSERT' OR (TG_OP = 'UPDATE' AND NEW.#{id_column} <> OLD.#{id_column})) THEN
39
- UPDATE #{table} SET #{count_column} = #{count_column} + 1 WHERE #{main_column} = NEW.#{id_column};
40
- END IF;
41
- IF (TG_OP = 'DELETE' OR (TG_OP = 'UPDATE' AND NEW.#{id_column} <> OLD.#{id_column})) THEN
42
- UPDATE #{table} SET #{count_column} = #{count_column} - 1 WHERE #{main_column} = OLD.#{id_column};
38
+ IF (TG_OP = 'UPDATE' AND (NEW.#{id_column} = OLD.#{id_column} OR (OLD.#{id_column} IS NULL AND NEW.#{id_column} IS NULL))) THEN
39
+ RETURN NEW;
40
+ ELSE
41
+ IF ((TG_OP = 'INSERT' OR TG_OP = 'UPDATE') AND NEW.#{id_column} IS NOT NULL) THEN
42
+ UPDATE #{table} SET #{count_column} = #{count_column} + 1 WHERE #{main_column} = NEW.#{id_column};
43
+ END IF;
44
+ IF ((TG_OP = 'DELETE' OR TG_OP = 'UPDATE') AND OLD.#{id_column} IS NOT NULL) THEN
45
+ UPDATE #{table} SET #{count_column} = #{count_column} - 1 WHERE #{main_column} = OLD.#{id_column};
46
+ END IF;
43
47
  END IF;
44
48
 
45
49
  IF (TG_OP = 'DELETE') THEN
@@ -118,10 +122,10 @@ module Sequel
118
122
  IF (TG_OP = 'UPDATE' AND NEW.#{id_column} = OLD.#{id_column}) THEN
119
123
  UPDATE #{table} SET #{sum_column} = #{sum_column} + NEW.#{summed_column} - OLD.#{summed_column} WHERE #{main_column} = NEW.#{id_column};
120
124
  ELSE
121
- IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN
125
+ IF ((TG_OP = 'INSERT' OR TG_OP = 'UPDATE') AND NEW.#{id_column} IS NOT NULL) THEN
122
126
  UPDATE #{table} SET #{sum_column} = #{sum_column} + NEW.#{summed_column} WHERE #{main_column} = NEW.#{id_column};
123
127
  END IF;
124
- IF (TG_OP = 'DELETE' OR TG_OP = 'UPDATE') THEN
128
+ IF ((TG_OP = 'DELETE' OR TG_OP = 'UPDATE') AND OLD.#{id_column} IS NOT NULL) THEN
125
129
  UPDATE #{table} SET #{sum_column} = #{sum_column} - OLD.#{summed_column} WHERE #{main_column} = OLD.#{id_column};
126
130
  END IF;
127
131
  END IF;
@@ -29,26 +29,40 @@ describe "PostgreSQL Triggers" do
29
29
  end
30
30
 
31
31
  specify "Should modify counter cache when adding or removing records" do
32
- DB[:accounts].filter(:id=>1).get(:num_entries).should == 0
33
- DB[:accounts].filter(:id=>2).get(:num_entries).should == 0
32
+ DB[:accounts].order(:id).select_map(:num_entries).should == [0, 0]
33
+
34
34
  DB[:entries] << {:id=>1, :account_id=>1}
35
- DB[:accounts].filter(:id=>1).get(:num_entries).should == 1
36
- DB[:accounts].filter(:id=>2).get(:num_entries).should == 0
35
+ DB[:accounts].order(:id).select_map(:num_entries).should == [1, 0]
36
+
37
37
  DB[:entries] << {:id=>2, :account_id=>1}
38
- DB[:accounts].filter(:id=>1).get(:num_entries).should == 2
39
- DB[:accounts].filter(:id=>2).get(:num_entries).should == 0
40
- DB[:entries] << {:id=>3, :account_id=>2}
41
- DB[:accounts].filter(:id=>1).get(:num_entries).should == 2
42
- DB[:accounts].filter(:id=>2).get(:num_entries).should == 1
38
+ DB[:accounts].order(:id).select_map(:num_entries).should == [2, 0]
39
+
40
+ DB[:entries] << {:id=>3, :account_id=>nil}
41
+ DB[:accounts].order(:id).select_map(:num_entries).should == [2, 0]
42
+
43
+ DB[:entries].where(:id=>3).update(:account_id=>2)
44
+ DB[:accounts].order(:id).select_map(:num_entries).should == [2, 1]
45
+
43
46
  DB[:entries].where(:id=>2).update(:account_id=>2)
44
- DB[:accounts].filter(:id=>1).get(:num_entries).should == 1
45
- DB[:accounts].filter(:id=>2).get(:num_entries).should == 2
46
- DB[:entries].filter(:id=>2).delete
47
- DB[:accounts].filter(:id=>1).get(:num_entries).should == 1
48
- DB[:accounts].filter(:id=>2).get(:num_entries).should == 1
47
+ DB[:accounts].order(:id).select_map(:num_entries).should == [1, 2]
48
+
49
+ DB[:entries].where(:id=>2).update(:account_id=>nil)
50
+ DB[:accounts].order(:id).select_map(:num_entries).should == [1, 1]
51
+
52
+ DB[:entries].where(:id=>2).update(:id=>4)
53
+ DB[:accounts].order(:id).select_map(:num_entries).should == [1, 1]
54
+
55
+ DB[:entries].where(:id=>4).update(:account_id=>2)
56
+ DB[:accounts].order(:id).select_map(:num_entries).should == [1, 2]
57
+
58
+ DB[:entries].where(:id=>4).update(:account_id=>nil)
59
+ DB[:accounts].order(:id).select_map(:num_entries).should == [1, 1]
60
+
61
+ DB[:entries].filter(:id=>4).delete
62
+ DB[:accounts].order(:id).select_map(:num_entries).should == [1, 1]
63
+
49
64
  DB[:entries].delete
50
- DB[:accounts].filter(:id=>1).get(:num_entries).should == 0
51
- DB[:accounts].filter(:id=>2).get(:num_entries).should == 0
65
+ DB[:accounts].order(:id).select_map(:num_entries).should == [0, 0]
52
66
  end
53
67
  end
54
68
 
@@ -123,29 +137,43 @@ describe "PostgreSQL Triggers" do
123
137
  end
124
138
 
125
139
  specify "Should modify sum cache when adding, updating, or removing records" do
126
- DB[:accounts].filter(:id=>1).get(:balance).should == 0
127
- DB[:accounts].filter(:id=>2).get(:balance).should == 0
140
+ DB[:accounts].order(:id).select_map(:balance).should == [0, 0]
141
+
128
142
  DB[:entries] << {:id=>1, :account_id=>1, :amount=>100}
129
- DB[:accounts].filter(:id=>1).get(:balance).should == 100
130
- DB[:accounts].filter(:id=>2).get(:balance).should == 0
143
+ DB[:accounts].order(:id).select_map(:balance).should == [100, 0]
144
+
131
145
  DB[:entries] << {:id=>2, :account_id=>1, :amount=>200}
132
- DB[:accounts].filter(:id=>1).get(:balance).should == 300
133
- DB[:accounts].filter(:id=>2).get(:balance).should == 0
134
- DB[:entries] << {:id=>3, :account_id=>2, :amount=>500}
135
- DB[:accounts].filter(:id=>1).get(:balance).should == 300
136
- DB[:accounts].filter(:id=>2).get(:balance).should == 500
146
+ DB[:accounts].order(:id).select_map(:balance).should == [300, 0]
147
+
148
+ DB[:entries] << {:id=>3, :account_id=>nil, :amount=>500}
149
+ DB[:accounts].order(:id).select_map(:balance).should == [300, 0]
150
+
151
+ DB[:entries].where(:id=>3).update(:account_id=>2)
152
+ DB[:accounts].order(:id).select_map(:balance).should == [300, 500]
153
+
137
154
  DB[:entries].exclude(:id=>2).update(:amount=>Sequel.*(:amount, 2))
138
- DB[:accounts].filter(:id=>1).get(:balance).should == 400
139
- DB[:accounts].filter(:id=>2).get(:balance).should == 1000
155
+ DB[:accounts].order(:id).select_map(:balance).should == [400, 1000]
156
+
140
157
  DB[:entries].where(:id=>2).update(:account_id=>2)
141
- DB[:accounts].filter(:id=>1).get(:balance).should == 200
142
- DB[:accounts].filter(:id=>2).get(:balance).should == 1200
143
- DB[:entries].filter(:id=>2).delete
144
- DB[:accounts].filter(:id=>1).get(:balance).should == 200
145
- DB[:accounts].filter(:id=>2).get(:balance).should == 1000
158
+ DB[:accounts].order(:id).select_map(:balance).should == [200, 1200]
159
+
160
+ DB[:entries].where(:id=>2).update(:account_id=>nil)
161
+ DB[:accounts].order(:id).select_map(:balance).should == [200, 1000]
162
+
163
+ DB[:entries].where(:id=>2).update(:id=>4)
164
+ DB[:accounts].order(:id).select_map(:balance).should == [200, 1000]
165
+
166
+ DB[:entries].where(:id=>4).update(:account_id=>2)
167
+ DB[:accounts].order(:id).select_map(:balance).should == [200, 1200]
168
+
169
+ DB[:entries].where(:id=>4).update(:account_id=>nil)
170
+ DB[:accounts].order(:id).select_map(:balance).should == [200, 1000]
171
+
172
+ DB[:entries].filter(:id=>4).delete
173
+ DB[:accounts].order(:id).select_map(:balance).should == [200, 1000]
174
+
146
175
  DB[:entries].delete
147
- DB[:accounts].filter(:id=>1).get(:balance).should == 0
148
- DB[:accounts].filter(:id=>2).get(:balance).should == 0
176
+ DB[:accounts].order(:id).select_map(:balance).should == [0, 0]
149
177
  end
150
178
  end
151
179
 
@@ -198,9 +226,27 @@ describe "PostgreSQL Triggers" do
198
226
  DB[:children].update(:parent_id1=>2)
199
227
  DB[:parents].order(:id1).select_map(:changed_on).map{|t| t.strftime('%F')}.should == [d.strftime('%F'), d.strftime('%F')]
200
228
 
229
+ DB[:parents].update(:changed_on=>d30)
230
+ DB[:children].update(:parent_id1=>nil)
231
+ DB[:parents].order(:id1).select_map(:changed_on).map{|t| t.strftime('%F')}.should == [d30.strftime('%F'), d.strftime('%F')]
232
+
233
+ DB[:parents].update(:changed_on=>d30)
234
+ DB[:children].update(:parent_id2=>1)
235
+ DB[:parents].order(:id1).select_map(:changed_on).map{|t| t.strftime('%F')}.should == [d30.strftime('%F'), d30.strftime('%F')]
236
+
237
+ DB[:parents].update(:changed_on=>d30)
238
+ DB[:children].update(:parent_id1=>2)
239
+ DB[:parents].order(:id1).select_map(:changed_on).map{|t| t.strftime('%F')}.should == [d30.strftime('%F'), d.strftime('%F')]
240
+
201
241
  DB[:parents].update(:changed_on=>d30)
202
242
  DB[:children].delete
203
243
  DB[:parents].order(:id1).select_map(:changed_on).map{|t| t.strftime('%F')}.should == [d30.strftime('%F'), d.strftime('%F')]
244
+
245
+ DB[:parents].update(:changed_on=>d30)
246
+ DB[:children] << {:id=>2, :parent_id1=>nil}
247
+ DB[:parents].order(:id1).select_map(:changed_on).map{|t| t.strftime('%F')}.should == [d30.strftime('%F'), d30.strftime('%F')]
248
+ DB[:children].where(:id=>2).delete
249
+ DB[:parents].order(:id1).select_map(:changed_on).map{|t| t.strftime('%F')}.should == [d30.strftime('%F'), d30.strftime('%F')]
204
250
  end
205
251
 
206
252
  specify "Should update the timestamp column of the related table when there is a composite foreign key" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel_postgresql_triggers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-20 00:00:00.000000000 Z
11
+ date: 2014-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel