db_memoize 0.2.2 → 0.2.3

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: ec9a05ceb15a0252b3e145983035f1192d9252c4
4
- data.tar.gz: 53190328b2d6a04e861bc0d4d88dd29aea61ad7c
3
+ metadata.gz: c30ad0bcaafd71d6b6e819e0d951561c0ccdee50
4
+ data.tar.gz: 99803b69c67ad890ed66a2648c4cd414e3c0bc2d
5
5
  SHA512:
6
- metadata.gz: 1da1fe68e2389d0d6a74ac03d38f486fcd249f58142f52e4fa5aa3b683a1905f2dc879bd57f188957519fb3181bcbd2fca2dcd521071e155ed3fda6e20e3cfe4
7
- data.tar.gz: f448a8946e63346fe401d763d396994638ced3cf9e25340ea36b40fcc26372654da88e9593bcbf18f596497325f3dc40c5ae4fc7877421a36d7f5b3489f41d0b
6
+ metadata.gz: bde9f19880b5809dbf35daf2b1664223f9db45033d1cc297a3714c41afbbec98b0099b4e232566c0be1346e78dada3c92b27c607224f5480599efe91de7a644b
7
+ data.tar.gz: 61aa061f01ba3df008f1b7be39f4e6510daf9f8e4dda38188b0c33deaace4c874cd60d426ea2cb3296ed4717f65635c5e4bdfa5ce9cb25cf47bd44042abaf020
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # db_memoize
2
2
  library to cache (memoize) method return values in database
3
3
 
4
+ **Note:** when updating from version 0.1 to 0.2 you need to run a migration on the existing table, see below.
5
+
4
6
  ## Usage in ActiveRecord models:
5
7
 
6
8
  ### Example Class:
@@ -112,6 +114,38 @@ Note that db_memoize needs Postgres. To set up the database needed to run tests,
112
114
  # createdb -O >>yourusername<< db_memoize_test
113
115
 
114
116
 
117
+ ### Updating from 0.1 -> 0.2
118
+
119
+ You need to run a migration, like the following:
120
+
121
+ ```
122
+ class FixDbMemoizeTable < ActiveRecord::Migration
123
+ def up
124
+ # After updating db_memoize to 0.2 we need to update some structures, depending
125
+ # on whether or not db_memoize 0.1 or 0.2 was creating the database tables.
126
+ execute <<-SQL
127
+ ALTER TABLE memoized_values
128
+ ALTER COLUMN entity_table_name SET NOT NULL,
129
+ ALTER COLUMN entity_id SET NOT NULL,
130
+ ALTER COLUMN method_name SET NOT NULL,
131
+ ALTER COLUMN created_at SET NOT NULL;
132
+ DROP INDEX IF EXISTS index_memoized_values_on_entity_table_name_and_entity_id;
133
+ CREATE INDEX IF NOT EXISTS index_memoized_values_on_entity_id_and_entity_table_name
134
+ ON memoized_values(entity_id, entity_table_name);
135
+ CREATE INDEX IF NOT EXISTS memoized_attributes_idx
136
+ ON memoized_values (((arguments_hash IS NULL)))
137
+ SQL
138
+ end
139
+
140
+ def down
141
+ execute <<-SQL
142
+ DROP INDEX memoized_attributes_idx;
143
+ DROP INDEX index_memoized_values_on_entity_id_and_entity_table_name;
144
+ SQL
145
+ end
146
+ end
147
+ ```
148
+
115
149
  Have fun!
116
150
 
117
151
 
@@ -26,13 +26,7 @@ module DbMemoize
26
26
  end
27
27
 
28
28
  def unmemoize(method_name = :all)
29
- if method_name != :all
30
- # FIXME: this works, but isn't immediately visible on the record.
31
- # See also note in create_memoized_value.
32
- memoized_values.where(method_name: method_name).delete_all
33
- else
34
- memoized_values.clear
35
- end
29
+ self.class.unmemoize id, method_name
36
30
  end
37
31
 
38
32
  #
@@ -96,7 +90,7 @@ module DbMemoize
96
90
  }
97
91
  conditions[:method_name] = method_name unless method_name == :all
98
92
 
99
- DbMemoize::Value.where(conditions).delete_all
93
+ DbMemoize::Value.where(conditions).delete_all_ordered
100
94
  end
101
95
 
102
96
  def memoize_values(records_or_ids, values, *args)
@@ -130,8 +124,23 @@ module DbMemoize
130
124
  def create_memoized_values_association
131
125
  unless reflect_on_association(:memoized_values)
132
126
  conditions = { entity_table_name: table_name }
127
+
128
+ # By defining this before_destroy callback we make sure **we** delete all
129
+ # memoized values before Rails deletes those via `has_many dependent:
130
+ # This leads to has_many later on not finding any values to be deleted.
131
+ #
132
+ # It would be nice if there was a `dependent: :manual/:noop` option.
133
+ #
134
+ # **Note:** before_destroy must be called before memoized_values is
135
+ # set up, to make sure that these things happen in the right order.
136
+ #
137
+ before_destroy do |rec|
138
+ rec.memoized_values.delete_all_ordered
139
+ end
140
+
133
141
  has_many :memoized_values, -> { where(conditions) },
134
142
  dependent: :delete_all, class_name: 'DbMemoize::Value', foreign_key: :entity_id
143
+
135
144
  end
136
145
  end
137
146
  end
@@ -3,5 +3,20 @@ module DbMemoize
3
3
  self.table_name = 'memoized_values'
4
4
 
5
5
  include DbMemoize::Metal
6
+
7
+ def self.delete_all_ordered
8
+ relation = self
9
+ relation = all unless is_a?(ActiveRecord::Relation)
10
+
11
+ sql = relation.select(:ctid).to_sql
12
+ connection.execute <<-SQL
13
+ DO $$DECLARE c record;
14
+ BEGIN
15
+ FOR c IN #{sql} ORDER BY ctid LOOP
16
+ DELETE FROM #{DbMemoize::Value.table_name} WHERE ctid = c.ctid;
17
+ END LOOP;
18
+ END$$;
19
+ SQL
20
+ end
6
21
  end
7
22
  end
@@ -1,3 +1,3 @@
1
1
  module DbMemoize
2
- VERSION = '0.2.2'.freeze
2
+ VERSION = '0.2.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db_memoize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - johannes-kostas goetzinger
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-26 00:00:00.000000000 Z
11
+ date: 2017-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties