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 +4 -4
- data/lib/pg_party/adapter_decorator.rb +9 -0
- data/lib/pg_party/cache.rb +35 -0
- data/lib/pg_party/model/shared_methods.rb +1 -3
- data/lib/pg_party/model_decorator.rb +32 -26
- data/lib/pg_party/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d8a08047a1190e154ea5e76a4cf5bafdad17829
|
4
|
+
data.tar.gz: 25a9a8be75f915006c1f36aa5f6cb2eb8111cb12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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(
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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)
|
data/lib/pg_party/version.rb
CHANGED
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.
|
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-
|
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.
|
243
|
+
rubygems_version: 2.4.5
|
243
244
|
signing_key:
|
244
245
|
specification_version: 4
|
245
246
|
summary: ActiveRecord PostgreSQL Partitioning
|