db_memoize 0.3.11 → 0.3.12

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
  SHA256:
3
- metadata.gz: 863ab8bb61c16da8578b0047d538d40cdcaf2f567555dd97f24b698146066af7
4
- data.tar.gz: 1558260290eb7b3b68d821f679642b1589be653eda64bdec4b21a8b2bafbb689
3
+ metadata.gz: ce34701844a9a089e61d5010352b335795bcfb477d858c60189866012162e4d3
4
+ data.tar.gz: a2e8c91d767ff7550c6a84f3adcdb61288c60557700ac629f6473e73ce4ed64c
5
5
  SHA512:
6
- metadata.gz: 4069e4ed8a024353fd7f1cf378af08c32795b1aaaedc365a6091e56c515b662b3c0118e1a8a66c7b32a535c9d209398fbbc8fdb1a91283011732bbd3c41ac5f4
7
- data.tar.gz: 031ccecc72875c72509b75134203c0cc5ff1a7ad6cef82cf449eccca8166c31338b98bbb2129404be853e5641fa81bd8586820049dcec6775975a3deab873938
6
+ metadata.gz: 264acf8b331b10ed97680e1ee56752d2bbf9fa990ff0910c54711dd6d7b70b1e2aaa37c328f85161b38217e91dd1e9564ee86695f1ba5c7a3854e5d3ba1cfbe1
7
+ data.tar.gz: 58869f58b470ecff3ea1ab33e98ca81e657dce81e86df549e18aa5d2f5a7dabd8047145640c36a1661c187333e64baa2913190eb30d6a50225d9c29e50a97f81
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.11
1
+ 0.3.12
@@ -22,8 +22,7 @@ module DbMemoize
22
22
 
23
23
  -- entity_id/entity_table_name should have a better chance to be useful, since
24
24
  -- there is more variance in entity_ids than there is in entity_table_names.
25
- CREATE UNIQUE INDEX IF NOT EXISTS memoized_attributes_idx2
26
- ON db_memoize.memoized_values(entity_id, entity_table_name, method_name);
25
+ DROP INDEX IF EXISTS memoized_attributes_idx2;
27
26
  SQL
28
27
  end
29
28
  end
@@ -52,6 +52,14 @@ module DbMemoize
52
52
  def find_memoized_value(method_name)
53
53
  method_name = method_name.to_s
54
54
 
55
+ # In order to prevent database level deadlocks we don't manage any unique
56
+ # index on memoized values. This can result in duplicate matching memoized
57
+ # values.
58
+ #
59
+ # It is important to always return the freshest value. To make sure this
60
+ # happens the \a memoized_values association is ordered by its creation
61
+ # time (via "created_at DESC"), which lets us just return the first matching
62
+ # entry here.
55
63
  memoized_values.detect do |rec|
56
64
  rec.method_name == method_name
57
65
  end
@@ -124,7 +132,9 @@ module DbMemoize
124
132
  rec.memoized_values.delete_all_ordered
125
133
  end
126
134
 
127
- has_many :memoized_values, -> { where(conditions) },
135
+ # memoized_values for this object. These values must be returned
136
+ # newest first, see the comment in \a find_memoized_value.
137
+ has_many :memoized_values, -> { where(conditions).order('created_at DESC') },
128
138
  dependent: :delete_all, class_name: 'DbMemoize::Value', foreign_key: :entity_id
129
139
 
130
140
  end
@@ -37,7 +37,7 @@ module DbMemoize
37
37
  SQL.ask <<-SQL
38
38
  DO $$DECLARE c record;
39
39
  BEGIN
40
- FOR c IN #{sql} ORDER BY ctid LOOP
40
+ FOR c IN SELECT * FROM (#{sql}) sq ORDER BY ctid LOOP
41
41
  DELETE FROM #{DbMemoize::Value.table_name} WHERE ctid = c.ctid;
42
42
  END LOOP;
43
43
  END$$;
@@ -57,11 +57,6 @@ module DbMemoize
57
57
  def self.fast_create(entity_table_name, entity_id, method_name, value)
58
58
  method_name = method_name.to_s
59
59
 
60
- # clear out old entry (if any). This makes sure that any existing value
61
- # is cleared out properly.
62
- SQL.ask "DELETE FROM #{table_name} WHERE(entity_table_name, entity_id, method_name) = ($1, $2, $3)",
63
- entity_table_name, entity_id, method_name
64
-
65
60
  column = case value
66
61
  when String then :val_string
67
62
  when Integer then :val_integer
@@ -82,15 +77,14 @@ module DbMemoize
82
77
  when :val_time then value = _reformat_time(value)
83
78
  end
84
79
 
85
- other_columns = ALL_COLUMNS - [column]
86
- default_updates = other_columns.map { |c| "#{c}=NULL" }
87
-
80
+ # We initialize created_at with +statement_timestamp()+ since this
81
+ # reflects the current time when running the insert, resulting in
82
+ # increasing timestamps even within the same transaction.
83
+ #
84
+ # (This is only relevant for tests, though.)
88
85
  sql = <<~SQL.freeze
89
86
  INSERT INTO #{table_name}(entity_table_name, entity_id, method_name, #{column}, created_at)
90
- VALUES($1,$2,$3,$4,NOW())
91
- ON CONFLICT (entity_id, entity_table_name, method_name)
92
- DO UPDATE
93
- SET #{default_updates.join(', ')}, #{column}=$4, created_at=NOW()
87
+ VALUES($1,$2,$3,$4,statement_timestamp())
94
88
  SQL
95
89
 
96
90
  SQL.ask sql, entity_table_name, entity_id, method_name, value
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.3.11
4
+ version: 0.3.12
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: 2018-10-30 00:00:00.000000000 Z
11
+ date: 2018-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: simple-sql