pg_party 0.6.0 → 0.7.1

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: f6b56b604caac2da35b9df72f1a43f1ac5464972
4
- data.tar.gz: 468454403e514a160bdc8d4eaeb1f3c5cfdd324b
3
+ metadata.gz: 0d8a08047a1190e154ea5e76a4cf5bafdad17829
4
+ data.tar.gz: 25a9a8be75f915006c1f36aa5f6cb2eb8111cb12
5
5
  SHA512:
6
- metadata.gz: 2c5e035dbee595f7a77fc8312618164f8abc4b4958263c37d827b9b43667f67f212361cd1b26318d8701c3dde333f3024fd826a53f3b451a9cb51e27e233dea2
7
- data.tar.gz: 196494724549d32744fdd76ff393a3afbf28d33b6db73219b275804b289fbe4dd847cb32bbb43e4be0a4c5b4e4973bd3c4db3d21b1570102b1e61b573e15d628
6
+ metadata.gz: 809f31226645fb6f22062d3aa1f2df143ad56798aeb94e65513f7937a451d52527dbe8116ec893a1936c75dd93bb17ea52165a311e341832538810f059d0527f
7
+ data.tar.gz: 070e5afc308dea1aef85f7de3c3f3b34ca586ceda999de73c9aaf787310e7d6425fe359df4f026be6260684d3d2c6e6f80ee166d7cf7e4df576c3a2313dee298
@@ -1,4 +1,5 @@
1
1
  require "digest"
2
+ require "pg_party/cache"
2
3
 
3
4
  module PgParty
4
5
  class AdapterDecorator < SimpleDelegator
@@ -46,6 +47,8 @@ module PgParty
46
47
  ATTACH PARTITION #{quote_table_name(child_table_name)}
47
48
  FOR VALUES FROM (#{quote(start_range)}) TO (#{quote(end_range)})
48
49
  SQL
50
+
51
+ PgParty::Cache.clear!
49
52
  end
50
53
 
51
54
  def attach_list_partition(parent_table_name, child_table_name, values:)
@@ -54,6 +57,8 @@ module PgParty
54
57
  ATTACH PARTITION #{quote_table_name(child_table_name)}
55
58
  FOR VALUES IN (#{Array.wrap(values).map(&method(:quote)).join(",")})
56
59
  SQL
60
+
61
+ PgParty::Cache.clear!
57
62
  end
58
63
 
59
64
  def detach_partition(parent_table_name, child_table_name)
@@ -61,6 +66,8 @@ module PgParty
61
66
  ALTER TABLE #{quote_table_name(parent_table_name)}
62
67
  DETACH PARTITION #{quote_table_name(child_table_name)}
63
68
  SQL
69
+
70
+ PgParty::Cache.clear!
64
71
  end
65
72
 
66
73
  private
@@ -121,6 +128,8 @@ module PgParty
121
128
  SQL
122
129
  end
123
130
 
131
+ PgParty::Cache.clear!
132
+
124
133
  child_table_name
125
134
  end
126
135
 
@@ -0,0 +1,35 @@
1
+ require "thread"
2
+
3
+ module PgParty
4
+ class Cache
5
+ LOCK = Mutex.new
6
+
7
+ class << self
8
+ def clear!
9
+ LOCK.synchronize { store.clear }
10
+
11
+ nil
12
+ end
13
+
14
+ def fetch_model(parent_table, child_table, &block)
15
+ LOCK.synchronize do
16
+ store[parent_table.to_sym][:models][child_table.to_sym] ||= block.call
17
+ end
18
+ end
19
+
20
+ def fetch_partitions(table_name, &block)
21
+ LOCK.synchronize do
22
+ store[table_name.to_sym][:partitions] ||= block.call
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def store
29
+ # automatically initialize a new hash when
30
+ # accessing a table name that doesn't exist
31
+ @store ||= Hash.new { |h, k| h[k] = { models: {}, partitions: nil } }
32
+ end
33
+ end
34
+ end
35
+ end
@@ -8,9 +8,7 @@ module PgParty
8
8
  end
9
9
 
10
10
  def table_exists?
11
- return @table_exists if defined?(@table_exists)
12
-
13
- @table_exists = PgParty::ModelDecorator.new(self).partition_table_exists?
11
+ PgParty::ModelDecorator.new(self).partition_table_exists?
14
12
  end
15
13
 
16
14
  def partitions
@@ -1,3 +1,5 @@
1
+ require "pg_party/cache"
2
+
1
3
  module PgParty
2
4
  class ModelDecorator < SimpleDelegator
3
5
  def partition_primary_key
@@ -16,25 +18,27 @@ module PgParty
16
18
  connection.schema_cache.send(table_exists_method, target_table)
17
19
  end
18
20
 
19
- def in_partition(table_name)
20
- Class.new(__getobj__) do
21
- self.table_name = table_name
22
-
23
- # to avoid argument errors when calling model_name
24
- def self.name
25
- superclass.name
26
- end
27
-
28
- # when returning records from a query, Rails
29
- # allocates objects first, then initializes
30
- def self.allocate
31
- superclass.allocate
32
- end
33
-
34
- # creating and persisting new records from a child partition
35
- # will ultimately insert into the parent partition table
36
- def self.new(*args, &blk)
37
- superclass.new(*args, &blk)
21
+ def in_partition(child_table_name)
22
+ PgParty::Cache.fetch_model(table_name, child_table_name) do
23
+ Class.new(__getobj__) do
24
+ self.table_name = child_table_name
25
+
26
+ # to avoid argument errors when calling model_name
27
+ def self.name
28
+ superclass.name
29
+ end
30
+
31
+ # when returning records from a query, Rails
32
+ # allocates objects first, then initializes
33
+ def self.allocate
34
+ superclass.allocate
35
+ end
36
+
37
+ # creating and persisting new records from a child partition
38
+ # will ultimately insert into the parent partition table
39
+ def self.new(*args, &blk)
40
+ superclass.new(*args, &blk)
41
+ end
38
42
  end
39
43
  end
40
44
  end
@@ -54,13 +58,15 @@ module PgParty
54
58
  end
55
59
 
56
60
  def partitions
57
- connection.select_values(<<-SQL)
58
- SELECT pg_inherits.inhrelid::regclass::text
59
- FROM pg_tables
60
- INNER JOIN pg_inherits
61
- ON pg_tables.tablename::regclass = pg_inherits.inhparent::regclass
62
- WHERE pg_tables.tablename = #{connection.quote(table_name)}
63
- SQL
61
+ PgParty::Cache.fetch_partitions(table_name) do
62
+ connection.select_values(<<-SQL)
63
+ SELECT pg_inherits.inhrelid::regclass::text
64
+ FROM pg_tables
65
+ INNER JOIN pg_inherits
66
+ ON pg_tables.tablename::regclass = pg_inherits.inhparent::regclass
67
+ WHERE pg_tables.tablename = #{connection.quote(table_name)}
68
+ SQL
69
+ end
64
70
  end
65
71
 
66
72
  def create_range_partition(start_range:, end_range:, **options)
@@ -1,3 +1,3 @@
1
1
  module PgParty
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_party
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Krage
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-23 00:00:00.000000000 Z
11
+ date: 2017-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -212,6 +212,7 @@ files:
212
212
  - lib/pg_party/adapter/abstract_methods.rb
213
213
  - lib/pg_party/adapter/postgresql_methods.rb
214
214
  - lib/pg_party/adapter_decorator.rb
215
+ - lib/pg_party/cache.rb
215
216
  - lib/pg_party/model/list_methods.rb
216
217
  - lib/pg_party/model/methods.rb
217
218
  - lib/pg_party/model/range_methods.rb
@@ -239,7 +240,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
239
240
  version: 1.8.11
240
241
  requirements: []
241
242
  rubyforge_project:
242
- rubygems_version: 2.6.11
243
+ rubygems_version: 2.4.5
243
244
  signing_key:
244
245
  specification_version: 4
245
246
  summary: ActiveRecord PostgreSQL Partitioning